鱼C论坛

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

有关链表的一个问题

[复制链接]
发表于 2020-5-25 20:25:53 | 显示全部楼层 |阅读模式

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

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

x
可以直接看第44行的位置。不明白为什么第二种就输出不了
第一种
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. struct book                                                //书籍信息的结构
  5. {       
  6.         int book_id;                               
  7.         char book_name[30];       //一个书的id、一个书名、5个关键字、3个作者、一个发布地、一个发布时间
  8.         char keyword1[30];
  9.         char keyword2[30];
  10.         char keyword3[30];
  11.         char keyword4[30];
  12.         char keyword5[30];
  13.         char author1[30];
  14.         char author2[30];
  15.         char author3[30];
  16.         char publisher[30];
  17.         char publish_time[30];
  18.         struct book *next;
  19. }*Book;

  20. struct book *data_book_sort(struct book *head);
  21. struct book *exchange(struct book *p);    //交换p的右边与p右边的右边的结点
  22. struct book *data_book_input();        //将数据库数据导出到一个结构指针上

  23. void main()
  24. {
  25.         struct book *head;
  26.         head = data_book_input();                                        //head是头指针
  27.         head = data_book_sort(head);
  28.         printf("%d\n",head->book_id);                                //这里是测试用
  29.         printf("%s\n",head->book_name);
  30.         head = head->next;
  31.         printf("%d\n",head->book_id);
  32.         printf("%s\n",head->book_name);
  33. }
  34. struct book *data_book_sort(struct book *head)                       
  35. {
  36.         struct book *p, *q;
  37.         p = (struct book *)malloc(sizeof(struct book));
  38.         q = (struct book *)malloc(sizeof(struct book));
  39.         q->next = head;
  40.         head = q;                                                //建立一个空的头结点插入到链表头里
  41.         p = q = head;
  42.         while(q->next != NULL)                               
  43.         {       
  44.                 p = head;
  45.                 while(p->next->next != NULL)                //不断循环,把最大的放到最后
  46.                 {
  47.                         if(p->next->book_id > p->next->next->book_id)                        //从小到大排序
  48.                                 exchange(p);
  49.                         p = p->next;
  50.                 }
  51.                 q = q->next;
  52.         }
  53.         head = head->next;
  54.         return head;
  55. }
  56. struct book *exchange(struct book *p)   //交换p后面两个结点顺序
  57. {
  58.         struct book *t;
  59.         t = p->next;
  60.         p->next = t->next;
  61.         t->next = p->next->next;
  62.         p->next->next = t;
  63. }
  64. struct book *data_book_input()                //将数据库数据导出到一个结构指针上
  65. {       
  66.         struct book *p,*head,*tail;
  67.         head = tail = NULL;
  68.         int book_id;
  69.         char book_name[30];
  70.         char keyword1[30];
  71.         char keyword2[30];
  72.         char keyword3[30];
  73.         char keyword4[30];
  74.         char keyword5[30];
  75.         char author1[30];
  76.         char author2[30];
  77.         char author3[30];
  78.         char publisher[30];
  79.         char publish_time[30];
  80.         FILE *f = NULL;
  81.         if((f=fopen("data_book.txt","r")) == NULL)
  82.         {
  83.                 printf("File is Error! No1\n");
  84.                 return NULL;
  85.         }
  86.         while(fscanf(f, "%d %s %s %s %s %s %s %s %s %s %s %s",
  87.                 &book_id,book_name,keyword1,keyword2,keyword3,keyword4,keyword5,
  88.                 author1,author2,author3,publisher,publish_time) != EOF)
  89.         {
  90.         if((p = (struct book *)malloc(sizeof(struct book)))==NULL)                //分配储存空间
  91.                 {       
  92.                         printf("Error!");
  93.                         return NULL;
  94.                 }
  95.                 p->book_id = book_id;                                        //导入数据
  96.                 strcpy(p->book_name , book_name);
  97.                 strcpy(p->keyword1 , keyword1);
  98.                 strcpy(p->keyword2 , keyword2);
  99.                 strcpy(p->keyword3 , keyword3);
  100.                 strcpy(p->keyword4 , keyword4);
  101.                 strcpy(p->keyword5 , keyword5);
  102.                 strcpy(p->author1 , author1);
  103.                 strcpy(p->author2 , author2);
  104.                 strcpy(p->author3 , author3);
  105.                 strcpy(p->publisher , publisher);
  106.                 strcpy(p->publish_time , publish_time);
  107.                 p->next = NULL;        //顺序建立链表
  108.                 if(head == NULL)
  109.             head = p;
  110.             else
  111.             tail->next = p;
  112.             tail = p;
  113.         }                        //链表头地址是head,最后的next是NULL
  114.         fclose(f);
  115.         return head;        //返回头指针
  116. }
复制代码

