鱼C论坛

 找回密码
 立即注册
查看: 2704|回复: 3

链表插入后,程序不停打印为什么?

[复制链接]
发表于 2019-3-15 17:51:52 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
int n;
struct student
{
        int num;
        float score;
        struct student *next;
} ;
int main()
{

        struct student * creat(void);
        struct student * del(struct student *pstu, struct student *p_del_stu);
        struct student * insert(struct student *pstu, struct student *p_in_stu);
        void print(struct student *pstu);
        struct student *stu,*stu_x;
        struct student student_X;
        stu_x = &student_X;

        stu = creat();
        print(stu);


        printf("请输入需要删除的学生的学号(0结束):");
        scanf_s("%d", &stu_x->num);
        while (stu_x->num)
        {
                stu=del(stu, stu_x);
                print(stu);

                printf("请输入需要删除的学生的学号(0结束):");
                scanf_s("%d", &stu_x->num);
        }

        printf("\n");
        printf("请输入需要插入的学生的学号(0结束):");
        scanf_s("%d", &stu_x->num);
        printf("请输入需要插入的学生的成绩:");
        scanf_s("%f", &stu_x->score);
        stu_x->next = NULL;
        while (stu_x->num)
        {
                stu = insert(stu, stu_x);
                print(stu);
                printf("请输入需要插入的学生的学号(0结束):");
                scanf_s("%d", &stu_x->num);
                printf("请输入需要插入的学生的成绩:");
                scanf_s("%f", &stu_x->score);
                stu_x->next = NULL;
        }

        system("pause");
        return 0;

}

//生成链表函数
struct student * creat(void)
{
        struct student *p1, *p2, *head;
        n = 0;
        p1 = p2 = (struct student *)malloc(sizeof(struct student));
        printf("please input stuent number(0 to terminate):");
        scanf_s("%d", &p1->num, 1);
        printf("please input stuent score:");
        scanf_s("%f", &p1->score, 2);
        head = NULL;
        while (p1->num)
        {
                n = n + 1;
                if (n == 1)
                        head = p1;
                else
                        p2->next = p1;
                p2 = p1;
                p1 = (struct student *)malloc(sizeof(struct student));
                printf("please input stuent number(0 to terminate):");
                scanf_s("%d", &p1->num, 1);
                printf("please input stuent score:");
                scanf_s("%f", &p1->score, 2);

        }
        p2->next = NULL;

        return head;


}


//链表节点删除函数
struct student * del(struct student *pstu, struct student *p_del_stu)
{
        struct student *head,*p1,*p2,*p0;
        p0 = p_del_stu;
        head = p1 = p2 = NULL;

        if (pstu)
                {
                        head = pstu;
                        p1 = head;
                        while (p1->num != p0->num && p1->next != NULL)
                        {
                                p2 = p1;
                                p1 = p1->next;

                        }

                        if (p1->num == p0->num)
                        {
                                if (p1 == head)
                                {
                                        head = p1->next;
                                }
                                else
                                        p2->next = p1->next;
                                n--;
                        }
                        else
                                printf("没有该学生记录\n");
                }

                else
                        printf("此表为空链表!!!\n\n");

        return head;
}

//链表插入
struct student * insert(struct student *pstu, struct student *p_in_stu )
{
        struct student *head,*p0, *p1, *p2;
        head = p1 = pstu;
        p2 = NULL;
        p0 = p_in_stu;

        if (head)
        {
                while (p0->num > p1->num &&p1->next != NULL)
                {
                        p2 = p1;
                        p1 = p1->next;
                }
                if (p0->num <= p1->num)
                {
                        if (p1 == head)
                        {
                                head = p0;
                                p0->next = p1;
                        }
                        else
                        {
                                p2->next = p0;
                                p0->next = p1;
                        }
                       

                }
                else
                {
                        p1->next = p0;
                        p0->next = NULL;
                }
                n++;
        }


        return head;
}



