删除顺序表元素。请在程序中建立一个含有10个元素,分别是1,2,3,4,5,6,7,8,9,10的顺序表。然后等候用户输入要删除的元素位序,如删除成功,输出顺序表的内容,否则输出删除失败。
#include<iostream>
#define MAXSIZE 100
#define ERROR 0
using namespace std;
typedef struct
{
int *elem;
int length;
}SqList;
int InitList(SqList &L)
{
L.elem=new int[MAXSIZE];
if(!L.elem) exit(-2);
L.length=1;
return 1;
}
void shuru(SqList &L)
{
for(int i=1;i<11;i++)
{
L.elem[i]=i;
L.length++;
}
}
void search(SqList &L)
{
int a,j;
cin>>a;
for(int j=1;j<11;j++)
{
if(a==L.elem[j])
{
cout<<a<<"是第"<<a<<"个数";
break;
}
if(j==10)
cout<<"查找失败";
}
}
int charu(SqList &L,int a,int e)
{
if(a<1||a>L.length+1)
cout<<"插入失败";
if(L.length==MAXSIZE)
cout<<"插入失败";
for(int j=L.length-1;j>=a-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[a]=e;
++L.length;
return 1;
}
int shanchu(SqList &L,int a)
{
a=a+1;
if(a<1||a>L.length)
cout<<"删除失败";
int e;
e=L.elem[a-1];
for(int j=a;j<=L.length;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return 1;
}
int main()
{
SqList L;
int a,b;
InitList(L);
shuru(L);
//cin>>a>>b;
//charu(L,a,b);
cin>>a;
shanchu(L,a);
for(int i=1;i<L.length;i++)
{
cout<<" "<<L.elem[i];
}
// search(L);
}