|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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;
- }
复制代码 |
|