郁闷啊我实现顺序表的操作问题
但结果不对啊
不知道怎么了
请帮下忙吧
先谢谢了啊
# include"stdio.h"
# include"stdlib.h"
# include"malloc.h"
#define LIST_INIT_SIZE 100
#define INCREMENT 10
#define ERROR 0
#define DEBUG 0
typedef struct{
int *elem;
int length;
int listsize;
}Sqlist;
int CreatLinklist(Sqlist &L);
int InitList_Sq(Sqlist &L,int n);
int ListInsert_Sq(Sqlist &L,int i,int e);
int ListDele_Sq(Sqlist &L,int i,int &e);
void print(Sqlist &L);
int CreatLinklist(Sqlist &L)
{
L.elem=(int*)malloc(sizeof(int)*LIST_INIT_SIZE);
if(!L.elem)exit(1);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return 1;
}
int InitList_Sq(Sqlist &L,int n)
{
int i,*k;
k=L.elem;
printf("\nInput the value of the Sqlist as follows:\n");
for(i=0;i<n;i++)
{
scanf("%d", k++);
L.length++;
}
k=0;
return 1;
}
/*
#if DEBUG调试加的
int ListInsert_Sq(Sqlist &L,int i,int e)
{
int *q,*p,*newbase;
if((i<1)||(i>L.length+1)) return ERROR;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+INCREMENT)*sizeof(int));
if(!L.elem)exit(1);
L.elem=newbase;
L.listsize+=INCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return 1;
}
int ListDele_Sq(Sqlist &L,int i,int &e)
{
int *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 1;
}
#endif*/
void print(Sqlist &L)
{
int *p;
p=L.elem;
while(p) printf("%2d", *p++);
}
int main()
{
int m, a1, a2, b;
Sqlist L;
int e;
CreatLinklist(L);
printf("Input the len of the Sqlist:\n");
scanf("%d", &m);
InitList_Sq(L,m);
print(L);
/*这是调试时加的
#if DEBUG
printf("Input the Inserted position and the Inserted value:\n");
scanf("%d%d", &a1, &b);
ListInsert_Sq(L, a1, b);
print(L);
printf("Input the deleted position :\n");
scanf("%d", &a2);
ListDele_Sq(L, a2,e);
print(L);
#endif*/
free(L.elem);
return 0;
}
出再的问题结果为一行又一行莫名其妙的数不知道怎么回事,我用的是VC++编绎器,还用了引用参数
去了我写的#if与#endif之间的很有问题,
[此贴子已经被作者于2006-5-10 22:37:59编辑过]