Stack Operation(Insert,Delete,Display)

Stack Operation(Insert,Delete,Display) Source Code: #include<stdio.h> #include<stdlib.h> int a[5],top=-1; void push(int item) { if(top==4) { printf("\nstack overflow........"); } else { top=top+1; a[top]=item; } } int pop() { int item; if(top==-1) { printf("\n\nStack underflow....."); item=-1; } else { item=a[top]; top=top-1; } return item; } void show() { int i; if(top==-1) { printf("\n stack empty..."); } else { printf("\nThe elements of the stack are: "); i=0; while(i<=4) { if(i<=top) printf(" %d",a[i]); else printf(" _"); i++; } printf("\n"); } } void main() { int ch,item; while(1) { printf("\n1.Push 2.Pop 3.Show 4.Exit"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter item:"); scanf("%d",&item); push(item); printf("\n----------------------------------------"); break; case 2: item=pop(); printf("\nThe popped item is-> %d",item); printf("\n----------------------------------------"); break; case 3: show(); printf("\n----------------------------------------"); break; case 4: exit(0); } } } /* OUTPUT : 1.Push 2.Pop 3.Show 4.Exit Enter your choice:1 Enter item:11 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:1 Enter item:22 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:1 Enter item:33 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:3 The elements of the stack are: 11 22 33 _ _ ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:1 Enter item:44 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:1 Enter item:55 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:3 The elements of the stack are: 11 22 33 44 55 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:1 Enter item:66 stack overflow........ ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:2 The popped item is-> 55 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:2 The popped item is-> 44 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:2 The popped item is-> 33 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:2 The popped item is-> 22 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:3 The elements of the stack are: 11 _ _ _ _ ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:2 The popped item is-> 11 ---------------------------------------- 1.Push 2.Pop 3.Show 4.Exit Enter your choice:2 Stack underflow..... ----------------------------------------

Share:

0 comments