#include "stdio.h"
#define list_init_size 100
#define listincreament 10
#define ElemType char
enum status{ok,fail};
typedef struct{
ElemType *elem;
int length;
int listsize;
}sqlist;
enum status initlist_sq(sqlist *L)
{
L->elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));
if(L->elem)
{
L->length=0;
L->listsize=list_init_size;
return(ok);
}
else return (fail);
}
enum status listinsert_sq(sqlist *L,int i,ElemType e)
{
char *newbase;
char *p,*q;
if(i<1||i>L->length+1) return fail;
else
{
if(L->length>=L->listsize)
{
newbase=(ElemType*)realloc(L->elem,(L->listsize+listincreament)*sizeof(ElemType));
if(!newbase) {return fail;goto loop0;}
L->elem=newbase;
L->listsize+=listincreament;
}
q=&L->elem[i-1];
for(p=&L->elem[L->length-1];p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return (ok);
}
loop0:printf("not successfull\n");
}
enum status listdelete_sq(sqlist *L,int i,ElemType *e)
{
char *p,*q;
if(i<1||i>(*L).length) return fail;
else
{
p=&L->elem[i-1];
q=&L->elem[L->length-1];
e=p;
for(++p;p<=q;++p) *(p-1)=*p;
--L->length;
return ok;
}
}
void main()
{
sqlist *L;char e,k;int i;
enum status m;
m=initlist_sq(L);
if(m!="ok")
{
printf("not successfull\n");
exit();
}
printf("please input the dataes:\n");
i=1;
while(1)
{
scanf("%s",&e);
getchar();
m=listinsert_sq(L,i,e);
i++;
if(m==fail){printf("not successfull\n");break;}
printf("please input stop or continue\n");
scanf("%s",&k);
if(k=="stop") break;
}
printf("you want insert the data y or n ?\n");
scanf("%c",&k);
if(k=='y')
while(1)
{
printf("data:e=");
scanf("%s",&e);
printf("\n");
printf("location:i=");
scanf("%d",&i);
printf("\n");
m=listinsert_sq(L,i,e);
if(m!=ok){printf("not successful\n");goto loop1;}
printf(" is successfull\n");
printf("you want to continue insert other data y or n?\n");
scanf("%c",&k);
if(k!='y') break;
}
loop1:printf(" you want to delete the data y or n?\n");
scanf("%c",&k);
if(k=='y')
loop2:
while(1)
{
printf("i=");
scanf("%d",&i);
m=listdelete_sq(L,i,&e);
if(m!='ok'){printf("not successful\n");goto loop2;}
printf("%s",e);
printf("is deleted\n");
printf("you want to continue todelete other data y or n?\n");
scanf("%c",&k);
if(k!='y') break;
}
printf("you want to display the data y or n\n");
scanf("%c",&k);
if(k=='y')
while(i<=L->length)
{
printf("%s",L->elem[i]);
}
else
printf("don't display\n");
}
不知为什么 不能运行 又没错 误了