#include<iostream>
#include<stdio.h>
using namespace std;
#define ok 0
#define error -1
#define max 7
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, int b);//洗掉值為b的結點
status nextelem(sqlist L, typeelem e);
status priorelem(sqlist L, typeelem e);
status clearlist(sqlist &L);
status destroylist(sqlist &L);
int main()
{
sqlist L;
int i, d, c, b;
typeelem e, a;
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);
cout << "listinsert"<< "put nuumber d" << endl;
cin >> d;
cout << "elem a" << endl;
cin >> a;
listinsert(L, d, a);
cout << "traverselist" << endl;
traverselist(L);
cout << "listdelete" << "put number b" << endl;
cin >> b;
listdelete(L, b);
cout << "nextelem" << endl;*/
nextelem(L, e);
priorelem(L, e);
clearlist(L);
destroylist(L);
// cout << "traverselist" << endl;
traverselist(L);
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;
if (p == NULL)
{
cout << "the list is empty" << endl;
return error;
}
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(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)
{
cout << c+1 << "isn't ture" << endl;
return error;
}
else
{
cout << "the potition is" << c << endl;
cout << q->data << endl;
return ok;
}
}
status listinsert(sqlist &L, int d, typeelem a)
{
int i;
noa q=L.head->next;
noa p=new noe[1];
for (i=1; i<d-1; i++)
{
q = q->next;
}
p->data = https://bbs.csdn.net/topics/a;
p->next = q->next;
q->next = p;
L.leng++;
}
status listdelete(sqlist &L, int b)
{
int i;
if(b<1||b>L.leng)
return error;
noa p = L.head->next;
noa q;
for (i = 1; i < b-1; i++)
{
p = p->next;
}
q = p->next;
p->next = p->next->next;
delete[] q;
L.leng--;
}
status nextelem(sqlist L, typeelem e)
{
int c=1;
noa q = L.head->next;
while (e != q->data && q->next != NULL)
{
q = q->next;
c++;
}
c++;
if (c > L.leng)
{
cout << e <<" doesn't have next elem" << endl;
return ok; }
else
{
cout << e << " have next elem" << endl;
q = q->next;
cout << "the elem is" <<" "<<q->data << endl;
return ok;
}
}
status priorelem(sqlist L, typeelem e)
{
noa q;
noa p = L.head->next;
if(e==p->data)
{
cout << e << " " << "doesn't have prior elem" << endl;
return ok;
}
while (e != p->data && p->next != NULL)
{
q = p;
p = p->next;
}
cout << e << ""<< " have prior elem" << endl;
cout << "the elem is " << q->data << endl;
return ok;
}
status clearlist(sqlist &L)
{
noa p = L.head->next;
noa q;
L.tail = L.head;
while (p != NULL)
{
q = p;
p = p->next;
delete[] q;
}
return ok;
}
status destroylist(sqlist &L)
{
noa p=L.head;
noa q;
L.tail = L.head;
while (p != NULL)
{
q = p;
p = p->next;
delete[] q;
}
return ok;
}
我想詢問一下destroylist函式的毀表為什么進行不下去,


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284987.html
標籤:C++ 語言
上一篇:大佬,求教c++
下一篇:新人求助