线性表的问题
请帮我看看,哪里出错呢. 程序可以运行出来.但最后会弹出对话框,提示出错.先谢谢了..
#include<iostream.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#define Status int
#define ElemType int
#define OK 1
#define ERROR -1
#define OVERFLOW -1
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
typedef struct
{
ElemType *elem;
int Length;
int ListSize;
}SqList;
Status InitList(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 OK;
}
Status List_Insert(SqList &L,int i,ElemType e)
{
if(i<1||i>L.Length+1) return ERROR; ///////为什么要加1
ElemType *newbase;
ElemType *q,*p;
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;
//cout<<*q<<endl;
L.Length++;
//cout<<L.Length<<"qqqqqqqqqqq"<<endl;
return OK;
}
Status ListDelete(SqList &L,int i,ElemType &e)
{
if((i<1)||(i>L.Length)) return ERROR;
ElemType *q,*p;
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;
}
void main()
{
SqList L;
InitList(L);
for(int i=1;i<=20;i++)
List_Insert(L,1,i);
//cout<<L.Length<<"asdafa"<<endl;
for(int j=1;j<5;j++)
cout<<L.elem[j-1]<<endl;
//ElemType *t;
//for(t=L.elem;t<=&(L.elem[L.Length-1]);t++) cout<<*(t)<<endl;
}