//打印函数
void print(struct student *pstu)
{
        struct student *head;
        head = pstu;
        printf("There are %d records!!!\n", n);
        while (head)
        {
                printf("student number = %d\n", head->num);
                printf("student score  = %5.2f\n\n", head->score);

                head = head->next;
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-15 18:08:14 | 显示全部楼层
这代码看起纠结死人。
又中文又英文,乱七八遭的。
先整理下思路,参考一下别人的代码改下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-16 01:09:55 | 显示全部楼层
本帖最后由 jackz007 于 2019-3-16 01:35 编辑

        你的代码太乱,看起来很费劲,我又按照你的程序框架重写了全部代码供你参考。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>

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

  10. int n                         ;

  11. //链表插入
  12. node * insert(node * pstu)
  13. {
  14.         node * head , * p1 , * p2                                                                         ;
  15.         int num                                                                                           ;
  16.         float score                                                                                       ;
  17.         bool f                                                                                            ;

  18.         head = pstu                                                                                       ;
  19.         for(;;) {
  20.                 printf("please input stuent number(0 to terminate): ")                                    ;
  21.                 scanf("%d", & num)                                                                        ;
  22.                 if(num == 0) {
  23.                         break                                                                             ;
  24.                 } else {
  25.                         f = true                                                                          ;
  26.                         p1 = p2 = head                                                                    ;
  27.                         while(p2 != NULL) {
  28.                                 if(p2 -> num == num) {
  29.                                         f = false                                                         ;
  30.                                         break                                                             ;
  31.                                 } else {
  32.                                         p1 = p2                                                           ;
  33.                                         p2 = p1 -> next                                                   ;
  34.                                 }
  35.                         }
  36.                         if(f) {
  37.                                 printf("please input stuent score: ")                                     ;
  38.                                 scanf("%f", & score)                                                      ;
  39.                                 p2 = (node *) malloc(sizeof(node))                                        ;
  40.                                 if(p2 != NULL) {
  41.                                         p2 -> num = num                                                   ;
  42.                                         p2 -> score = score                                               ;
  43.                                         p2 -> next = NULL                                                 ;
  44.                                         if(! n) head = p2                                                 ;
  45.                                         else p1 -> next = p2                                              ;
  46.                                         n ++                                                              ;
  47.                                 } else {
  48.                                         fprintf(stderr , "error : allocate memory operation failure !\n") ;
  49.                                 }
  50.                         } else {
  51.                                 printf("抱歉,学号为 %d 的学生已经存在!\n" , num)                         ;
  52.                         }
  53.                 }
  54.         }
  55.         return head                                                                                       ;
  56. }

  57. //链表节点删除函数
  58. node * del(node * pstu)
  59. {
  60.         node * p1 , * p2 , * head                                                        ;
  61.         int num , m                                                                      ;

  62.         bool f                                                                           ;
  63.         head = pstu                                                                      ;
  64.         if(n > 0) {
  65.                 while(n > 0) {
  66.                         printf("please input stuent number(0 to terminate): ")           ;
  67.                         scanf("%d", & num)                                               ;
  68.                         if(num != 0) {
  69.                                 f = false                                                ;
  70.                                 p2 = head                                                ;
  71.                                 m = 0                                                    ;
  72.                                 while(p2 != NULL) {
  73.                                         if(p2 -> num == num) {
  74.                                                 if(! m) head = p2 -> next                ;
  75.                                                 else p1 -> next = p2 -> next             ;
  76.                                                 f = true                                 ;
  77.                                                 break                                    ;
  78.                                         } else {
  79.                                                 p1 = p2                                  ;
  80.                                                 p2 = p1 -> next                          ;
  81.                                         }
  82.                                         m ++                                             ;
  83.                                 }
  84.                                 if(f) {
  85.                                         free(p2)                                         ;
  86.                                         printf("成功删除学号为 %d 的学生\n" , num)       ;
  87.                                         n --                                             ;
  88.                                 } else {
  89.                                         printf("抱歉,没有找到学号为 %d 的学生\n" , num) ;
  90.                                 }
  91.                         } else {
  92.                                 break                                                    ;
  93.                         }
  94.                 }
  95.         } else {
  96.                 printf("链表为空!!!\n\n")                                                ;
  97.         }
  98.         return head                                                                      ;
  99. }

  100. //打印函数
  101. void print(node * pstu)
  102. {
  103.         node * p                                                          ;
  104.         if(n > 0) {
  105.                 p = pstu                                                  ;
  106.                 printf("There are %d records!!!\n", n)                    ;
  107.                 while (p != NULL) {
  108.                         printf("student number = %d\n" , p -> num)        ;
  109.                         printf("student score  = %5.2f\n\n" , p -> score) ;
  110.                         p = p -> next                                     ;
  111.                 }
  112.         } else {
  113.                 printf("链表为空!!!\n\n")                                 ;
  114.         }
  115. }

  116. int main(void)
  117. {
  118.         node * stu                                                                             ;
  119.         char c                                                                                 ;
  120.         stu = insert(NULL)                                                                     ;
  121.         for(;;) {
  122.                 printf("\n")                                                                   ;
  123.                 printf("**********************************************\n")                     ;
  124.                 printf("*             学生成绩信息管理系统           *\n")                     ;
  125.                 printf("**********************************************\n")                     ;
  126.                 printf("             1. 添加学生成绩信息.\n")                                  ;
  127.                 printf("             2. 删除学生成绩信息.\n")                                  ;
  128.                 printf("             3. 显示学生成绩信息.\n")                                  ;
  129.                 printf("             0. 退出学生成绩信息系统.\n")                              ;
  130.                 printf("----------------------------------------------\n")                     ;
  131.                 printf("           请选择(0 - 4):[1] ")                                        ;
  132.                 c = getch()                                                                    ;
  133.                 printf("\n\n")                                                                 ;
  134.                 if((c > 0x2f && c < 0x34) || c == 0x03 || c == 0x0d) {
  135.                         if(c == 0x03 || c == 0x30) {
  136.                                 break                                                          ;
  137.                         } else {
  138.                                 switch(c) {
  139.                                 case 0x0d:
  140.                                 case 0x31:
  141.                                         stu = insert(stu)                                      ;
  142.                                         break                                                  ;
  143.                                 case 0x32:
  144.                                         stu = del(stu)                                         ;
  145.                                         break                                                  ;
  146.                                 case 0x33:
  147.                                         print(stu)                                             ;
  148.                                         break                                                  ;
  149.                                 }
  150.                         }
  151.                 }
  152.         }
  153.         system("pause")                                                                        ;
  154. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-16 11:05:25 | 显示全部楼层
谢谢,指正。才开始学习,以后注意写代码习惯。我发现了我循环插入时没有重新开辟空间导致死循环。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 11:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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