gpt4 book ai didi

c - 程序自动终止,无需等待我的响应。为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:07 25 4
gpt4 key购买 nike

<分区>

我正在编写一个将数字输入堆栈的程序,do-while 循环自动完成,无需等待我的响应。因此只获取并显示了一个数据。

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};
typedef struct node NODE;

NODE *top = NULL;

void push(int x)
{
NODE *p;

p = (NODE*)malloc(sizeof(NODE));
p->data = x;
p->next = top;
top = p;
}

void display(void)
{
NODE *t;

t = top;
if(t == NULL)
{
printf("\nstack is empty");
}
else
{
while(t != NULL)
{
printf("%d ", t->data);
t = t->next;
}
}
}

int main(void)
{
int m;
char ans;

do
{
printf("\nEnter the no. to insert in stack: \n");
scanf("%d", &m);
push(m);

printf("\nDo you want to enter more data???\n");
scanf("%c", &ans);
} while(ans == 'y' || ans == 'Y'); // here after entering a value for variable 'm', the program terminates displaying the stack with one element.

display();
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com