问题补充:
#include
#include
#include
#define MAXSIZE 100
struct SeqList
{
int *elem;
int last;
int listsize;
};
void InitList(SeqList&L)
{
L.elem = (int *)malloc(MAXSIZE*sizeof(int));
if (!L.elem)
exit(0);
L.last = 0;
L.listsize = MAXSIZE;
}
int ListInsert(SeqList &L, int i, int e)
{
int *p, *q;
if (i<1 || i > L.last + 1)
{
printf("位置错误!\n");
return -1;
}
if (L.last > L.listsize)
{
printf("表满!\n");
return 0;
}
q = L.elem + i - 1;
for (p = L.elem + L.last - 1; p >= q; p--)
*(p + 1) = *p;
*q = e;
L.last++;
return 1;
}
int showlist(SeqList&L)
{
int i;
for (i = 0; i < L.last; i++)
{
printf("%d",L.elem[i]);
}
return 0;
}
int deletelist(SeqList&L, int i)
{
if (i < 1 || i>L.last + 1)
printf("位置错误");
return -1;
if (i > L.last)
printf("表满");
return 0;
int *p, *q;
q = L.elem + i - 1;
p = L.elem + L.last - 1;
for (q; q < p; q++)
{
*q = *(q+1);
L.last--;
}
return 1;
}
int main()
{
SeqList L;
InitList(L);
int a[4] = { 1,2,3,4 };
int i;
int e;
for (i = 0; i < 4; i++)
ListInsert(L, i + 1, a[i]);
printf("线性表为:\n");
showlist(L);
printf("\n");
printf("删除后的线性表为:\n");
deletelist(L,1);
showlist(L);
printf("\n");
return 0;
}
为什么输出的是两个一样的。。。删除函数算法不对吗