#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *top = NULL;
int push(int );
int pop ();
void show();
main()
{
int x,opn;
do
{
printf("\n\n\t <1>PUSH <2>POP <3>SHOW <4>EXIT");
printf("\n\t Enter your option : ");
scanf("%d", &opn);
switch(opn)
{
case 1: printf("\t Enter number to push : ");
scanf("%d", &x);
if( push(x))
printf("\n\t ERROR : OVERFLOW !!!");
printf("\t Press any key to continue ...\n");
getch();
break;
case 2: if( top == NULL)
printf("\n\t ERROR : UNDERFLOW !!!");
else
printf("\t poped number is : %d ", pop());
printf("\t Press any key to continue ...\n");
getch();
break;
case 3: if( top == NULL)
printf("\n\t STACK IS EMPTY");
else
show();
printf("\t Press any key to continue ...\n");
getch();
break;
}
}while( opn != 4);
}
int push(int x)
{
struct Node *newNode = malloc(sizeof(struct Node));
if( newNode == NULL )
return -1; newNode->data = x;
newNode->link = top;
top = newNode;
return 0;
}
int pop ()
{
int x;
struct Node *tmpNode = top;
if(top == NULL)
return -1;
x = top->data;
top = top->link;
free(tmpNode);
return x;
}
void show ( )
{
struct Node *tmpNode = top;
if(top == NULL)
return ;
printf("\t STACK = [ ");
for( ; tmpNode != NULL ; tmpNode = tmpNode->link )
printf(" %d ",tmpNode->data);
printf("]");
}
Output