请帮我看一下这个单链表的插入与删除问题,有一处错误。
谢谢
#include<stdio.h>
#include<stdlib.h>
#define list_init_size 100
#define listincrement 10
#define overflow -2
typedef int status;
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
} sqlist;
status initlist_sq(sqlist &L)
{
L.elem=(elemtype *)malloc(list_init_size * sizeof(elemtype));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_init_size;
return 1;
}
status listinsert_sq(sqlist &L, int i ,elemtype e)
{
int * q , *p ,* newbase;
if(i<1 || i>L.length + 1) return 0;
if(L.length >= L.listsize)
{
newbase=(elemtype *)realloc(L.elem,(L.listsize+listincrement) * sizeof(elemtype));
if(!newbase) exit (overflow);
L.elem=newbase;
L.listsize+=listincrement;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]) ;p>=q ;--p)
*(p+1) = *p;
*q = e;
++L.length;
return 1;
}
status listdelete_sq(sqlist &L, int i, elemtype &e)
{
int *p ,*q ;
if((i<1) || (i>L.length)) return 0;
p=&(L.elem[i-1]);
e=* p;
q=L.elem + L.length - 1;
for(++p ;p<=q ;++p)
*(p-1)=*p;
--L.length;
return 1;
}
status locateelem_sq(sqlist &L, elemtype e)
{
for(int pass=1 ;pass<=L.length ;pass++)
{
if(L.elem[pass-1]==e)
return pass;
}
return 0;
}
int main()
{
int i, e, swich_sq, pa;
sqlist L;
initlist_sq(L);
printf("请设置字符的长度:\n");
scanf("%d",&L.length);
if(L.length<=0 || L.length>list_init_size)
{
printf("错误的输入!\n");
return 0;
}
for( pa=1;pa<=L.length; pa++)
{
printf("请输入数组第%d个数!\n",pa);
scanf("%d",&L.elem[pa-1]);
}
do{
printf("\n\n当前的数是: ");
for (pa=0;pa<L.length;pa++)
{
printf("%d ",L.elem[pa]);
}
printf("\n请输入要执行的操作:\n 1,插入一个数\n 2,删除一个数\n 3,查找一个数\n 0,退出程序\n");
scanf("%d",&swich_sq);
switch(swich_sq)
{
case 1:
printf("请输入要插入数的位置:\n");
scanf("%d",&i);
if (i>=1 && i<=L.length)
{
printf("请输入要插入的数的值:\n");
scanf("%d",&e);
listinsert_sq(L,i,e);
}
else printf("\n\n\n输入不正确!\n");
break;
case 2:
printf("请输入要删除的数的序号:\n");
scanf("%d",&i);
if (i>=1 && i<=L.length)
{
listdelete_sq(L,i,e);
printf("你删除的数是L.elem[%d] = %d",i-1,e);
}
else printf("\n\n\n你的输入不正确!\n");
break;
case 3:
printf("请输入要查找的数:\n");
scanf("%d",&e);
if (locateelem_sq(L,e)!=0)
{
printf("你查找的数在结构中,并且是第%d个数!\n",locateelem_sq(L,e));
}
else printf("\n\n\n你输入的数不在结构中!\n");
break;
}
}
while (swich_sq != 0);
free (L.elem);
}