鱼C论坛

 找回密码
 立即注册
查看: 2616|回复: 5

[已解决]带你学C带你飞p47第一题回文链表

[复制链接]
发表于 2023-1-2 17:59:59 | 显示全部楼层 |阅读模式

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

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

x
我真的崩溃了,大佬们告诉我这个程序哪里出问题了,它没报错但在DEV C++上就是不能运行
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node
  4. {
  5.         int num;
  6.         struct Node *next;
  7. };

  8. void touchafa(struct Node **head1,int input);
  9. void touchafa(struct Node **head1,int input)
  10. {
  11.        
  12.         struct Node *temp;
  13.        
  14.         struct Node *p=(struct Node *)malloc(sizeof(struct Node));
  15.         p->num=input;
  16.         if(*head1==NULL)
  17.         {
  18.                 *head1=p;
  19.                 p->next=NULL;
  20.         }
  21.         else
  22.         {
  23.                 temp=*head1;
  24.                 *head1=p;
  25.                 p->next=temp;       
  26.         }
  27. }

  28. void weichafa(struct Node **head2,int input);
  29. void weichafa(struct Node **head2,int input)
  30. {
  31.         static struct Node *store;
  32.         struct Node *p=(struct Node *)malloc(sizeof(struct Node));
  33.         p->num=input;
  34.         if(*head2==NULL)
  35.         {
  36.                 *head2=p;
  37.                 p->next=NULL;
  38.         }
  39.         else
  40.         {
  41.                 store->next=p;
  42.                 p->next=NULL;
  43.         }
  44.         store=p;
  45. }

  46. void printnum(struct Node *head);
  47. void printnum(struct Node *head)
  48. {
  49.         struct Node *p=head;
  50.         while(p!=NULL)
  51.         {
  52.                 printf("%d ",p->num);
  53.                 p=p->next;
  54.         }
  55.         putchar('\n');
  56. }


  57. int main()
  58. {
  59.         int input;struct Node *head1;struct Node *head2;
  60.         printf("下面开始录入链表,录入完毕后将检查这是否是回文链表(即顺着倒着读结果一样的链表):\n ");
  61.         while(1)
  62.         {
  63.                 printf("请输入一个整数(输入-1表示结束):\n");
  64.                 scanf("%d",&input);
  65.                 if(input== -1)
  66.                 {
  67.                         break;
  68.                 }
  69.                
  70.                 else
  71.                 {
  72.                         touchafa(&head1,input);
  73.                         weichafa(&head2,input);
  74.                 }       
  75.         }
  76.         printf("录入的链表是 ");
  77.         printnum(head2);

  78.         while(head1!=NULL)
  79.         {
  80.                         if(head1->num==head2->num)
  81.                         {
  82.                                 head1=head1->next;
  83.                                 head2=head2->next;
  84.                         }
  85.                         else
  86.                         {
  87.                                 break;
  88.                         }
  89.         }
  90.         if(head1==NULL&&head2==NULL)
  91.         {
  92.                 printf("经检查,该链表确实是回文链表!\n");
  93.         }
  94.         else
  95.         {
  96.                 printf("一眼就看出,这不是回文链表!\n");
  97.         }
  98.         return 0;
  99.        
  100.          
  101. }