第二种
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. struct book                                                //书籍信息的结构
  5. {       
  6.         int book_id;                               
  7.         char book_name[30];       //一个书的id、一个书名、5个关键字、3个作者、一个发布地、一个发布时间
  8.         char keyword1[30];
  9.         char keyword2[30];
  10.         char keyword3[30];
  11.         char keyword4[30];
  12.         char keyword5[30];
  13.         char author1[30];
  14.         char author2[30];
  15.         char author3[30];
  16.         char publisher[30];
  17.         char publish_time[30];
  18.         struct book *next;
  19. }*Book;

  20. struct book *data_book_sort(struct book *head);
  21. struct book *exchange(struct book *p);    //交换p的右边与p右边的右边的结点
  22. struct book *data_book_input();        //将数据库数据导出到一个结构指针上

  23. void main()
  24. {
  25.         struct book *head;
  26.         head = data_book_input();                                        //head是头指针
  27.         head = data_book_sort(head);
  28.         printf("%d\n",head->book_id);                                //这里是测试用
  29.         printf("%s\n",head->book_name);
  30.         head = head->next;
  31.         printf("%d\n",head->book_id);
  32.         printf("%s\n",head->book_name);
  33. }
  34. struct book *data_book_sort(struct book *head)                       
  35. {
  36.         struct book *p, *q;
  37.         p = (struct book *)malloc(sizeof(struct book));
  38.         q = (struct book *)malloc(sizeof(struct book));
  39.         q->next = head;
  40.         head = q;                                                //建立一个空的头结点插入到链表头里
  41.         p = q = head;
  42.         while(q->next->next != NULL)                               
  43.         {       
  44.                 p = head;
  45.                 while(p->next->next != NULL)                //不断循环,把最大的放到最后
  46.                 {
  47.                         if(p->next->book_id > p->next->next->book_id)                        //从小到大排序
  48.                                 exchange(p);
  49.                         p = p->next;
  50.                 }
  51.                 q = q->next;
  52.         }
  53.         head = head->next;
  54.         return head;
  55. }
  56. struct book *exchange(struct book *p)   //交换p后面两个结点顺序
  57. {
  58.         struct book *t;
  59.         t = p->next;
  60.         p->next = t->next;
  61.         t->next = p->next->next;
  62.         p->next->next = t;
  63. }
  64. struct book *data_book_input()                //将数据库数据导出到一个结构指针上
  65. {       
  66.         struct book *p,*head,*tail;
  67.         head = tail = NULL;
  68.         int book_id;
  69.         char book_name[30];
  70.         char keyword1[30];
  71.         char keyword2[30];
  72.         char keyword3[30];
  73.         char keyword4[30];
  74.         char keyword5[30];
  75.         char author1[30];
  76.         char author2[30];
  77.         char author3[30];
  78.         char publisher[30];
  79.         char publish_time[30];
  80.         FILE *f = NULL;
  81.         if((f=fopen("data_book.txt","r")) == NULL)
  82.         {
  83.                 printf("File is Error! No1\n");
  84.                 return NULL;
  85.         }
  86.         while(fscanf(f, "%d %s %s %s %s %s %s %s %s %s %s %s",
  87.                 &book_id,book_name,keyword1,keyword2,keyword3,keyword4,keyword5,
  88.                 author1,author2,author3,publisher,publish_time) != EOF)
  89.         {
  90.         if((p = (struct book *)malloc(sizeof(struct book)))==NULL)                //分配储存空间
  91.                 {       
  92.                         printf("Error!");
  93.                         return NULL;
  94.                 }
  95.                 p->book_id = book_id;                                        //导入数据
  96.                 strcpy(p->book_name , book_name);
  97.                 strcpy(p->keyword1 , keyword1);
  98.                 strcpy(p->keyword2 , keyword2);
  99.                 strcpy(p->keyword3 , keyword3);
  100.                 strcpy(p->keyword4 , keyword4);
  101.                 strcpy(p->keyword5 , keyword5);
  102.                 strcpy(p->author1 , author1);
  103.                 strcpy(p->author2 , author2);
  104.                 strcpy(p->author3 , author3);
  105.                 strcpy(p->publisher , publisher);
  106.                 strcpy(p->publish_time , publish_time);
  107.                 p->next = NULL;        //顺序建立链表
  108.                 if(head == NULL)
  109.             head = p;
  110.             else
  111.             tail->next = p;
  112.             tail = p;
  113.         }                        //链表头地址是head,最后的next是NULL
  114.         fclose(f);
  115.         return head;        //返回头指针
  116. }


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-25 20:32:19 | 显示全部楼层
笔者奇怪的是while循环应该是都有出口,但是为什么第二种没有输出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-25 20:58:40 | 显示全部楼层
没有输出是什么意思呢,是没有输出链表结点的值吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-25 21:07:53 | 显示全部楼层
chxchxkkk 发表于 2020-5-25 20:58
没有输出是什么意思呢,是没有输出链表结点的值吗?

就是我那里文件数据导入到链表里,第一种的printf能打印出排序后的数据,第二种全黑的,啥都没有,没有输出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-25 21:46:05 | 显示全部楼层
我不会,我是来学习的经验的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-25 22:25:03 | 显示全部楼层
设置断点,单步调试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-28 19:20:23 | 显示全部楼层
拯救一下沉帖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-4 20:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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