
代码一:
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
#define MAXLEN 20
typedef struct
{
datatype data[MAXLEN];
int last;
}SeqList;
SeqList* CreatSeqList()
{
int x,i=0;
SeqList P;
P.last=0;
scanf(“%d”,&x);
while(x!=-1)
{
P.data[i]=x;
i++;
P.last++;
scanf(“%d”,&x);
}
return &P;
}
void ShowSeqList(SeqList* L)
{
int i;
for(i=0;i<L->last;i++)
printf(“%d “,L->data[i]);
}
int main()
{
SeqList *L;
L=CreatSeqList();
ShowSeqList(L);
return 1;
}
代码二:
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
#define MAXLEN 20
typedef struct
{
datatype data[MAXLEN];
int last;
}SeqList;
SeqList* CreatSeqList()
{
int x,i=0;
SeqList *p;
p->last=0;
while(1)
{
scanf(“%d”,&x);
if(x==-1) break;
p->data[i]=x;
i++;
p->last++;
}
return p;
}
void ShowSeqList(SeqList* L)
{
int i;
for(i=0;i<L->last;i++)
printf(“%d “,L->data[i]);
}
int main()
{
SeqList *L;
L=CreatSeqList();
ShowSeqList(L);
return 1;
}
上面这两个代码错哪里了呢?

解决方案
60
#include<stdio.h> #include<stdlib.h> typedef int datatype; #define MAXLEN 20 typedef struct { datatype data[MAXLEN]; int last; }SeqList; SeqList* CreatSeqList() { int x, i = 0; SeqList *P = (SeqList *)malloc(sizeof(SeqList)); P->last = 0; scanf("%d", &x); while (x != -1) { P->data[i] = x; i++; P->last++; scanf("%d", &x); } return P; } void ShowSeqList(SeqList* L) { int i; for (i = 0; i<L->last; i++) printf("%d ", L->data[i]); } int main() { SeqList *L; L = CreatSeqList(); ShowSeqList(L); return 1; }