2顺序表基本操作的实现(菜单的建立)复习过程

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
if(L.length==maxsize)
{
cout<<"满表";
return;
}
if(i<1||i>L.length+1)
{
cout<<"位置不合理";
return;
}
for(int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
i=Locateelem(L,e);
if(i==-1)
cout<<"元素"<<e<<"不在顺序表中\n";
else
cout<<"\n元素"<<e<<"在顺序表中第"<<i<<"位置\n";
cout<<"\n请输入插入的位置和元素:";
cin>>i>>e;
InsertList(L, i, e);
OutList(L);
break;
}
}
/*SqList L;
InitList(L);
CreateList(L);
OutList(L);
int i;
elemtype e;
cout<<"\n请输入查找的位置:";
cin>>i;
if(Getelem(L,i,e))
cout<<e;
cout<<"\n请输入查找的元素:";
cin>>e;
#define maxsize 100
typedef int elemtype;
typedef struct sqlist
{
elemtype *elem;//elem存放地址
int length;
}SqList;#include <iostream.h>
void InitList(SqList &L)
return 0;
}
e=L.elem[i-1];
return 1;
}
int Locateelem(SqList L,elemtype e)
{
for(int i=0;i<L.length;i++)
if(e==L.elem[i])
return i+1;
return -1;
}
void InsertList(SqList &L,int i,elemtype e)
cout<<"\n\t\t 5:顺序表删除";
cout<<"\n\t\t 6:顺序表输出";
cout<<"\n\t\t 0:退出";
int flag;
while(flag=1)
{
int choose;
cin>>choose;
switch(choose)
{
case 1:CreateList(L);
break;
{
L.elem=new elemtype[maxsize];
if(L.elem==NULL)
{
cout<<"空间分配失败";
return;
}Leabharlann Baidu
L.length=0;
}
void CreateList(SqList &L)
{
cout<<"请输入表长";
cin>>L.length;
for(int i=0;i<L.length;i++)
case 2:cout<<"\n请输入查找的位置:";
cin>>i;
if(Getelem(L,i,e))
cout<<e;
break;
case 3: cout<<"\n请输入查找的元素:";
cin>>e;
i=Locateelem(L,e);
if(i==-1)
cout<<"元素"<<e<<"不在顺序表中\n";
--L.length;
}
void main()
{
SqList L;
InitList(L);
int i;
elemtype e;
cout<<"\n\t\t 1:顺序表建立";
cout<<"\n\t\t 2:顺序表查找(位置)";
cout<<"\n\t\t 3:顺序表查找(值)";
cout<<"\n\t\t 4:顺序表插入";
DeleteList(L, i);*/
}
else
cout<<"\n元素"<<e<<"在顺序表中第"<<i<<"位置\n";
break;
case 4: cout<<"\n请输入插入的位置和元素:";
cin>>i>>e;
InsertList(L, i, e);
break;
case 5:if(L.length==0)
cout<<"空表";return;
if(i<1||i>L.length)
cout<<"位置不合理";return;
for(intj=i;j<L.length;j++)
L.elem[j-1]=L.elem[j];
L.length--;
DeleteList(L, i);
break;
case 6:OutList(L);
break;
case 0:flag=0;
++L.length;
}
void DeleteList(SqList &L,int i)
{
if(L.length==0)
{
cout<<"空表";
return;
}
if(i<1||i>L.length)
{
cout<<"位置不合理";
return;
}
for(int j=i;j<L.length;j++)
L.elem[j-1]=L.elem[j];
cin>>L.elem[i];
}
void OutList(SqList L)
{
for(int i=0;i<L.length;i++)
cout<<L.elem[i]<<"\t";
}
int Getelem(SqList L,int i,elemtype &e)
{
if(i<1||i>L.length)
{
cout<<"位置不合理";
相关文档
最新文档