鱼C论坛

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

关于小甲鱼视频中单链表的问题

[复制链接]
发表于 2018-3-9 19:50:57 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Piziaa 于 2018-3-9 19:58 编辑

为什么小甲鱼在linux中可以直接运行,而我使用dev c++和vs2017 运行后都会在输入一个数字+回车后,卡死.
内存和cpu也没用明显变化.
但是输入-1却可以直接退出.

这道题是s1e43中视频的题,是我代码哪写错了吗,我是按照小甲鱼的答案写的,也对照过.
#include <stdio.h>
#include <stdlib.h>

struct Node
{
        int value;
        struct Node *next;
};

void printNode(struct Node *head)
{
        struct Node *current;

        current = head;
        while (current != NULL)
        {
                printf("%d", current->value);
                current = current->next;
        }
        putchar('\n');
}

void insertNode(struct Node **head, int value)
{
        struct Node *previous;
        struct Node *current;
        struct Node *new;

        current = *head;
        previous = NULL;

        while (current != NULL && current->value < value)
        {
                previous = current;
                current = current->next;
        }

        new = (struct Node *)malloc(sizeof(struct Node));
        if (new = NULL)
        {
                printf("内存分配失败!\n");
                exit(1);
        }

        new->value = value;
        new->next = current;

        if (previous == NULL)
        {
                *head = new;
        }
        else
        {
                previous->next = new;
        }
}

int main(void)
{
        struct Node *head = NULL;
        int input;

        while (1)
        {
                printf("输入一个值(-1表示结束 ):");
                scanf_s("%d", &input);

                if (input == -1)
                {
                        break;
                }

                insertNode(&head, input);
                printNode(head);
        }

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

使用道具 举报

 楼主| 发表于 2018-3-9 19:52:34 | 显示全部楼层
还有想问一下:为什么编译产生的程序,如果是鼠标双击打开就会秒退?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-9 19:57:31 | 显示全部楼层
new不是关键字吗?为什么能直接使用?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-3-9 21:03:01 | 显示全部楼层
Piziaa 发表于 2018-3-9 19:57
new不是关键字吗?为什么能直接使用?

谁告诉你 new 是C语言的关键字?
这是C语言,不是C++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-10 21:07:31 | 显示全部楼层
人造人 发表于 2018-3-9 21:03
谁告诉你 new 是C语言的关键字?
这是C语言,不是C++

dev c++里新建源代码默认是cpp文件,new是c++里的关键字
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-11 12:55:39 | 显示全部楼层
统冠陶瓷 发表于 2018-5-10 21:07
dev c++里新建源代码默认是cpp文件,new是c++里的关键字

无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 23:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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