c语言问题?
假如我输入两组数据为什么只输出一组数据呢?小白疑问#include<stdio.h>#include<stdlib.h>intget(structstu**head);v...
假如我输入两组数据为什么只输出一组数据呢?小白疑问
#include<stdio.h>
#include<stdlib.h>
int get(struct stu **head);
void input(struct stu *infor);
void output(struct stu *head);
struct stu
{
char Name[15];
float Maths, English, C, Total;
struct stu *next;
};
int main()
{
struct stu *head = NULL;
int num, i = 1;
printf("请输入人数:");
scanf("%d", &num);
while((i )<=num)
{
get(&head);
}
output(head);
return 0;
}
int get(struct stu **head)
{
struct stu *infor;
struct stu *temp;
infor = (struct stu *)malloc(sizeof(struct stu));
if(infor==NULL)
{
printf("内存分配失败。");
exit(1);
}
input(infor);
if(*head==NULL)
{
*head = infor;
(*head)->next=NULL;
}else
{
temp = (*head)->next;
while(temp!=NULL)
{
temp = temp->next;
}
temp = infor;
temp->next = NULL;
}
return 0;
}
void input(struct stu *infor)
{
printf("请输入学生姓名:");
scanf("%s", infor->Name);
printf("请输入数学成绩:");
scanf("%f", &(infor->Maths));
printf("请输入英语成绩:");
scanf("%f", &(infor->English));
printf("请输入C语言成绩:");
scanf("%f", &(infor->C));
infor->Total = (infor->Maths) (infor->English) (infor->C);
}
void output(struct stu *head)
{
struct stu *a;
a = head;
do
{
printf("%s", a->Name);
printf("%.1f", a->Maths);
printf("%.1f", a->English);
printf("%.1f", a->C);
printf("%.1f", a->Total);
a = a->next;
}while(a!=NULL);
} 展开
#include<stdio.h>
#include<stdlib.h>
int get(struct stu **head);
void input(struct stu *infor);
void output(struct stu *head);
struct stu
{
char Name[15];
float Maths, English, C, Total;
struct stu *next;
};
int main()
{
struct stu *head = NULL;
int num, i = 1;
printf("请输入人数:");
scanf("%d", &num);
while((i )<=num)
{
get(&head);
}
output(head);
return 0;
}
int get(struct stu **head)
{
struct stu *infor;
struct stu *temp;
infor = (struct stu *)malloc(sizeof(struct stu));
if(infor==NULL)
{
printf("内存分配失败。");
exit(1);
}
input(infor);
if(*head==NULL)
{
*head = infor;
(*head)->next=NULL;
}else
{
temp = (*head)->next;
while(temp!=NULL)
{
temp = temp->next;
}
temp = infor;
temp->next = NULL;
}
return 0;
}
void input(struct stu *infor)
{
printf("请输入学生姓名:");
scanf("%s", infor->Name);
printf("请输入数学成绩:");
scanf("%f", &(infor->Maths));
printf("请输入英语成绩:");
scanf("%f", &(infor->English));
printf("请输入C语言成绩:");
scanf("%f", &(infor->C));
infor->Total = (infor->Maths) (infor->English) (infor->C);
}
void output(struct stu *head)
{
struct stu *a;
a = head;
do
{
printf("%s", a->Name);
printf("%.1f", a->Maths);
printf("%.1f", a->English);
printf("%.1f", a->C);
printf("%.1f", a->Total);
a = a->next;
}while(a!=NULL);
} 展开
1个回答
2020-08-10
展开全部
if(*head==NULL)
{
*head = infor;
(*head)->next=NULL;
}else
/* {
temp = (*head)->next;
while(temp!=NULL)
{
temp = temp->next;
} //temp初始指向尾结点的next域
temp = infor;// 再局哪将temp指向了infor,就改变了temp的指向:从指向则腊谨尾结点的next域改为指向了infor。所以实际上并没有通过这条语句来改变链表。
temp->next = NULL;
}*/
下面是我做的一种改法
{
temp = *head;
while(temp->孙基next!=NULL)
{
temp = temp->next;
}
temp->next=infor;
temp->next->next = NULL;
}
同是小白一枚,若有错误,实在抱歉。
{
*head = infor;
(*head)->next=NULL;
}else
/* {
temp = (*head)->next;
while(temp!=NULL)
{
temp = temp->next;
} //temp初始指向尾结点的next域
temp = infor;// 再局哪将temp指向了infor,就改变了temp的指向:从指向则腊谨尾结点的next域改为指向了infor。所以实际上并没有通过这条语句来改变链表。
temp->next = NULL;
}*/
下面是我做的一种改法
{
temp = *head;
while(temp->孙基next!=NULL)
{
temp = temp->next;
}
temp->next=infor;
temp->next->next = NULL;
}
同是小白一枚,若有错误,实在抱歉。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询