哥们指点下这个顺序表
#include <iostream>#include <cstdlib>
using namespace std;
struct list{
int *list1;
int maxsize;
int size;
};
void initlist(list &l);
int lengthlist(list &l);
void travelselist(list &l);
bool findlist(list &l,int &item);
bool deletelist(list &l,int &item,int pos);
bool insertlist(list &l,int &item,int pos);
int getlist(list &l,int pos);
void main()
{
int a[6]={233,4,5 ,2,88,11};
int x;
list t;
initlist (t);//初始化表
int pos;
cout<<"please enter the insert pos"<<endl;
cin>>pos;
cout<<"please enter the insert item"<<endl;
int item;
cin>>item;
insertlist(t,item,pos);//插入特定数字
cout<<getlist(t,2)<<endl;//得到一个数
travelselist(t);//遍历表
cout<<"输入待查找的数"<<endl;//查找特定的数字
cin>>x;
if(findlist(t,x))
cout<<"查找成功"<<endl;
else
cout<<"查找失败"<<endl;
cout<<"enter dai shan chu de zi"<<endl;
cin>>x;
if (deletelist(t,x,0))//删除一个数
cout<<"it is sucess"<<endl;
else
cout<<"it is wrong"<<endl;
travelselist(t);
cout<<"线性表的长度"<<lengthlist (t)<<endl;//求长度
}
void initlist(list &l)
{
l.maxsize=10;
l.list1=new int [l.maxsize];
if(l.list1==0){
cout<<"kong jian yong wan"<<endl;
exit (1);
}
l.size=0;
}
int lengthlist(list &l)
{
return l.size;
}
void travelselist(list &l)
{
for(int i=0;i<l.size;i++)
cout<<l.list1[i]<<" ";
cout<<endl;
}
bool findlist(list &l,int &item)
{
for(int i=0;i<l.size;i++)
if(l.list1[i]==item){
item=l.list1[i];
return true;
}
return false;
}
bool insertlist(list &l,int &item,int pos)
{
if(pos>l.size){
pos=l.size+1;//最后一个元素后插入
l.list1[pos]=item;
}
if(pos<0){
pos=1;
l.list1[pos]=item;
l.size++;
}
for(int k=l.size;k>=pos;k--)
{
l.list1[k]=l.list1[k-1];
l.list1[pos]=item;
l.size++;
return true;
}
return false;
}
bool deletelist(list &l,int &item,int pos)
{
if(pos<0||pos>l.size)
cout<<"it is wrong";
for(int k=pos;k<l.size;k++)
{
l.list1[k-1]=l.list1[k];
l.size--;
return true;
}
return false;
}
int getlist(list &l,int pos)
{
return l.list1[pos-1];
}