主题:新手求助:链表输出无法实现
# include <stdio.h>
# include <malloc.h>
typedef struct LNode{
char name[20];
struct LNode *next;
}linklist;
linklist *create(int n){///创建大小为n的链表
linklist *h,*p,*s;
int tmp;
if((h=(linklist *)malloc(sizeof(linklist)))==NULL) ///创立头结点
exit(0);
h->name[0]='\0';
h->next=NULL;
p=h;
for(tmp=0;tmp<n;tmp++){
if((s=(linklist *)malloc(sizeof(linklist)))==NULL) ////新增结点
exit(0);
p=s;
printf("input the name:");
scanf("%s",s->name);
s->next=NULL;
p=s;
}
return(h);
}
void print(linklist *h)
{
linklist *p;
p=h->next;
printf("the data are:");
while(p!=NULL)
{
printf("%s ",p->name);
p=p->next;
}
}
main(){
linklist *head;
int n;
printf("first create the list,input n:");
scanf("%d",&n);
head=create(n);
print(head);
}
以上是我写的关于链表创建和输出的程序,为什么无法实现输出?
# include <malloc.h>
typedef struct LNode{
char name[20];
struct LNode *next;
}linklist;
linklist *create(int n){///创建大小为n的链表
linklist *h,*p,*s;
int tmp;
if((h=(linklist *)malloc(sizeof(linklist)))==NULL) ///创立头结点
exit(0);
h->name[0]='\0';
h->next=NULL;
p=h;
for(tmp=0;tmp<n;tmp++){
if((s=(linklist *)malloc(sizeof(linklist)))==NULL) ////新增结点
exit(0);
p=s;
printf("input the name:");
scanf("%s",s->name);
s->next=NULL;
p=s;
}
return(h);
}
void print(linklist *h)
{
linklist *p;
p=h->next;
printf("the data are:");
while(p!=NULL)
{
printf("%s ",p->name);
p=p->next;
}
}
main(){
linklist *head;
int n;
printf("first create the list,input n:");
scanf("%d",&n);
head=create(n);
print(head);
}
以上是我写的关于链表创建和输出的程序,为什么无法实现输出?