鱼C论坛

 找回密码
 立即注册
查看: 1291|回复: 6

[已解决]关于链表的C语言程序求助

[复制链接]
发表于 2021-1-15 16:42:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 guhusf 于 2021-1-15 16:45 编辑

麻烦各位鱼油帮我看看 这个程序哪里出问题了,谢谢 (输入的时候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 17:23:09
本帖最后由 jackz007 于 2021-1-15 17:37 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct student
  4. {
  5.         int num               ;
  6.         float score           ;
  7.         struct student * next ;
  8. }                             ;

  9. int n                         ;

  10. struct student * creat(void)
  11. {
  12.         struct student * head , * p1 , * p2                                        ;
  13.         int num                                                                    ;
  14.         float score                                                                ;
  15.         for(n = 0 , head = NULL ;;) {
  16.                 printf("please enter the num : ")                                  ;
  17.                 scanf("%d" , & num)                                                ;
  18.                 if(num > 0) {
  19.                         printf("plese enter the score : ")                         ;
  20.                         scanf("%f" , & score)                                      ;
  21.                         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
  22.                                 p1 -> num = num                                    ;
  23.                                 p1 -> score = score                                ;
  24.                                 p1 -> next = NULL                                  ;
  25.                                 if(! head) head = p1                               ;
  26.                                 else p2 -> next = p1                               ;
  27.                                 p2 = p1                                            ;
  28.                                 n ++                                               ;
  29.                         } else {
  30.                                 for(p1 = head ; p1 ; p1 = p2) {
  31.                                         p2 = p1 -> next                            ;
  32.                                         free(p1)                                   ;
  33.                                 }
  34.                                 head = NULL                                        ;
  35.                                 fprintf(stderr , "\n")                             ;
  36.                                 fprintf(stderr , "failed to allocate memory !\n")  ;
  37.                                 fprintf(stderr , "\n")                             ;
  38.                                 break                                              ;
  39.                         }
  40.                 } else {
  41.                         break                                                      ;
  42.                 }
  43.         }
  44.         return head                                                                ;
  45. }

  46. void print(student * head)
  47. {
  48.         student * p                                                                                     ;
  49.         printf("\n")                                                                                    ;
  50.         printf("There are %d records\n" , n)                                                            ;
  51.         for(p = head ; p ; p = p -> next) printf("学号为 %d 的成绩是 : %.1f\n" , p -> num , p -> score) ;
  52.         printf("\n")                                                                                    ;
  53. }

  54. int main(void)
  55. {
  56.         struct student * stu = NULL ;
  57.         stu = creat() ;
  58.         print(stu)    ;
  59.         printf("\n")  ;
  60. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-15 16:47:34 | 显示全部楼层

回帖奖励 +3 鱼币

本帖最后由 心驰神往 于 2021-1-15 16:49 编辑

先领鱼币,这程序在我这都不能运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 17:01:17 | 显示全部楼层
1, 你的代码重写了printf函数,所以stdio.h里的printf函数被你写的覆盖了,把你写的函数换个名字就行了
2,你的循环条件好像永远都是为true的?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-15 17:09:09 | 显示全部楼层
心驰神往 发表于 2021-1-15 16:47
先领鱼币,这程序在我这都不能运行

好家伙,老白嫖选手了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-15 17:09:40 | 显示全部楼层
qiuyouzhi 发表于 2021-1-15 17:01
1, 你的代码重写了printf函数,所以stdio.h里的printf函数被你写的覆盖了,把你写的函数换个名字就行了
2 ...

好的,我试试,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 17:11:04 | 显示全部楼层
guhusf 发表于 2021-1-15 17:09
好家伙,老白嫖选手了

我把printf函数改了名字,没报错了,但还是不行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-15 17:23:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-1-15 17:37 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct student
  4. {
  5.         int num               ;
  6.         float score           ;
  7.         struct student * next ;
  8. }                             ;

  9. int n                         ;

  10. struct student * creat(void)
  11. {
  12.         struct student * head , * p1 , * p2                                        ;
  13.         int num                                                                    ;
  14.         float score                                                                ;
  15.         for(n = 0 , head = NULL ;;) {
  16.                 printf("please enter the num : ")                                  ;
  17.                 scanf("%d" , & num)                                                ;
  18.                 if(num > 0) {
  19.                         printf("plese enter the score : ")                         ;
  20.                         scanf("%f" , & score)                                      ;
  21.                         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
  22.                                 p1 -> num = num                                    ;
  23.                                 p1 -> score = score                                ;
  24.                                 p1 -> next = NULL                                  ;
  25.                                 if(! head) head = p1                               ;
  26.                                 else p2 -> next = p1                               ;
  27.                                 p2 = p1                                            ;
  28.                                 n ++                                               ;
  29.                         } else {
  30.                                 for(p1 = head ; p1 ; p1 = p2) {
  31.                                         p2 = p1 -> next                            ;
  32.                                         free(p1)                                   ;
  33.                                 }
  34.                                 head = NULL                                        ;
  35.                                 fprintf(stderr , "\n")                             ;
  36.                                 fprintf(stderr , "failed to allocate memory !\n")  ;
  37.                                 fprintf(stderr , "\n")                             ;
  38.                                 break                                              ;
  39.                         }
  40.                 } else {
  41.                         break                                                      ;
  42.                 }
  43.         }
  44.         return head                                                                ;
  45. }

  46. void print(student * head)
  47. {
  48.         student * p                                                                                     ;
  49.         printf("\n")                                                                                    ;
  50.         printf("There are %d records\n" , n)                                                            ;
  51.         for(p = head ; p ; p = p -> next) printf("学号为 %d 的成绩是 : %.1f\n" , p -> num , p -> score) ;
  52.         printf("\n")                                                                                    ;
  53. }

  54. int main(void)
  55. {
  56.         struct student * stu = NULL ;
  57.         stu = creat() ;
  58.         print(stu)    ;
  59.         printf("\n")  ;
  60. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-2 19:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表