/*C Program Stack Operation*/ #include<stdio.h> #include<conio.h> #define SIZE 5 int top=-1,S[SIZE]; void push() { int item; if(top==SIZE-1) printf("stack overflow \n"); else { printf(" enter the element \n"); scanf("%d",&item); top=top++; S[top]=item; } } void pop() { int x; if(top==-1) { printf("stack is underflow!\n"); return; } x=S[top]; printf("poped element=%d \n",x); top--; } void display() { int i; if(top==-1) { printf(" stack is underflow!\n"); return; } printf(" stack contents are:\n"); for(i=0; i<=top; i++) { printf("%d \n",S[i]); } } void main() { int ch; for(;;) { printf("1.PUSH\t2.POP\t3.DISPLAY\t4.EXIT\n"); printf("enter the choice: \n"); scanf("%d",&ch); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; default: exit(0); } } }
1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 1 enter the element 1 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 1 enter the element 2 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 1 enter the element 3 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 1 enter the element 4 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 1 enter the element 5 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 1 stack overflow 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 3 stack contents are: 1 2 3 4 5 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 2 poped element=5 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 2 poped element=4 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 2 poped element=3 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 2 poped element=2 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 2 poped element=1 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 2 stack is underflow! 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter the choice: 4
No comments:
Post a Comment