如何删除元素?
#include "stdafx.h"#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
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("顺序表中的元素为: ");
for (i=1;i<=L.length;i++)
printf("%d",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值不合法\n");
else
PrintList_Sq(L);
return 0;
}