69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 5
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
} SqList;
void InitList(SqList **L)
{
(*L)->elem = (ElemType *)malloc(sizeof(ElemType));
if(!(*L)->elem)
return;
else
{
(*L)->length = 0;
(*L)->listsize = LIST_INIT_SIZE;
}
}
void CreateList(SqList *L)
{
ElemType val;
int i;
val = rand() % 100;
for(i = 0; i < 30; i++)
{
if(L->length >= L->listsize)
{
L->elem = (ElemType *)realloc(L->elem, L->listsize + LISTINCREMENT * sizeof(ElemType));
if(!L->elem)
return;
else
{
L->listsize += 1;
}
}
else
{
L->elem[L->length] = val;
L->length++;
}
val = rand() % 100;
}
}
int main(void)
{
SqList *list;
list = (SqList *)malloc(sizeof(SqList));
InitList(&list);
CreateList(list);
return 0;
}
if(L->length >= L->listsize)
{
L->elem = (ElemType *)realloc(L->elem, L->listsize + LISTINCREMENT * sizeof(ElemType));
if(!L->elem)
return;
else
{
L->listsize += LISTINCREMENT ;
}
}