鱼C论坛

 找回密码
 立即注册
查看: 841|回复: 2

[已解决]请问为什么代码最后判断(我想让用户判断是否反转链表)程序自动return0,具体见注释

[复制链接]
发表于 2022-3-16 22:52:23 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>[free]
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include "a.h"

  5. //这是c语言链表3的第一个作业题

  6. struct Num
  7. {
  8.     int num;
  9.     struct Num* next;
  10. };

  11. void sortnum(struct Num **head,int a)
  12. {
  13.     struct Num* c=*head;
  14.     struct Num* p=NULL;
  15.     struct Num* new;
  16.    
  17.     new=(struct Num*)malloc(sizeof(struct Num));
  18.     new->num=a;
  19.    
  20.     while(c!=NULL && c->num<a)
  21.     {
  22.         p=c;
  23.         c=c->next;
  24.     }
  25.     new->next=c;
  26.    
  27.     if(p==NULL)
  28.     {
  29.         *head=new;
  30.     }
  31.     else{
  32.         p->next=new;
  33.     }
  34. }

  35. struct Num* returnnum(struct Num* head)
  36. {
  37.     struct Num* c=head;
  38.     struct Num* p=NULL;
  39.     while(c)
  40.             {
  41.                     struct Num *next = c->next;
  42.                     c->next = p;
  43.                     p = c;
  44.                     c = next;
  45.             }
  46.         
  47.             return p;
  48.    
  49. }

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

  60. void freenum(struct Num* head)
  61. {
  62.     struct Num* h=head;
  63.     while(h)
  64.     {
  65.         h=h->next;
  66.         free(h);
  67.     }
  68. }

  69. int main(void)
  70. {
  71.     struct Num* head=NULL;
  72.     int num;
  73.    
  74.     while(1)
  75.     {
  76.         printf("please input a (int)number : \n");
  77.         scanf("%d",&num);
  78.         if(num==-1)
  79.         {
  80.             break;
  81.         }
  82.         else[/free]
  83.         {
  84.             sortnum(&head,num);
  85.             printnum(head);
  86.         }
  87.     }
  88.    
  89.     char* c;//不做判断只调用程序正常,做了后下面直接return 0
  90.     c=(char*)malloc(sizeof(char));
  91.     printf("是否要反转(y/n)\n");
  92.     scanf("%c",c);
  93.    if(*c=='y')
  94.    {
  95.         printf("反转:\n");
  96.         head=returnnum(head);
  97.         printnum(head);
  98.    }
  99.     free(c);
  100.     freenum(head);
  101.    
  102.     return 0;
  103. }
复制代码
最佳答案
2022-3-16 22:59:30
没问题

  1. #include<stdio.h>
  2. #include<stdlib.h>


  3. int main(void)
  4. {
  5.     char *c;
  6.     c=(char*)malloc(sizeof(char));

  7.     printf("是否要反转(y/n)\n");
  8.     scanf("%c", c);
  9.    if(*c=='y')
  10.    {
  11.         printf("反转:\n");
  12.    }

  13.     return 0;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-16 22:59:30 | 显示全部楼层    本楼为最佳答案   
没问题

  1. #include<stdio.h>
  2. #include<stdlib.h>


  3. int main(void)
  4. {
  5.     char *c;
  6.     c=(char*)malloc(sizeof(char));

  7.     printf("是否要反转(y/n)\n");
  8.     scanf("%c", c);
  9.    if(*c=='y')
  10.    {
  11.         printf("反转:\n");
  12.    }

  13.     return 0;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-16 23:02:09 | 显示全部楼层

可是程序就会跳过我下面注释往后的部分直接return 0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 19:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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