C语言链表问题 输出只出现第一个节点 没后面的 求大神指教
#include<stdio.h>#include<stdlib.h>structstudent{intnum;charname[20];intscore;structs...
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
};
int n=0;
struct student *creat(void) //建立链表的函数 返回首地址、、//
{
struct student *q,*w;
struct student *head;
q=w=(struct student *)malloc(sizeof(struct student));
head=NULL;
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
while(q->num!=0)
{
n=n+1;
if(n==1)
head=q;
else w->next=NULL;
w=q;
q=(struct student *)malloc(sizeof(struct student));
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
}
w->next=NULL;
return head;
}
int main()
{ void print(struct student *head);
struct student *head;
head=creat();
print(head);
return 0;
}
void print(struct student *head) //输出节点函数//
{
struct student *p;
p=head;
printf("The %d record is \n",n);
if(head!=NULL)
do
{
printf("%d %s %d",p->num,p->name,p->score);
p=p->next;
}while(p!=NULL);
}
//////// 为什么 只输出了第一个节点 求大神解答///// 展开
#include<stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
};
int n=0;
struct student *creat(void) //建立链表的函数 返回首地址、、//
{
struct student *q,*w;
struct student *head;
q=w=(struct student *)malloc(sizeof(struct student));
head=NULL;
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
while(q->num!=0)
{
n=n+1;
if(n==1)
head=q;
else w->next=NULL;
w=q;
q=(struct student *)malloc(sizeof(struct student));
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
}
w->next=NULL;
return head;
}
int main()
{ void print(struct student *head);
struct student *head;
head=creat();
print(head);
return 0;
}
void print(struct student *head) //输出节点函数//
{
struct student *p;
p=head;
printf("The %d record is \n",n);
if(head!=NULL)
do
{
printf("%d %s %d",p->num,p->name,p->score);
p=p->next;
}while(p!=NULL);
}
//////// 为什么 只输出了第一个节点 求大神解答///// 展开
展开全部
你第一个节点之后没有指向后面的节点,你丢失了后面所有节点,我改了之后是这样的
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
};
int n=0;
struct student *creat(void) //建立链表的函数 返回首地址、、//
{
struct student *q,*w;
struct student *head;
q=w=(struct student *)malloc(sizeof(struct student));
head=NULL;
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
while(q->num!=0)
{
n=n+1;
if(n==1)
{
head=q;
head->next = NULL;
}
else
{
w->next=q;
q->next = NULL;
}
w=q;
q=(struct student *)malloc(sizeof(struct student));
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
}
w->next=NULL;
return head;
}
int main()
{ void print(struct student *head);
struct student *head;
head=creat();
print(head);
return 0;
}
void print(struct student *head) //输出节点函数//
{
struct student *p;
p=head;
printf("The %d record is \n",n);
if(head!=NULL)
do
{
printf("%d %s %d",p->num,p->name,p->score);
p=p->next;
}while(p!=NULL);
}
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
};
int n=0;
struct student *creat(void) //建立链表的函数 返回首地址、、//
{
struct student *q,*w;
struct student *head;
q=w=(struct student *)malloc(sizeof(struct student));
head=NULL;
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
while(q->num!=0)
{
n=n+1;
if(n==1)
{
head=q;
head->next = NULL;
}
else
{
w->next=q;
q->next = NULL;
}
w=q;
q=(struct student *)malloc(sizeof(struct student));
scanf("%d",&q->num);
scanf("%s",&q->name);
scanf("%d",&q->score);
}
w->next=NULL;
return head;
}
int main()
{ void print(struct student *head);
struct student *head;
head=creat();
print(head);
return 0;
}
void print(struct student *head) //输出节点函数//
{
struct student *p;
p=head;
printf("The %d record is \n",n);
if(head!=NULL)
do
{
printf("%d %s %d",p->num,p->name,p->score);
p=p->next;
}while(p!=NULL);
}
追问
多谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询