How could I display stack when I insert x in to programs.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node* next;
} * top;
void popStack()
{
struct Node *temp, *var = top;
if (var == top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node* temp;
temp = (struct Node*)malloc(sizeof(struct Node));
temp->Data = value;
if (top == NULL)
{
top = temp;
top->next = NULL;
}
else
{
temp->next = top;
top = temp;
}
}
void display()
{
struct Node* var = top;
if (var != NULL)
{
printf("\nElements are as:\n");
while (var != NULL)
{
printf("\t%d\n", var->Data);
var = var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char* argv[])
{
printf(" Wellcome to Basic Stacking. \n");
top = NULL;
while (1)
{
when i insert "x" I want program to display stack and exit but it does not work after I insert x in this programs it will be infinite loop and don't display the stack and don't exit what should i do????.
char x ;
int value;
if (value != x)
{
printf("please enter Your Name:");
scanf("%d", &value);
push(value);
fflush(stdin);
display();
}
else
{
// popStack();
display();
break;
}
}
getch();
}