关于链表的C语言程序求助
本帖最后由 guhusf 于 2021-1-15 16:45 编辑麻烦各位鱼油帮我看看 这个程序哪里出问题了,谢谢{:5_100:} (输入的时候num不能输入)
程序如下:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student) //结构的大小
struct student *creat(); //创造链表
void printf(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量
main()
{
struct student *stu;
stu=creat();
printf(stu);
printf("\n");
}
struct student *creat()
{
struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(LEN);//LEN是student结构的大小
printf("please enter the num:");
scanf("%d",p1->num);
printf("plese enter the score:");
scanf("%.1f",p1->score);
head=NULL;
n=0;
while(p1->num)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("please enter the num:");
scanf("%d",p1->num);
printf("\n");
printf("plese enter the score:");
scanf("%.1f",p1->score);
}
p2->next=NULL;
return head;
}
void printf(struct student *head)
{
struct student *p;
printf("There are %d records\n",n);
p=head;
if(head)
{
do
{
printf("学号为%d的成绩是:%.1f\n",p->num,p->score);
p=p->next;
}while(p);
}
} 本帖最后由 心驰神往 于 2021-1-15 16:49 编辑
{:10_254:}先领鱼币,这程序在我这都不能运行 1, 你的代码重写了printf函数,所以stdio.h里的printf函数被你写的覆盖了,把你写的函数换个名字就行了
2,你的循环条件好像永远都是为true的? 心驰神往 发表于 2021-1-15 16:47
先领鱼币,这程序在我这都不能运行
好家伙,老白嫖选手了{:10_256:} qiuyouzhi 发表于 2021-1-15 17:01
1, 你的代码重写了printf函数,所以stdio.h里的printf函数被你写的覆盖了,把你写的函数换个名字就行了
2 ...
好的,我试试,谢谢大佬{:10_275:} guhusf 发表于 2021-1-15 17:09
好家伙,老白嫖选手了
我把printf函数改了名字,没报错了,但还是不行 本帖最后由 jackz007 于 2021-1-15 17:37 编辑
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num ;
float score ;
struct student * next ;
} ;
int n ;
struct student * creat(void)
{
struct student * head , * p1 , * p2 ;
int num ;
float score ;
for(n = 0 , head = NULL ;;) {
printf("please enter the num : ") ;
scanf("%d" , & num) ;
if(num > 0) {
printf("plese enter the score : ") ;
scanf("%f" , & score) ;
if((p1 = (struct student *) malloc(sizeof(struct student)))) {
p1 -> num = num ;
p1 -> score = score ;
p1 -> next = NULL ;
if(! head) head = p1 ;
else p2 -> next = p1 ;
p2 = p1 ;
n ++ ;
} else {
for(p1 = head ; p1 ; p1 = p2) {
p2 = p1 -> next ;
free(p1) ;
}
head = NULL ;
fprintf(stderr , "\n") ;
fprintf(stderr , "failed to allocate memory !\n");
fprintf(stderr , "\n") ;
break ;
}
} else {
break ;
}
}
return head ;
}
void print(student * head)
{
student * p ;
printf("\n") ;
printf("There are %d records\n" , n) ;
for(p = head ; p ; p = p -> next) printf("学号为 %d 的成绩是 : %.1f\n" , p -> num , p -> score) ;
printf("\n") ;
}
int main(void)
{
struct student * stu = NULL ;
stu = creat() ;
print(stu) ;
printf("\n");
}
页:
[1]