有谁知道以下代码可能有什么问题?当我运行它时,我得到以下输出:
Insert a value in the list: 1
Do you want to continue? y/N:
1 ->
事实是 do-while 循环一直执行到scanf("%c", &ch)
语句,然后它跳出(所以我不能为ch
变量提供任何输入)。我尝试使用 GDB 进行调试,但收到了一些奇怪的消息:
GI___libc_malloc (bytes=16) at malloc.c:malloc.c: No such file or directory.
此外,它说编译器找不到该vscanf.c
文件。有人对这种奇怪的行为有解释吗?谢谢!(目的是以相反的顺序打印单链表的值。)
#include <stdio.h>
#include <stdlib.h>
struct node{
int info;
struct node* next;
};
struct node* head = 0;
void add_node(int value){
struct node* current = malloc(sizeof(struct node));
current->info = value;
current->next = head;
head = current;
}
void print_node(struct node* head){
while(head){
printf(" %d -> ", head->info);
head = head->next;
}
printf("\n");
}
int main(void){
int val;
char ch;
do {
printf("Insert a value in the list: ");
scanf("%d", &val);
add_node(val);
printf("Do you want to continue? y/N: ");
scanf("%c", &ch);
} while(ch == 'y' || ch == 'Y');
printf("\n");
print_node(head);
return 0;
}