数据结构---顺序表

来源:百度文库 编辑:神马文学网 时间:2024/05/02 02:01:37

#define Listsize 100
#include
typedef struct
{ char elem[Listsize];
  int length;}Sqlist;
  Sqlist L;

void add(int i,char b)
{int j;
 if(L.length==Listsize) printf("插入失败");
 for(j=L.length;j>=i+1;j--)
 L.elem[j+1]=L.elem[j];
 L.elem[i+1]=b;
 ++L.length;
 for(i=1;i<=L.length;i++)
 printf("%c",L.elem[i]);}

void search(char b)
{int j;
 for(j=1;j<=L.length;j++)
 if(L.elem[j]==b)
 {printf("%c is in the %d",b,j-1);
  break;}
 if(j>L.length) printf("%c is not in the list",b);}

void delet(int i)
{
 int j;
 for(j=i+2;j<=L.length;j++)
  L.elem[j-1]=L.elem[j];
  --L.length;
 for(j=1;j<=L.length;j++)
  printf("%c",L.elem[j]);            
}

void main()
{char x,e1,e2;
 int p1,p2,p3;
 int i;
 printf("please input the length of the list:");
 scanf("%d",&L.length);
 printf("please input %d elements:",L.length-1);
 for(i=1;i<=L.length;i++)
 scanf("%c",&L.elem[i]);
 printf("what do you want to do?a(add) s(search) or d(delete)");getchar( );
 scanf("%c",&x);
 if(x=='a')
 {printf("please input the new element and the position:");getchar( );
 scanf("%c,%d",&e1,&p1);
 add(p1,e1);}
 else if(x=='s')
 {printf("please input the element :");getchar( );
 scanf("%c",&e2);
 search(e2);}
 else if(x=='d')
 {printf("please input the position :");getchar( );
 scanf("%d",&p3);
 delet(p3);}
}