链表,在我想在begining添加开始时 添加元素,但它只能接受第一个元素,然后 treminate 它不接受其他元素什么是错用while循环什么问题呢?这个代码不链表的开头插入元素
#include <stdio.h>
typedef struct node_type {
int data; struct node_type *next;
} node;
typedef node* list;
void main() {
list head,temp; int n; char ch;
head = NULL;
printf("\n Enter the data:(y/n):");
scanf("%c", &ch);
while (ch == 'y' || ch == 'Y') {
printf("\n Enter Element:");
scanf("%d", &n);
temp = (list) malloc(sizeof(node));
temp->data = n;
temp->next = head;
head = temp;
printf("\n Enter more data:");
scanf("%c", &ch);
}
temp = head;
while (temp != NULL) {
printf("%d", temp->data);
temp = temp->next;
}
}
请注意,执行'fflush(stdin)'是* undefined behavior *。一些“标准”库将其添加为扩展名,但如果可能的话避免它。 –
你做了什么调试? –
由于缺少标点符号,无法解析问题...... *叹*。我觉得这是... – alk