主要是线性表的建立,删除,插入。
完整代码:
#include<iostream>
#include<malloc.h>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXSIZE 100
#define List_Init_Size 50
#define Increase 20
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Statue;
typedef int Elemtype;
typedef struct Sqlist
{
Elemtype *ElemArry;
int length;
int Listsize;
}Sqlist;
Statue InitList(Sqlist &L)
{
L.ElemArry=(Elemtype*)malloc(List_Init_Size*sizeof(Elemtype));
if(!L.ElemArry) exit(OVERFLOW);
L.length=0;
L.Listsize=List_Init_Size;
return OK;
}
void CreatList(Sqlist &L)
{
int n,i;
cout<<"Input the number of elem n:";
cin>>n;
cout<<"Input the list"<<endl;
for(i=0;i<n;i++)
cin>>L.ElemArry[i];
L.length=n;
}
bool Emptylist(Sqlist L)
{
if(L.length==0) return true;
else return false;
}
Statue Listlength(Sqlist L)
{
return L.length;
}
Elemtype Getelem(Sqlist L,int i)
{
return L.ElemArry[i-1];
}
Statue Findelem(Sqlist L,Elemtype key)
{
int i;
for(i=0;i<L.length;i++)
if(L.ElemArry[i]==key) return i+1;
return 0;
}
Statue Priorelem(Sqlist L,Elemtype key)
{
Statue t;
t=Findelem(L,key);
if(t)
{
if(t==1) return 0; //Find e but e is the first elem no prior elem
else return t-1; //the i-1
}
else
return -1; //list no e
}
Statue Nextelem(Sqlist L,Elemtype key)
{
Statue t;
t=Findelem(L,key);
if(t)
{
if(t==Listlength(L)) return 0; //e is the last elem no next elem
else return t; //i+1
}
else return -1; //list no e
}
Statue InsertElem(Sqlist &L,int i,Elemtype key)
{
if(i<1 || i>Listlength(L)+1) return ERROR;
if(Listlength(L)>=L.Listsize)
{
Elemtype *newbase;
newbase=(Elemtype*)realloc(L.ElemArry,(L.Listsize+Increase)*sizeof(Elemtype));
L.ElemArry=newbase;
L.Listsize+=Increase;
}
Elemtype *p,*q;
q=&L.ElemArry[i-1];
for(p=&L.ElemArry[Listlength(L)];p>=q+1;p--)
*(p)=*(p-1);
*q=key;
L.length++;
return OK;
}
Statue DeletElem(Sqlist &L,int i)
{
if(i<1 || i>L.length) return ERROR;
Elemtype *p,*q;
p=&L.ElemArry[i];
q=L.ElemArry+L.length;
for(p=p;p<=q;p++) *(p)=*(p+1);
L.length--;
return OK;
}
void Print(Sqlist L)
{
int i;
if(L.length<1) cout<<"The list is empty!"<<endl;
else
{
for(i=0;i<L.length;i++)
cout<<L.ElemArry[i]<<" ";
cout<<endl;
}
}
int main()
{
Sqlist L;
int operate,i,t;
Elemtype e;
InitList(L);
CreatList(L);
operate=1;
while(operate)
{
cout<<"Operate 1:Check the list is empty or not."<<endl;
cout<<"Operate 2:Output the length of the list."<<endl;
cout<<"Operate 3:Output the ith number of the list."<<endl;
cout<<"Operate 4:Output the location of the elem e."<<endl;
cout<<"Operate 5:Outpuet the e's prior elem."<<endl;
cout<<"Operate 6:Output the e's next elem."<<endl;
cout<<"Operate 7:Insert e at the front of ith position."<<endl;
cout<<"Operate 8:Delete the ith elem,and print it."<<endl;
cout<<"Operate 9:Print the list."<<endl;
cout<<"Operate 0:Operate end!"<<endl;
cin>>operate;
switch(operate)
{
case 1:if(Emptylist(L)) cout<<"List is empty"<<endl;
else cout<<"List has elem."<<endl;
break;
case 2:cout<<"The length of the list is "<<Listlength(L)<<endl;
break;
case 3:cin>>i;
if(i>Listlength(L) || i<0) cout<<"i is two illegal."<<endl;
else
cout<<"The ith elem is:"<<Getelem(L,i)<<endl;
break;
case 4:cin>>e;
t=Findelem(L,e);
if(t)
cout<<"The position of the e is :"<<t<<endl;
else cout<<"No such elem."<<endl;
break;
case 5:cin>>e;
t=Priorelem(L,e);
if(t) cout<<"The e's prior is :"<<Getelem(L,t)<<endl;
else if(!t) cout<<"The e is the first elem,no prior."<<endl;
else cout<<"List no e."<<endl;
break;
case 6:cin>>e;
t=Nextelem(L,e);
if(t) cout<<"The e's next elem is :"<<Getelem(L,t)<<endl;
else if(!t) cout<<"The e is the last elem,no next."<<endl;
else cout<<"List no e."<<endl;
break;
case 7:cin>>e>>i;
t=InsertElem(L,i,e);
if(t)
{
cout<<"Insert success.The list is:"<<endl;
for(i=0;i<L.length;i++)
cout<<L.ElemArry[i]<<" ";
cout<<endl;
}
else cout<<"Inset fail."<<endl;
break;
case 8:cin>>i;
t=DeletElem(L,i);
if(t)
{
cout<<"Dlete success.The elem is:"<<L.ElemArry[i]<<endl;
for(i=0;i<L.length;i++)
cout<<L.ElemArry[i]<<" ";
cout<<endl;
}
else cout<<"Delete fail."<<endl;
break;
case 9:cout<<"The list is:"<<endl;
Print(L);
case 0:break;
default:break;
}
}
return 0;
}