求各位大侠帮小弟看看,顺序表有点错误
为什么我输入数字后,什么都没反应呢?
#include "stdlib.h "
#include "stdio.h "
#define true 1
#define false 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef struct SqList{
ElemType *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L)
{
L.elem=(ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
int ListInsert_Sq(SqList &L,int i,ElemType e)
{
if(i <=0||i> L.length+1) return ERROR;
if(L.length> =L.listsize)
{
L.elem=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.listsize+=LISTINCREMENT;
}
int *p=&L.elem[i];
for(int *q=&L.elem[L.length-1];q> =p;q--)
*(q+1)=*q;
*p=e;
L.length+=1;
return OK;
}
int CreateList_Sq(SqList &L,int n)
{
if(n <=0||n> L.listsize) return ERROR;
ElemType e;
for(int i=0;i <n;i++)
{
scanf( "%d ",&e);
L.elem[i]=e;
}
return OK;
}
int GetElem_Sq(SqList L,int i,ElemType &e)
{
if(i <=0||i> L.length) return ERROR;
e=L.elem[i-1];
return OK;
}
int OutputList_Sq(SqList L)
{
ElemType e;
for(int i=0;i <L.length;i++)
{
GetElem_Sq(L,i+1,e);
printf( "%5d ",e);
}
return OK;
}
int DestroyList_Sq(SqList &L)
{
if(!L.elem) free(L.elem);
return OK;
}
void main()
{
SqList L;
InitList_Sq(L);
CreateList_Sq(L,3);
OutputList_Sq(L);
DestroyList_Sq(L);
}
[解决办法]
CreateList_Sq函数里少了个L.length++;
int CreateList_Sq(SqList &L,int n)
{
if(n <=0||n> L.listsize) return ERROR;
ElemType e;
for(int i=0;i <n;i++)
{
scanf( "%d ",&e);
L.elem[i]=e;
L.length++;
}
return OK;
}