#include<stdio.h> #include<conio.h> #include<malloc.h> struct stack { int data; struct stack *next; };struct stack *nw, *temp, *top; void push() { int item; clrscr(); printf("Enter Element : "); scanf("%d",&item); if(top->data == NULL) top->data = item; else { nw = (struct stack *)malloc(sizeof(struct stack)); nw -> data = item; nw -> next = top; top = nw; } } void pop() { clrscr(); if(top->data == NULL) printf("\nSTACK UNDERFLOW ! ! !"); else { temp = top; top = top -> next; printf("%d is element deleted . . .",temp->data); free(temp); } } void display() { if(top->data == NULL) printf("STACK UNDERFLOW . . ."); else { temp = top; while(temp != NULL) { printf("Your Stack Element : %d\n",temp->data); temp = temp->next; } } } void main() { int ch; nw = (struct stack *)malloc(sizeof(struct stack)); nw -> data = NULL; nw -> next = NULL; top = nw; do { clrscr(); printf("1. Push Operation\n2. Pop Operation\n3. Display\n4. Exit\nEnter Any number : "); scanf("%d",&ch); switch(ch) { case 1: push(); break; case 2: pop(); getch(); break; case 3: clrscr(); display(); getch(); break; } }while(ch != 4); }
OUTPUT1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 1 Enter any Number : 10 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 1 Enter any Number : 20 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 1 Enter any Number : 30 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 1 Enter any Number : 40 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 1 Enter any Number : 50 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 3 Stack Element - 50 40 30 20 10 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 2 Deletion Element is = 50 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 2 Deletion Element is = 40 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 2 Deletion Element is = 30 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 2 Deletion Element is = 20 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 2 Deletion Element is = 10 1. Push Operation 2. Pop Operation 3. Display 4. Exit Enter Your Choice : 2 STACK UNDERFLOW !!!!!!!!!