使用C++實現線性表的基本功能

#include <iostream.h>
#include <conio.h>
#include <alloc.h>
typedef int elemtype;
//線性表的基本操作
void initiallist(elemtype *l);
int isempty(elemtype *l);
void listtraverse(elemtype *l);
int nextelem(elemtype *l);
int priorelem(elemtype *l);
int locateelem(elemtype *l,elemtype &e);
void getelem(elemtype *l);
void listinsert(elemtype *l);
void listdelete(elemtype *l);
void clearlist(elemtype *l);
const int n=10;
elemtype temp;//全局變數!
void initiallist(elemtype *l)
{
for(int i=0;i<n;i++)
*(l+i)='#';
}
int isempty(elemtype *l)
{
if(*l=='#')
return 1;
else
return 0;
}
void listtraverse(elemtype *l)
{
static int k;
if(*(l)==35)
cout<<"the list is null!\n";
else
{
cout<<"the records of the list are:\n";
for(int i=0;i<n+k;i++)
{
if((*(l+i)>32768))
break;
else
cout<<*(l+i)<<" ";
}
k++;
}
}
int nextelem(elemtype *l)
{
int index;
elemtype e;
cout<<"input the records for searching it's next elem!\n";
cin>>e;
index=locateelem(l,e);
if(*(l+index+1)>32768)
cout<<"it has no next elem!\n";
else
cout<<e<<"的後繼是:"<<*(l+index+1)<<endl;
}

int priorelem(elemtype *l)
{
int index;
elemtype e;
cout<<"input the records for searching it's prior elem!\n";
cin>>e;
index=locateelem(l,e);
if(index>n)
cout<<"it has no next elem!\n";
else if(index==-1||index==0)
{
cout<<"it has no prior elem!\n";
return 0;
}
else
cout<<e<<"的前驅是:"<<*(l+index-1)<<endl;
}
int locateelem(elemtype *l,elemtype &e)
{
int i;
for(i=0;i<n+1&&(*(l+i)!='#');i++)
{
if(*(l+i)==e)
return i;
else
continue;
}
if(i<n||i>=n)
return -1;
return i;
}
void getelem(elemtype *l)
{
int index;
cout<<"input the value of i:\n";
cin>>index;
if(*(l+index-1)>32768)
cout<<"the records null!\n";
else
cout<<"the records which you want to search are: "<<*(l+index-1)<<endl;
}
void listinsert(elemtype *l)
{
elemtype e;
int index,i;
cout<<"please input only one inserted number and it's location!\n";
cin>>e;
cin>>index;
if(index>n)
*(l+index-1)=e;
else
{
for(i=n;i>=index;i--)
*(l+i)=*(l+i-1);
*(l+index-1)=e;
}
listtraverse(l);
}
void listdelete(elemtype *l)
{
elemtype e;
int index,i;
cout<<"input the number which you want to deleted!\n";
cin>>e;
index=locateelem(l,e);
for(i=index;i<n+1&&(*(l+i)!='#');i++)
*(l+i-1)=*(l+i);
cout<<"deleted "<<e<<endl;
listtraverse(l);
}
void clearlist(elemtype *l)
{
for(int i=0;i<n+1;i++)
*(l+i)='#';
}
int main()
{
int choice,flag=0;
char ch;
elemtype *p,e;
p=(elemtype *)malloc((n+1)*sizeof(elemtype));
if(!p)
{
cout<<"no more memory can be obtained!\n";
goto loop;
}
loop: p=(elemtype *)realloc(p,(n+1)*sizeof(elemtype));
if(!p)
{
cout<<"overflow!\n";
exit(0);
}
initiallist(p);
cout<<"input "<<n<<" records!\n";//數據互不相同!
for(int i=0;i<n;i++)
cin>>*(p+i);
if(isempty(p))
cout<<"the list is null!\n";
cout<<" menu fuction \n"
<<" 1: 遍歷線性表的元素!( 只允許調用一次!)\n"
<<" 2: 求某一元素的前驅和後繼!\n"
<<" 3: 獲取線性表l中的第i個數據元素內容!\n"
<<" 4: 線上性表中插入一個元素!\n"
<<" 5: 刪除線性表中值為e的元素!\n"
<<" 6: 檢索值為e的數據元素!\n"
<<" 7: 清空線性表!"<<endl;
do
{
cout<<"please input your choice!\n";
cin>>choice;
switch(choice)
{
case 1: listtraverse(p); break;
case 2: nextelem(p);
priorelem(p);
break;
case 3: getelem(p); break;
case 4: listinsert(p); break;
case 5: listdelete(p); break;
case 6:
cout<<"input the records !\n" ;
cin>>e;
cout<<"it's location is "<<locateelem(p,e)+1;
break;
case 7: clearlist(p);
cout<<"now ";
listtraverse(p);
break;
}

cout<<"\ndid you want to continue the operation?(y/n)\n";
cin>>ch;
if(ch=='y'||ch=='y')
flag=1;
else
flag=0;
}while(flag);
free(p);
getch();
return 0;
}