线性表如何进行删除操作?
#include "stdafx.h"#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define OK 1
#define Error -1
#define OVERFLOW 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct LNode
{
ElemType *elem;
ElemType *newbase;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
int i;
L.elem = (ElemType * )malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (! L.elem) exit (OVERFLOW);
for(i=1;i<=5;i++)
{
scanf("%d",&L.elem[i-1]);
}
L.length = 5;
L.listsize = LIST_INIT_SIZE;
return OK;
}
Status PrintList_Sq(SqList L)
{
int i;
printf("Linklist yuansu is:\n ");
for (i=1;i<=L.length;i++)
printf("%4d",L.elem[i-1]);
printf("\n");
return OK;
}
Status ListDelete_Sq(SqList &L, int i ,ElemType &e)//为什么不能删除元素?代码该如何修改?
{
ElemType *p,*q;
if ((i<1) || (i>L.length))
return Error;
p = & (L.elem[i-1]);
e = *p;
q= L.elem + L.length - 1;
for (++p;p<=q;++p)*(p-1)= *p;
--L.length;
return OK;
}
int main(int argc, char* argv[])
{
SqList L;
ElemType e;
int select,i;
InitList_Sq(L);
PrintList_Sq(L);
ListDelete_Sq(L,i,e);
printf("input i:\n");
scanf("%d",i);
printf("input i=");
scanf("%d",&i);
if (ListDelete_Sq(L,i,e)==Error)
printf("i is error!\n");
else
PrintList_Sq(L);
return 0;
}