我创建了一个堆栈来解决一个问题。这是插入功能:
insert()
{
char data;
scanf("%c",&data);
struct node* newNode=create_node(data);//creates a new node
if(head==NULL)
{
head=newNode;
tail=head;
return;
}
newNode->next=head;
head=newNode;
return;
现在,我尝试像这样在堆栈中推送一些元素,
main()
{
char input[20];
insert();
insert();
insert();
insert();
insert();
print();
}
但是我注意到每次插入后的插入都被跳过了。所以当我应该取5时它只需要3 个输入..即,我在取 3 个输入后给出输出。
我能够通过在插入函数中添加一个fflush来解决这个问题。
我想知道导致这种情况的实际情况。 这是否意味着我们不能像我那样接受输入?