复制代码
最佳答案
2023-1-3 13:20:10
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node {
  4.     int num;
  5.     struct Node *next;
  6. };

  7. //void touchafa(struct Node **head1, int input);        需要这一行吗?为什么?
  8. void touchafa(struct Node **head1, int input) {
  9.     //struct Node *temp;

  10.     //struct Node *p = (struct Node *)malloc(sizeof(struct Node));
  11.     struct Node *p = malloc(sizeof(struct Node));       // 为什么要强制转换呢?少写几个字符不好吗?
  12.     p->num = input;
  13.     /*
  14.     if(*head1 == NULL) {
  15.         *head1 = p;
  16.         p->next = NULL;
  17.     } else {
  18.         temp = *head1;
  19.         *head1 = p;
  20.         p->next = temp;
  21.     }
  22.     */
  23.     p->next = *head1;
  24.     *head1 = p;
  25. }

  26. //void weichafa(struct Node **head2, int input);        // 你在做什么?
  27. void weichafa(struct Node **head2, int input) {
  28.     //static struct Node *store;    // 这么写,用的时候不方便
  29.     static struct Node **store;
  30.     //struct Node *p = (struct Node *)malloc(sizeof(struct Node));
  31.     //struct Node *p = malloc(sizeof(struct Node));     // 少写上几个字符吧,写那么多干嘛?
  32.     struct Node *p = malloc(sizeof(*p));                // 一个更好的写法
  33.                                                         // 这样写是为了在修改p的类型之后不需要修改sizeof里面的类型
  34.                                                         // 少改一个地方就少一个出错的可能
  35.     p->num = input;
  36.     p->next = NULL;
  37.     /*
  38.     if(*head2 == NULL) {
  39.         *head2 = p;
  40.         p->next = NULL;
  41.     } else {
  42.         store->next = p;
  43.         p->next = NULL;
  44.     }
  45.     store = p;
  46.     */
  47.     if(!store) store = head2;
  48.     *store = p;
  49.     store = &p->next;
  50. }

  51. //void printnum(struct Node *head);     // 同上
  52. void printnum(struct Node *head) {
  53.     struct Node *p = head;
  54.     while(p != NULL) {
  55.         printf("%d ", p->num);
  56.         p = p->next;
  57.     }
  58.     putchar('\n');
  59. }

  60. void free_list(struct Node *head) {
  61.     if(!head) return;
  62.     free_list(head->next);
  63.     free(head);
  64. }

  65. int main() {
  66.     int input;
  67.     //struct Node *head1;
  68.     //struct Node *head2;
  69.     // 你不初始化吗?为什么?
  70.     struct Node *head1 = NULL;
  71.     struct Node *head2 = NULL;
  72.     printf("下面开始录入链表,录入完毕后将检查这是否是回文链表(即顺着倒着读结果一样的链表):\n ");
  73.     while(1) {
  74.         printf("请输入一个整数(输入-1表示结束):\n");
  75.         scanf("%d", &input);
  76.         /*
  77.         if(input == -1) {
  78.             break;
  79.         } else {
  80.             touchafa(&head1, input);
  81.             weichafa(&head2, input);
  82.         }
  83.         */
  84.         // 等价修改
  85.         // else 什么?
  86.         // 都跳出去了还else ?
  87.         if(input == -1) break;
  88.         touchafa(&head1, input);
  89.         weichafa(&head2, input);
  90.     }
  91.     printf("录入的链表是 ");
  92.     printnum(head2);

  93.     // 你这么写,你还释放内存不了?
  94.     // 申请了内存不释放就和借了东西不还一样恶心
  95.     // 都像你这么写程序,别说16GB,就是16EB的内存也不够你用呀
  96.     // 你这一直借东西不还,之后人家就不借给你了
  97.     // 系统哪有那么多内存给你这么整?
  98.     // 一直申请内存不释放,系统也没内存了,还怎么分配给你?
  99.     /*
  100.     while(head1 != NULL) {
  101.         if(head1->num == head2->num) {
  102.             head1 = head1->next;
  103.             head2 = head2->next;
  104.         } else {
  105.             break;
  106.         }
  107.     }
  108.     if(head1 == NULL && head2 == NULL) {
  109.         printf("经检查,该链表确实是回文链表!\n");
  110.     } else {
  111.         printf("一眼就看出,这不是回文链表!\n");
  112.     }
  113.     */
  114.     struct Node *t1 = head1;
  115.     struct Node *t2 = head2;
  116.     while(t1 != NULL && t2 != NULL) {
  117.         if(t1->num != t2->num) break;
  118.         t1 = t1->next;
  119.         t2 = t2->next;
  120.     }
  121.     if(t1 == NULL && t2 == NULL) {
  122.         printf("经检查,该链表确实是回文链表!\n");
  123.     } else {
  124.         printf("一眼就看出,这不是回文链表!\n");
  125.     }

  126.     // 释放内存,不要借了不还,这样很不礼貌
  127.     free_list(head2);
  128.     free_list(head1);
  129.     return 0;
  130. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-1-2 18:28:26 From FishC Mobile | 显示全部楼层
head1 和 head2 需不需要初始化?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-3 12:27:58 From FishC Mobile | 显示全部楼层
本帖最后由 wyxnogiveup 于 2023-1-3 18:05 编辑
dolly_yos2 发表于 2023-1-2 18:28
head1 和 head2 需不需要初始化?


试过了,没用,它是在尾插法函数死掉的



来自六小时后的回复:呜呜呜确实是初始化的问题,我太粗心了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-3 13:20:10 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node {
  4.     int num;
  5.     struct Node *next;
  6. };

  7. //void touchafa(struct Node **head1, int input);        需要这一行吗?为什么?
  8. void touchafa(struct Node **head1, int input) {
  9.     //struct Node *temp;

  10.     //struct Node *p = (struct Node *)malloc(sizeof(struct Node));
  11.     struct Node *p = malloc(sizeof(struct Node));       // 为什么要强制转换呢?少写几个字符不好吗?
  12.     p->num = input;
  13.     /*
  14.     if(*head1 == NULL) {
  15.         *head1 = p;
  16.         p->next = NULL;
  17.     } else {
  18.         temp = *head1;
  19.         *head1 = p;
  20.         p->next = temp;
  21.     }
  22.     */
  23.     p->next = *head1;
  24.     *head1 = p;
  25. }

  26. //void weichafa(struct Node **head2, int input);        // 你在做什么?
  27. void weichafa(struct Node **head2, int input) {
  28.     //static struct Node *store;    // 这么写,用的时候不方便
  29.     static struct Node **store;
  30.     //struct Node *p = (struct Node *)malloc(sizeof(struct Node));
  31.     //struct Node *p = malloc(sizeof(struct Node));     // 少写上几个字符吧,写那么多干嘛?
  32.     struct Node *p = malloc(sizeof(*p));                // 一个更好的写法
  33.                                                         // 这样写是为了在修改p的类型之后不需要修改sizeof里面的类型
  34.                                                         // 少改一个地方就少一个出错的可能
  35.     p->num = input;
  36.     p->next = NULL;
  37.     /*
  38.     if(*head2 == NULL) {
  39.         *head2 = p;
  40.         p->next = NULL;
  41.     } else {
  42.         store->next = p;
  43.         p->next = NULL;
  44.     }
  45.     store = p;
  46.     */
  47.     if(!store) store = head2;
  48.     *store = p;
  49.     store = &p->next;
  50. }

  51. //void printnum(struct Node *head);     // 同上
  52. void printnum(struct Node *head) {
  53.     struct Node *p = head;
  54.     while(p != NULL) {
  55.         printf("%d ", p->num);
  56.         p = p->next;
  57.     }
  58.     putchar('\n');
  59. }

  60. void free_list(struct Node *head) {
  61.     if(!head) return;
  62.     free_list(head->next);
  63.     free(head);
  64. }

  65. int main() {
  66.     int input;
  67.     //struct Node *head1;
  68.     //struct Node *head2;
  69.     // 你不初始化吗?为什么?
  70.     struct Node *head1 = NULL;
  71.     struct Node *head2 = NULL;
  72.     printf("下面开始录入链表,录入完毕后将检查这是否是回文链表(即顺着倒着读结果一样的链表):\n ");
  73.     while(1) {
  74.         printf("请输入一个整数(输入-1表示结束):\n");
  75.         scanf("%d", &input);
  76.         /*
  77.         if(input == -1) {
  78.             break;
  79.         } else {
  80.             touchafa(&head1, input);
  81.             weichafa(&head2, input);
  82.         }
  83.         */
  84.         // 等价修改
  85.         // else 什么?
  86.         // 都跳出去了还else ?
  87.         if(input == -1) break;
  88.         touchafa(&head1, input);
  89.         weichafa(&head2, input);
  90.     }
  91.     printf("录入的链表是 ");
  92.     printnum(head2);

  93.     // 你这么写,你还释放内存不了?
  94.     // 申请了内存不释放就和借了东西不还一样恶心
  95.     // 都像你这么写程序,别说16GB,就是16EB的内存也不够你用呀
  96.     // 你这一直借东西不还,之后人家就不借给你了
  97.     // 系统哪有那么多内存给你这么整?
  98.     // 一直申请内存不释放,系统也没内存了,还怎么分配给你?
  99.     /*
  100.     while(head1 != NULL) {
  101.         if(head1->num == head2->num) {
  102.             head1 = head1->next;
  103.             head2 = head2->next;
  104.         } else {
  105.             break;
  106.         }
  107.     }
  108.     if(head1 == NULL && head2 == NULL) {
  109.         printf("经检查,该链表确实是回文链表!\n");
  110.     } else {
  111.         printf("一眼就看出,这不是回文链表!\n");
  112.     }
  113.     */
  114.     struct Node *t1 = head1;
  115.     struct Node *t2 = head2;
  116.     while(t1 != NULL && t2 != NULL) {
  117.         if(t1->num != t2->num) break;
  118.         t1 = t1->next;
  119.         t2 = t2->next;
  120.     }
  121.     if(t1 == NULL && t2 == NULL) {
  122.         printf("经检查,该链表确实是回文链表!\n");
  123.     } else {
  124.         printf("一眼就看出,这不是回文链表!\n");
  125.     }

  126.     // 释放内存,不要借了不还,这样很不礼貌
  127.     free_list(head2);
  128.     free_list(head1);
  129.     return 0;
  130. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-1-3 18:00:58 | 显示全部楼层
dolly_yos2 发表于 2023-1-2 18:28
head1 和 head2 需不需要初始化?

呜呜呜,确实是没有初始化的问题,感谢指正
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-1-3 18:03:56 | 显示全部楼层

太谢谢大佬了!呜呜呜我以后肯定会牢记要释放内存的!那个将sizeof(struct Node)改为sizeof(*p)同样很巧妙,原来还能这样。真的谢谢大佬,细致地帮我指出问题!萌新一定好好改正陋习,不负教导!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 18:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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