70,020
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>//这里我调整了位置,不知道你的行不行,我看着舒服这样
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int *elem;
int length;
int listsize;
}sqlist;
int Initlist(sqlist *L)//这里改成指针
{
L->elem = (int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L->elem) exit(-2);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return 1;
}
int Insert(sqlist *L,int i,int e)//这里改成指针
{
if(i<1||i>L->length + 1)return 0;
if(L->length>=L->listsize)
{
int * newbase = (int *)realloc(L->elem,(L->listsize + LISTINCREMENT)*sizeof(int));
if(!newbase) exit(-2);
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
int *q = &(L->elem[i-1]);
int *p = &(L->elem[L->length - 1]);
for(; p>=q;--p) *(p+1) = *p;
*q = e;
L->length ++;
return 1;
}
int Delete(sqlist *L,int i)//这里改成指针
{
if(i<1||i>L->length) return 0;
int *q = &(L->elem[i-1]);
int *p = &L->elem[L->length - 1];
for(;q != p;++q) *q = *(q+1);
--L->length;
return 1;
}
void Print(sqlist *L)//这里改成指针
{
int i=0;
int *p = L->elem;
int *q = L->elem + L->length;//这里改了,所以最后的一句输出不需要了
while(p != q)
{
printf("%d",L->elem[i++]);
p++;//这里添加了一句
}
}
int main()
{
sqlist *L;
L = (sqlist *)malloc(sizeof(sqlist));
Initlist(L);
int i = 0,j = 1;
while(scanf("%d",&i) != EOF) Insert(L,j++,i);
Print(L);
free(L->elem);//建议楼主这个地方在写一个destory函数用来释放Initlist开辟的空间。这里用free有点四不象。
free(L);
return 0;
}