#include<bits/stdc++.h>
using namespace std;
const int n=101;
struct sqlist{
int * elem;
int length;
int listsize;
};
void initlist_sq(sqlist &l)
{
l.elem=new int[n];
if(!l.elem) {cout<<"分配内存失败";exit(0);}
l.length = 0;
l.listsize=n;
}
void fuzhi(sqlist &l)
{
int a;
cout<<"输入要插入的个数";
cin>>a;
for(int i=0;i<a;i++)
{
cin>>l.elem[i];
++l.length;
}
return ;
}
void destroylist(sqlist &l)
{
if(l.elem)
delete []l.elem;
}
void clearlist(sqlist &l)
{
l.length=0;
}
int getlength(sqlist l)
{
return l.length;
}
int isempty(sqlist l)
{
if(l.length==0)
return 1;
else
return 0;
}
int getelem(sqlist l,int i,int &e)
{
if(i<1||i>l.length)
{cout<<"没有这个元素";
return 0;
}
e=l.elem[i-1];
return 1;
}
int findelem(sqlist l,int e)
{
for(int i=0;i<l.length;i++)
if(l.elem[i]==e)
return i+1;
return 0;
}
void listinsert_sq(sqlist &l,int i,int e)
{
if(i<1||i>l.length+1)
{
cout<<"插入操作错误";
return ;
}
if(l.length==n)
{
cout<<"插入操作错误";
return ;
}
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;
}
void listdelete_sq(sqlist &l,int i)
{
if(i<1||i>l.length)
{
cout<<"删除操作错误";
return ;
}
for(int j=i;j<=l.length-1;j++)
{
l.elem[j-1]=l.elem[j];
}
--l.length;
cout<<"删除成功"<<endl;
return;
}
void jm()
{
cout<<"1、初始化"<<endl<<"2、赋值"<<endl<<"3、插入"<<endl<<"4、查找"<<endl<<"5、删除"<<endl<<"6、总览"<<endl<<"7、退出"<<endl;
}
void bianli(sqlist l)
{
for(int i=0;i<l.length;i++)
{
cout<<l.elem[i];
}
cout<<endl;
return;
}
int main()
{
sqlist z;
jm();
while(1)
{
int m;
cin>>m;
switch(m)
{
case 1:
initlist_sq(z);
break;
case 2:
fuzhi(z);
break;
case 3:
{ int i;
cout<<"请输入插入的位置";
cin>>i;
int e;
cout<<"请输入插入的数字";
cin>>e;
listinsert_sq(z,i,e);
break;
}
case 4:{ int e;
cout<<"请输入查找的数字";
cin>>e;
if(findelem(z,e))
cout<<"位置在"<<findelem(z,e)<<endl;
else
cout<<"找不见"<<endl;
break;
}
case 5:
{ int i;
cout<<"请输入删除的位置";
cin>>i;
listdelete_sq(z,i);
break; }
case 6:
bianli(z);
break;
case 7:
exit(0);
}
}
return 0;}
数据结构----1、线性表详细代码——简单易懂,菜鸟编写
猜你喜欢
转载自blog.csdn.net/qq_44162236/article/details/102664268
今日推荐
周排行