64,685
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<stdio.h>
using namespace std;
#define ok 0
#define error -1
#define max 6
typedef char typeelem;
typedef int status;
typedef struct no
{
struct no *next;
typeelem data;
} noe, *noa;
typedef struct noc
{
noa tail, head;
int leng;
} sqlist;
status initlist(sqlist &L);//创建一个空的链式线性表
status shuzhi(sqlist &L);//对表进行创建max大小链式表
status traverselist(sqlist L);//对链式表进行遍历
status getelem(sqlist L, int i);//在链式表中将第i个元素取出
status locateelem(sqlist L, typeelem e, int c);//在链式表中找值为e的结点并找第c个结点的值
status listinsert(sqlist &L, int d, typeelem a);//链式表中插入值
status listdelete(sqlist &L, typeelem b);//删除值为b的结点
int main()
{
sqlist L;
int i, d, c;
typeelem e, a, b;
initlist(L);
cout << "put max=" << max << "elem" << endl;
shuzhi(L);
cout << "traverselist" << endl;
traverselist(L);
cout << "put i from 1-" << max << endl;
cin >> i;
getelem(L, i);
cout << "seek number c and elem e" << endl;
cin >> e;
locateelem(L, e, c);
return ok;
}
status initlist(sqlist &L)
{
L.head = new noe[1];
if(!L.head)
{
cout <<"\bfailure to open up space" << endl;
return error;
}
else
L.tail = L.head;
L.leng = 0;
}
status shuzhi(sqlist &L)
{
noa q;
L.leng = max;
for (int i = 0; i < L.leng; i++)
{
q = new noe[1];
if (!q)
{
cout << "failure to open up space" << endl;
return error;
}
cin >> q->data;
L.tail->next = q;
L.tail = q;
q->next = NULL;
}
return ok;
}
status traverselist(sqlist L)
{
noa p;
p = L.head->next;
for (int i = 0; i < L.leng; i++)
{
cout << p->data << endl;
p = p->next;
}
return ok;
}
status getelem(sqlist L, int i)
{
int j;
noa q = L.head;
if(i>max || i<1) //if(max<i<0)
{
cout << "i is error" << endl;
return error;
}
else
for (j = 1; j <= i; j++)
{
q = q->next;
}
cout << q->data << endl;
return ok;
}
status locateelem(sqlist L, typeelem e, int c)
{
c = 1;
noa q = L.head->next;
while(q->data != e && q->next!=NULL)
{
q = q->next;
c++;
}
if(q->data != e)//if (c > L.leng)
{
cout << c+1 << " isn't ture" << endl;
return error;
}
else
{
cout << "the potition is " << c << endl;
cout << q->data << endl;
return ok;
}
}