新手菜鸟 自己写的不知道哪里错了 运行有问题 求大神指导 谢谢
#include "stdio.h"#include "malloc.h"
#include "process.h"
#include "string.h"
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}List;
Status InitList(List L)
{
L.elem=(ElemType * ) malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length = 0;
L.listsize=LIST_INIT_SIZE;
printf("初始化一个线性表\n\n");
return OK;
}
Status ListInsert(List L,int i,ElemType e){
ElemType *q,*p,*newbase;
if (i<1||i>L.length+1) return ERROR;
if (L.length>=L.listsize){
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if (!newbase) return(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 OK;
}
Status ListDelete(List L,int i,ElemType e2){
ElemType *p,*q;
if ((i<1)||(i>L.length)) return ERROR;
p=&(L.elem[i-1]);
e2= *p;
q=(L.elem+L.length-1);
for (++p;p<=q;++p) *(p-1)=*p;
--L.length;
return OK;
}
void main()
{
List L;
ElemType e1,e2;
Status k;
int i,a,b,c,d,e,n,j;
InitList(L);
printf("初始化表元素个数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("输入所插入的数据:");
scanf("%d",&a);
ListInsert(L,i,a);
}
for(j=1;j<2;)
{
printf("请选择您要进行的操作:1:插入 2:删除 \n");
scanf("%d",&c);
if(c==1)
{
printf("请输入插入元素的位置及元素:");
scanf("%d %d",&d,&e);
ListInsert(L,d,e);
printf("线性表元素总数为%d\n",n+1);
n++;
for(i=1;i<=n;i++)
{printf("%d \n",L.elem[i-1]);}
}
if(c==2)
{
printf("输入删除第几个数:");
scanf("%d",&c);
if(c<1||c>L.length)
printf("输入错误!!!元素不存在\n");
k=ListDelete(L,c,e2);
if(k==ERROR)
printf("删除第%d个元素失败\n ",c);
else
printf("删除第%d个元素成功,其值为%d\n\n",c,e2);
}
}
}