鱼C论坛

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

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

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

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

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

x
#include<stdio.h>[free]
#include<string.h>
#include<stdlib.h>
#include "a.h"

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

struct Num
{
    int num;
    struct Num* next;
};

void sortnum(struct Num **head,int a)
{
    struct Num* c=*head;
    struct Num* p=NULL;
    struct Num* new;
    
    new=(struct Num*)malloc(sizeof(struct Num));
    new->num=a;
    
    while(c!=NULL && c->num<a)
    {
        p=c;
        c=c->next;
    }
    new->next=c;
    
    if(p==NULL)
    {
        *head=new;
    }
    else{
        p->next=new;
    }
}

struct Num* returnnum(struct Num* head)
{
    struct Num* c=head;
    struct Num* p=NULL;
    while(c)
            {
                    struct Num *next = c->next;
                    c->next = p;
                    p = c;
                    c = next;
            }
        
            return p;
   
}

void printnum(struct Num* head)
{
    struct Num* h=head;
    while(h!=NULL)
    {
        printf("%d ",h->num);
        h=h->next;
    }
    putchar('\n');
}

void freenum(struct Num* head)
{
    struct Num* h=head;
    while(h)
    {
        h=h->next;
        free(h);
    }
}

int main(void)
{
    struct Num* head=NULL;
    int num;
    
    while(1)
    {
        printf("please input a (int)number : \n");
        scanf("%d",&num);
        if(num==-1)
        {
            break;
        }
        else[/free]
        {
            sortnum(&head,num);
            printnum(head);
        }
    }
    
    char* c;//不做判断只调用程序正常,做了后下面直接return 0
    c=(char*)malloc(sizeof(char));
    printf("是否要反转(y/n)\n");
    scanf("%c",c);
   if(*c=='y')
   {
        printf("反转:\n");
        head=returnnum(head);
        printnum(head);
   }
    free(c);
    freenum(head);
    
    return 0;
}
最佳答案
2022-3-16 22:59:30
没问题
#include<stdio.h>
#include<stdlib.h>


int main(void)
{
    char *c;
    c=(char*)malloc(sizeof(char));

    printf("是否要反转(y/n)\n");
    scanf("%c", c);
   if(*c=='y')
   {
        printf("反转:\n");
   }

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-16 22:59:30 | 显示全部楼层    本楼为最佳答案   
没问题
#include<stdio.h>
#include<stdlib.h>


int main(void)
{
    char *c;
    c=(char*)malloc(sizeof(char));

    printf("是否要反转(y/n)\n");
    scanf("%c", c);
   if(*c=='y')
   {
        printf("反转:\n");
   }

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

可是程序就会跳过我下面注释往后的部分直接return 0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 12:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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