#include
#include
typedef struct LNode{
struct LNode *next;
int data;
}LNode,*LinkList;
void creatList(LinkList L,int n){
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
LinkList end=NULL;
end=L;
LinkList node=NULL;
int i=0;
while(imalloc(sizeof(LNode));
scanf("%d",&(node->data));
end->next=node;
end=node;
i++;
}
end->next=NULL;
}
void PrintList(LinkList L){
LinkList p;
p=L->next;
while(p){
printf("%d ",p->data);
p=p->next;
}
}
int main(){
LNode La;
int n1;
scanf("%d",&n1);
creatList(&La,n1);
PrintList(&La);
return 0;
}
把第10行去掉就可以了。因为头结点La已经在main函数创建了,只需要在它的next节点新增节点与数据即可,如果加上第10行,则在creatList()函数里增加的数据是第10行新创建的L节点它的数据,而不是main方法里的La头结点的数据。修改如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
struct LNode *next;
int data;
}LNode,*LinkList;
void creatList(LinkList L,int n){
// L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
LinkList end=NULL;
end=L;
LinkList node=NULL;
int i=0;
while(i<n){
node=(LinkList)malloc(sizeof(LNode));
scanf("%d",&(node->data));
end->next=node;
end=node;
i++;
}
end->next=NULL;
}
void PrintList(LinkList L){
LinkList p;
p=L->next;
while(p){
printf("%d ",p->data);
p=p->next;
}
}
int main(){
LNode La;
int n1;
scanf("%d",&n1);
creatList(&La,n1);
PrintList(&La);
return 0;
}