PROGRAM by Ashish
#include<stdio.h> #include<conio.h> #define MAX 10 int stack[MAX]; int top=-1; void push(int num); void pop(); void traverse(); void main() { int num,ch,p=0; clrscr(); do { printf("Enter 1 to Print STACK\n"); printf("ENTER 2 to PUSH ITEM\n"); printf("ENTER 3 to POP ELEMENT\n"); scanf("%d",&ch); switch(ch) { case 1: { printf("\nElements in Stack are:-\t"); traverse(); break; } case 2: { printf("\nEnter the Element to be pushed"); scanf("%d",&num); push(num); break; } case 3: { printf("\nGoing to Perform POP OPERATION"); pop(); break; } } printf("\nENTER 1 to Continue"); scanf("%d",&p); }while(p==1); getch(); } void traverse() { int i; if(top==-1) { printf("\nStack is EMPTY"); } else { for(i=top;i>=0;i--) { printf("\n%d",stack[i]); } } } void push(int num) { if(top==MAX) { printf("\nOVERFLOW"); } else { top=top+1; stack[top]=num; printf("\n Element added"); } } void pop() { int item; if(top==-1) { printf("\nUnder FLow"); } else { item=stack[top]; top=top-1; printf("\Element poped is%d",item); } } |
Output:-