鱼C论坛

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

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

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

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

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

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

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

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

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

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

  8. void printNode(struct Node *head)
  9. {
  10.         struct Node *current;

  11.         current = head;
  12.         while (current != NULL)
  13.         {
  14.                 printf("%d", current->value);
  15.                 current = current->next;
  16.         }
  17.         putchar('\n');
  18. }

  19. void insertNode(struct Node **head, int value)
  20. {
  21.         struct Node *previous;
  22.         struct Node *current;
  23.         struct Node *new;

  24.         current = *head;
  25.         previous = NULL;

  26.         while (current != NULL && current->value < value)
  27.         {
  28.                 previous = current;
  29.                 current = current->next;
  30.         }

  31.         new = (struct Node *)malloc(sizeof(struct Node));
  32.         if (new = NULL)
  33.         {
  34.                 printf("内存分配失败!\n");
  35.                 exit(1);
  36.         }

  37.         new->value = value;
  38.         new->next = current;

  39.         if (previous == NULL)
  40.         {
  41.                 *head = new;
  42.         }
  43.         else
  44.         {
  45.                 previous->next = new;
  46.         }
  47. }

  48. int main(void)
  49. {
  50.         struct Node *head = NULL;
  51.         int input;

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

  56.                 if (input == -1)
  57.                 {
  58.                         break;
  59.                 }

  60.                 insertNode(&head, input);
  61.                 printNode(head);
  62.         }

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

使用道具 举报

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

使用道具 举报

 楼主| 发表于 2018-3-9 19:57:31 | 显示全部楼层
new不是关键字吗?为什么能直接使用?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

谁告诉你 new 是C语言的关键字?
这是C语言,不是C++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

dev c++里新建源代码默认是cpp文件,new是c++里的关键字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

无标题.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 10:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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