主题:顺序链表的实现 有几个错误找不出来~~
#include <iostream>
using namespace std;
typedef int elemtype;
typedef int status;
#define maxsize 10
#define incerment 3
#define error 0
#define ok 1
#define overflow -2
#define NULL 0
#define infeasible -1
typedef struct
{
elemtype *base;
int length;
int listsize;
}List;
status init_list(List &L)
{
L.base=(elemtype *)malloc(maxsize*sizeof(elemtype));
if(!L.base)exit(overflow);
int n,j=1;
cout<<"请最多输入10个数据,输入n的值为0 时建立完毕"<<endl;
cout<<"输入N的值"<<endl;
while(n && j>10)
{
cout<<"请输入第 "<<j<<"个数据的值"<<endl;
cin>>*(L.base+j-1);
j++;
cout<<"输入N的值"<<endl;
cin>>n;
}
L.length=j-1;
L.listsize=maxsize*malloc(sizeof(elemtype));
cout<<"连表建立成功,共"<<L.length<<"个元素"<<endl;
return ok;
}
status list_length(List &L)
{
return L.length;
}
status list_empty(List &L)
{
if(L.length==0)return false;
else return true;
}
status list_destroy(List &L)
{
L.base=NULL;
L.base=0;
L.listsize=0;
return ok;
}
status list_set_empty(List &L)
{
L.length=0;
return ok;
}
status list_insert(List &L,int i,elemtype e)
{
if(i>L.length || i<1)
return error;
if(L.length>=maxsize)
{
L.base=(elemtype *)realloc(L.base,malloc(sizeof(elemtype)*(increment+L.length)));
if(!L.base)exit(overflow);
L.listsize=L.listsize+malloc(sizeof(elemtype)*increment);
}
*(L.base+i-1)=e;
++L.length;
return ok;
}
status list_del(List &L,int i)
{
if(i>L.length || i<1) return error;
for(int j=i;j<L.length;j++)
{
*(L.base+j-1)=*(L.base+j);
}
--L.length;
return ok;
}
status list_priorelem(List &L,elemtype &cue,elemtype &pre)
{
int i=2;
while(*(L.base+i-1)!=cue && i<=L.length)
{
i++;
}
if(i>L.length)return infeasible;
pre=*(L.base+i-2);
return ok;
}
status list_nextelem(List &L,elemtype &cue,elemtype &pre)
{
int i=1;
while(*(L.base+i-1)!=pre && i<=L.length)
{
i++;
}
if(i==L.length)return infeasible;
cue=*(L.base+i);
return ok;
}
int maint()
{ int i,n;
List list[maxsize];
elemtype e,pre,cue;
do{
cout<<" **************************************************************** "<<endl;
cout<<" * <1>建立连表 <2>销毁连表 <3>获得连表长度 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <4>将表置空 <5>判断表是否为空 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <6>判断元素的存在 <7>返回指定第I个数据 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <8>返回指定元素的后继 <9>返回指定元素的前驱 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <10>插入新数据 <11> 删除元素 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <12>用VISTI函数访问 <13> 退出 * "<<endl;
cout<<" ***************************************************************** "<<endl;
cout<<"请输入您要的操作"<<endl;
cin>>n;
while(n>13 || n<1)
{
cout<<"请输入正确的序号"<<endl;
cin>>n;
}
switch(n)
{
case 1:init_list(list);break;
case 2:{
list_destroy(list);
cout<<" 连表销毁"<<endl;
};break;
case 3:{
i=list_length(list);
cout<<"连表长度为"<<i<<endl;
};break;
case 4:list_set_empty(list);break;
case 5:{
i=list_empty(list);
if(i)cout<<"连表不为空"<<endl;
else cout<<"连表为空"<<endl;
};break;
case 10:
{
cout<<"请输入你要在那一个元素前插入元素的序号"<<endl; //和
cin>>i;
cout<<"你要插入的元素的值"<<endl;
cin>>e;
cout<<endl;
list_insert(list,i,e);
};
break;
case 11:{
cout<<"请输入您要删除的元素的序号"<<endl;
cin>>i;
list_del(list,i);
};
break;
case 9:{
cout<<"请输入该元素的序号"<<endl;
cin>>i;
list_priorelem(list,i,e);
cout<<"该元素的前驱是"<<e<<endl;
};
break;
case 8:{
cout<<"请输入该元素的序号"<<endl;
cin>>i;
list_nextelem(list,cue,pre);
cout<<"该元素的后继是"<<e<<endl;
};break;
}
while(n!=13);
cout<<"谢谢再见"<<endl;
/*List list[maxsize]; */
return 0;
}
using namespace std;
typedef int elemtype;
typedef int status;
#define maxsize 10
#define incerment 3
#define error 0
#define ok 1
#define overflow -2
#define NULL 0
#define infeasible -1
typedef struct
{
elemtype *base;
int length;
int listsize;
}List;
status init_list(List &L)
{
L.base=(elemtype *)malloc(maxsize*sizeof(elemtype));
if(!L.base)exit(overflow);
int n,j=1;
cout<<"请最多输入10个数据,输入n的值为0 时建立完毕"<<endl;
cout<<"输入N的值"<<endl;
while(n && j>10)
{
cout<<"请输入第 "<<j<<"个数据的值"<<endl;
cin>>*(L.base+j-1);
j++;
cout<<"输入N的值"<<endl;
cin>>n;
}
L.length=j-1;
L.listsize=maxsize*malloc(sizeof(elemtype));
cout<<"连表建立成功,共"<<L.length<<"个元素"<<endl;
return ok;
}
status list_length(List &L)
{
return L.length;
}
status list_empty(List &L)
{
if(L.length==0)return false;
else return true;
}
status list_destroy(List &L)
{
L.base=NULL;
L.base=0;
L.listsize=0;
return ok;
}
status list_set_empty(List &L)
{
L.length=0;
return ok;
}
status list_insert(List &L,int i,elemtype e)
{
if(i>L.length || i<1)
return error;
if(L.length>=maxsize)
{
L.base=(elemtype *)realloc(L.base,malloc(sizeof(elemtype)*(increment+L.length)));
if(!L.base)exit(overflow);
L.listsize=L.listsize+malloc(sizeof(elemtype)*increment);
}
*(L.base+i-1)=e;
++L.length;
return ok;
}
status list_del(List &L,int i)
{
if(i>L.length || i<1) return error;
for(int j=i;j<L.length;j++)
{
*(L.base+j-1)=*(L.base+j);
}
--L.length;
return ok;
}
status list_priorelem(List &L,elemtype &cue,elemtype &pre)
{
int i=2;
while(*(L.base+i-1)!=cue && i<=L.length)
{
i++;
}
if(i>L.length)return infeasible;
pre=*(L.base+i-2);
return ok;
}
status list_nextelem(List &L,elemtype &cue,elemtype &pre)
{
int i=1;
while(*(L.base+i-1)!=pre && i<=L.length)
{
i++;
}
if(i==L.length)return infeasible;
cue=*(L.base+i);
return ok;
}
int maint()
{ int i,n;
List list[maxsize];
elemtype e,pre,cue;
do{
cout<<" **************************************************************** "<<endl;
cout<<" * <1>建立连表 <2>销毁连表 <3>获得连表长度 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <4>将表置空 <5>判断表是否为空 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <6>判断元素的存在 <7>返回指定第I个数据 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <8>返回指定元素的后继 <9>返回指定元素的前驱 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <10>插入新数据 <11> 删除元素 * "<<endl;
cout<<" * * "<<endl;
cout<<" * <12>用VISTI函数访问 <13> 退出 * "<<endl;
cout<<" ***************************************************************** "<<endl;
cout<<"请输入您要的操作"<<endl;
cin>>n;
while(n>13 || n<1)
{
cout<<"请输入正确的序号"<<endl;
cin>>n;
}
switch(n)
{
case 1:init_list(list);break;
case 2:{
list_destroy(list);
cout<<" 连表销毁"<<endl;
};break;
case 3:{
i=list_length(list);
cout<<"连表长度为"<<i<<endl;
};break;
case 4:list_set_empty(list);break;
case 5:{
i=list_empty(list);
if(i)cout<<"连表不为空"<<endl;
else cout<<"连表为空"<<endl;
};break;
case 10:
{
cout<<"请输入你要在那一个元素前插入元素的序号"<<endl; //和
cin>>i;
cout<<"你要插入的元素的值"<<endl;
cin>>e;
cout<<endl;
list_insert(list,i,e);
};
break;
case 11:{
cout<<"请输入您要删除的元素的序号"<<endl;
cin>>i;
list_del(list,i);
};
break;
case 9:{
cout<<"请输入该元素的序号"<<endl;
cin>>i;
list_priorelem(list,i,e);
cout<<"该元素的前驱是"<<e<<endl;
};
break;
case 8:{
cout<<"请输入该元素的序号"<<endl;
cin>>i;
list_nextelem(list,cue,pre);
cout<<"该元素的后继是"<<e<<endl;
};break;
}
while(n!=13);
cout<<"谢谢再见"<<endl;
/*List list[maxsize]; */
return 0;
}