if (L.length > L.listsize)
{
if ((L.elem = (int*)realloc(L.elem, sizeof(int) *(L.listsize + INCREMENT))) == NULL)
{
exit(1);
}
L.listsize += INCREMENT;
这个地方可能还有点问题,但我想不起来了
应该是if (L.length >= L.length)
日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
# include <stdio.h>
# include <conio.h>
# include <stdlib.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);
void print(Sqlist L, int n);
int CreatLinklist(Sqlist &L)
{
if((L.elem = (int*)malloc(sizeof(int) * LIST_INIT_SIZE)) == NULL)
{
exit(1);
}
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return 1;
}
int InitList_Sq(Sqlist &L,int n)
{
int i;
printf("\nInput the value of the Sqlist as follows:\n");
for(i = 0; i < n; i++)
{
scanf("%d", &L.elem[i]);
L.length++;
}
return 1;
}
void print(Sqlist L, int n)
{
int i;
for (i = 0;i < n; i++)
{
printf("%d ", L.elem[i]);
}
}
int main(void)
{
int m;
Sqlist L;
CreatLinklist(L);
printf("Input the len of the Sqlist:\n");
scanf("%d", &m);
InitList_Sq(L,m);
print(L, m);
free(L.elem);
getch();
return 0;
}
改了下你的~~
[CODE]
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#include "alloc.h"
#define LIST_INIT_SIZE 100
#define INCREMENT 10
#define ERROR 0
#define DEBUG 0
typedef struct{
int *elem;
int length;
int listsize;
} Sqlist;
void CreatLinklist(Sqlist &L);
void InitList_Sq(Sqlist &L,int n);
void Print(Sqlist &L);
void 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;
}
void InitList_Sq(Sqlist &L,int n)
{
int i,*k;
if(n>LIST_INIT_SIZE) /*这样不正式,但这个程序足以*/
{
L.elem=(int *)realloc(L.elem,sizeof(int)*n);
L.listsize=n;
}
k=L.elem;
printf("\nInput the value of the Sqlist as follows:\n");
for(i=0;i<n;i++)
{
scanf("%d", k++);
L.length++;
}
}
void Print(Sqlist &L)
{
int *p;
p=L.elem;
while(p < L.elem+L.length)
printf("%d ", *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);
free(L.elem);
return 0;
}