I was making a program to enter numbers into a stack and the do-while loop was automatically finished without waiting for my response. Hence only one data was taken and displayed.
#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;
}