#include <stdio.h>
#define MAXLEN 20
typedef int elemtype;
typedef struct
{
elemtype List[MAXLEN];
int Num;
}Seqlist;
int insert(Seqlist *la,int i,elemtype x);
int del(Seqlist *la,int i);
void output(Seqlist *la);
void sort_insert(Seqlist *la,int t);
void k_del(Seqlist *la,int i,int k);
void x_del(Seqlist *la,elemtype x);
main()
{
Seqlist la;
int m,i,ki,kk;
elemtype x,t;
la.Num=-1;
printf("\nPlease input data:");
for(i=0;i<=9;i++)
{
++la.Num;
scanf(" %d",&la.List[i]);
}
printf("Please input the value of m:m=1---insert m=2---delete\n"
"m=3---sort_delete m=4---k_del m=5---x_del\nm=");
scanf("%d",&m);
if(m==1)
{
printf("Please input insert location and insert data:");
scanf("%d%d",&i,&x);
insert(&la,i,x);
output(&la);
}
else if(m==2)
{
printf("Please input delete location:");
scanf("%d",&i);
del(&la,i);
output(&la);
}
else if(m==3)
{
printf("input insert number:");
scanf("%d",&t);
sort_insert(&la,t);
output(&la);
}
else if(m==4)
{
printf("input delete location and how many:");
scanf("%d%d",&ki,&kk);
k_del(&la,ki,kk);
output(&la);
}
else if(m==5)
{
printf("input delete number:");
scanf("%d",&x);
x_del(&la,x);
output(&la);
}
else
printf("The value of m is wrong!");
}
int insert(Seqlist *la,int i,elemtype x)
{
int j;
if (i<0||i>la->Num)
{
printf ("\n 插入位置不合理!");
return 0;
}
else if(la->Num==MAXLEN-1)
{
printf ("\n 表满,不能插入!");
return 0;
}
else
{
for(j=la->Num;j>=i-1;j--)
la->List[j+1]=la->List[j];
la->List[i-1]=x;
la->Num++;
return 1;
}
}
int del(Seqlist *la,int i)
{
int j;
if(i<0||i>la->Num)
{
printf("\n the position is invalid");
return 0;
}
else
{
for (j=i; j<=la->Num; j++)
la->List[j-1]=la->List[j];
la->Num--;
return 1;
}
}
void output(Seqlist *la)
{
int m;
printf("当前的线性表为");
for(m=0;m<=la->Num;m++)
printf(" %d",la->List[m]);
printf("\n表长为%d\n",la->Num+1);
}
void sort_insert(Seqlist *la,elemtype t)
{
int i,j;
for(i=0;i<=la->Num;i++)
if(la->List[i]>t)
break;
for(j=la->Num;j>=i;j--)
la->List[j+1]=la->List[j];
la->List[i]=t;
la->Num++;
}
void k_del(Seqlist *la,int i,int k)
{
int j;
if(i<0 || i>la->Num)
printf("\nthe position is invalid");
else
{
for(j=i;j<=la->Num;j++)
la->List[j-1]=la->List[j+k-1];
la->Num-=k;
}
}
void x_del(Seqlist *la,elemtype x)
{
int i,j,flag=0;
for(i=0;i<=la->Num;i++)
if(la->List[i]==x)
{
flag++;
for(j=i;j<=la->Num;j++)
la->List[j]=la->List[j+1];
}
la->Num-=flag;
}
#define MAXLEN 20
typedef int elemtype;
typedef struct
{
elemtype List[MAXLEN];
int Num;
}Seqlist;
int insert(Seqlist *la,int i,elemtype x);
int del(Seqlist *la,int i);
void output(Seqlist *la);
void sort_insert(Seqlist *la,int t);
void k_del(Seqlist *la,int i,int k);
void x_del(Seqlist *la,elemtype x);
main()
{
Seqlist la;
int m,i,ki,kk;
elemtype x,t;
la.Num=-1;
printf("\nPlease input data:");
for(i=0;i<=9;i++)
{
++la.Num;
scanf(" %d",&la.List[i]);
}
printf("Please input the value of m:m=1---insert m=2---delete\n"
"m=3---sort_delete m=4---k_del m=5---x_del\nm=");
scanf("%d",&m);
if(m==1)
{
printf("Please input insert location and insert data:");
scanf("%d%d",&i,&x);
insert(&la,i,x);
output(&la);
}
else if(m==2)
{
printf("Please input delete location:");
scanf("%d",&i);
del(&la,i);
output(&la);
}
else if(m==3)
{
printf("input insert number:");
scanf("%d",&t);
sort_insert(&la,t);
output(&la);
}
else if(m==4)
{
printf("input delete location and how many:");
scanf("%d%d",&ki,&kk);
k_del(&la,ki,kk);
output(&la);
}
else if(m==5)
{
printf("input delete number:");
scanf("%d",&x);
x_del(&la,x);
output(&la);
}
else
printf("The value of m is wrong!");
}
int insert(Seqlist *la,int i,elemtype x)
{
int j;
if (i<0||i>la->Num)
{
printf ("\n 插入位置不合理!");
return 0;
}
else if(la->Num==MAXLEN-1)
{
printf ("\n 表满,不能插入!");
return 0;
}
else
{
for(j=la->Num;j>=i-1;j--)
la->List[j+1]=la->List[j];
la->List[i-1]=x;
la->Num++;
return 1;
}
}
int del(Seqlist *la,int i)
{
int j;
if(i<0||i>la->Num)
{
printf("\n the position is invalid");
return 0;
}
else
{
for (j=i; j<=la->Num; j++)
la->List[j-1]=la->List[j];
la->Num--;
return 1;
}
}
void output(Seqlist *la)
{
int m;
printf("当前的线性表为");
for(m=0;m<=la->Num;m++)
printf(" %d",la->List[m]);
printf("\n表长为%d\n",la->Num+1);
}
void sort_insert(Seqlist *la,elemtype t)
{
int i,j;
for(i=0;i<=la->Num;i++)
if(la->List[i]>t)
break;
for(j=la->Num;j>=i;j--)
la->List[j+1]=la->List[j];
la->List[i]=t;
la->Num++;
}
void k_del(Seqlist *la,int i,int k)
{
int j;
if(i<0 || i>la->Num)
printf("\nthe position is invalid");
else
{
for(j=i;j<=la->Num;j++)
la->List[j-1]=la->List[j+k-1];
la->Num-=k;
}
}
void x_del(Seqlist *la,elemtype x)
{
int i,j,flag=0;
for(i=0;i<=la->Num;i++)
if(la->List[i]==x)
{
flag++;
for(j=i;j<=la->Num;j++)
la->List[j]=la->List[j+1];
}
la->Num-=flag;
}