#include <iostream>
using namespace std;
#define Maxsize 100
struct SqList
{
int *elem;
int length;
} ;
int InitList(SqList &L)
{//构造一个空的顺序表
L.elem=new int[Maxsize];
if(!L.elem)
{
return false;
}
L.length=0;
return true;
}
int CreateList(SqList &L)
{
int a,i=0;
cin>>a;
while(a!=-1)
{
if(L.length==Maxsize)
{
cout<<"顺序表满了!";
return false;
}
L.elem[i++]=a;
L.length++;
cin>>a;
}
return true;
}
//顺序表取值
int GetElem(SqList L,int i,int &e)
{
if(i<0|| i>L.length)
{
return false;
}
e=L.elem[i-1];
}
//顺序表查找
int LocateElem(SqList L,int e)
{
for(int i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return 0;
}
//顺序表插入
int ListInsert(SqList &L,int i,int e)
{
if(i<1||i>L.length+1)
{
return false;
}
if(L.length==Maxsize)
{
return false;
}
for(int j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=e;
++L.length;
return true;
}
int ListDelete(SqList &L ,int i,int &e)
{
if(i<1||i>L.length)
{
return false;
}
for(int j=i;j<=L.length-1;j++)
{
L.elem[j-1]=L.elem[j];
}
L.length--;
return true;
}
int main()
{
SqList myL;
int i,e,x;
cout << "1. 查找\n";
cout << "2. 插入\n";
cout << "3. 删除\n";
int choose=-1;
while(choose!= 0)
{
cout<<("请选择:");
cin>>choose;
switch(choose)
{
case 1://查找
cout<<"请输入要查找的数x:"<<endl;
cin>>x;
if(LocateElem(myL,x)==-1)
cout<<"FA"<<endl;
else
cout<<"SU"<<endl;
break;
case 2://插入
cout<<"请输入要插入的位置和要插入的数据元素e:";
cin>>i>>e;
if(ListInsert(myL,i,e))
cout<<"成功!"<<endl;
else
cout<<"失败!"<<endl;
break;
case 3://删除
cout<<"请输入要删除的第i个位置:"<<endl;
cin>>i;
if((ListDelete(myL,i,e))!=-1)
cout<<"SU:"<<endl;
else
cout<<"FA"<<endl;
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
#define Maxsize 100
struct SqList
{
int *elem;
int length;
} ;
int InitList(SqList &L)
{//构造一个空的顺序表
L.elem=new int[Maxsize];
if(!L.elem)
{
return false;
}
L.length=0;
return true;
}
//顺序表查找
int LocateElem(SqList L,int e)
{
for(int i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return 0;
}
//顺序表插入
int ListInsert(SqList &L,int i,int e)
{
if(i<1||i>L.length+1)
{
return false;
}
if(L.length==Maxsize)
{
return false;
}
for(int j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=e;
++L.length;
return true;
}
//顺序表删除
int ListDelete(SqList &L ,int i,int &e)
{
if(i<1||i>L.length)
{
return false;
}
for(int j=i;j<=L.length-1;j++)
{
L.elem[j-1]=L.elem[j];
}
L.length--;
return true;
}
int main()
{
SqList myL;
int i,e,x;
cout << "1. 查找\n";
cout << "2. 插入\n";
cout << "3. 删除\n";
int choose=-1;
while(choose!= 0)
{
cout<<("请选择:");
cin>>choose;
switch(choose)
{
case 1://查找
cout<<"请输入要查找的数x:"<<endl;
cin>>x;
if(LocateElem(myL,x)==-1)
cout<<"FA"<<endl;
else
cout<<"SU"<<endl;
break;
case 2://插入
cout<<"请输入要插入的位置和要插入的数据元素e:";
cin>>i>>e;
if(ListInsert(myL,i,e))
cout<<"成功!"<<endl;
else
cout<<"失败!"<<endl;
break;
case 3://删除
cout<<"请输入要删除的第i个位置:"<<endl;
cin>>i;
if((ListDelete(myL,i,e))!=-1)
cout<<"SU:"<<endl;
else
cout<<"FA"<<endl;
break;
}
}
return 0;
}