【发布时间】:2019-11-16 15:37:58
【问题描述】:
昨天我从一位新贡献者那里看到了this question,关于链表在 C 中的工作原理。由于我的回答可能对某人有所帮助,我决定重新发布问题并提供我的代码。
首先,我按照 Antti Haapala 和 Weather Vane 的建议修改了 OP 代码。以下是我的版本:
#include <stdio.h>
#include <stdlib.h>
typedef struct suppliers {
int idSUP;
int coonection;
int bill;
suppliers* next;
}suppliers;
void printlist(suppliers* h);
void insert(suppliers* head, int idSUP, int coonection, int bill);
void main() {
int idSUP;
int coonection;
int bill;
suppliers* head = NULL;
printf("please enter supplier data\n");
while (scanf("%d,%d,%d", &idSUP, &coonection, &bill) != EOF)
insert(head, idSUP, coonection, bill);
printlist(head);
}
void insert(suppliers* head, int idSUP, int coonection, int bill) {
suppliers* t, temp;
t = (struct suppliers*)malloc(sizeof(struct suppliers));
if (t == NULL) {
printf("ERROR");
}
t->idSUP = idSUP;
t->bill = bill;
t->coonection = coonection;
t->next = head;
head = t;
}
void printlist(suppliers* h) { while (h != NULL) { printf("%d", h->bill); h = h->next; } }
我输入了以下,但是在我输入 Ctrl+Z 后程序挂在了scanf。
please enter supplier data
1,1,1
^Z
我不怎么用scanf,所以不知道是什么问题。也许这与OP遇到的问题相同。我不知道。有谁知道为什么我上面链接的上一个问题的 OP 中的代码没有产生任何输出?
以下是我的建议修改。
【问题讨论】:
-
“挂”是什么意思?你的操作系统是什么?在 Linux / Unix 下 control-Z 停止当前进程
-
@bruno。我使用的是 Windows 10。在 Windows 上,Ctrl+Z 表示文件结束。当我说它“挂起”时,我无法再输入任何内容,也没有任何输出。
-
好吧,我知道这是个坏主意。对不起。如果没有其他人反对它,我将不胜感激。
-
这是正常的/预计您无法在 end 文件之后读取值。如果你害怕 DV 最好删除你的答案 ;-) (他们都不是来自我)
-
看起来你使用c++编译器来编译C。(和:
int main(void){})
标签: c data-structures linked-list stack