#include<stdio.h> #include<conio.h> #define max 5 void push(); int pop(); void display(); int stack[max]; int top=-1; void main() { int n,item,ch,ele; clrscr(); do { clrscr(); printf("1. Push Operation \n2. Pop Operation \n3. Display\n4. Exit\n\nEnter Your Choice : "); scanf("%d",&ch); switch(ch) { case 1: clrscr(); if(top==max-1) { printf("Stack Overflow !!!!!!!!!"); getch(); } else { printf("\nEnter Any Number : "); scanf("%d",&item); push(item); break; } case 2: if(top==-1) printf("Stack Underflow !!!!!!"); else ele = pop(); printf("Deletion Element is = %d",ele); getch(); break; case 3: display(); break; } }while(ch != 4); // getch(); } ///////////////////////// void push(int number) { top++; stack[top] = number; } int pop() { int num; if(top == -1) printf("Stack Underflow !!!!!!"); else num = stack[top]; top--; return num; } void display() { int i; clrscr(); if(top == -1) printf("Stack Undrflow !!!!!!!!"); else { printf("Stack Elements - \n"); for(i=top;i>=0;i--) { printf("%d\n",stack[i]); } } getch(); }
OUTPUT 1. 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 STACK OVERFLOW!!!!!!!!! 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 !!!!!!!!!