#include#include #define MAX 50 int size; typedef struct stack { int data[MAX]; int top; }stack; void init(stack *s) { s->top = -1; } int empty(stack *s) { if(s->top==-1) return(1); else return(0); } int full(stack *s) { if(s->top==size-1) return(1); else return(0); } void push(stack *s, int x) { s->top++; s->data[s->top] = x; } int pop(stack *s) { int x; x = s->data[s->top]; s->top--; return(x); } void display(stack *s) { int i; for (i = s->top; i >= 0; i--) printf("\n%d", s->data[i]); } int main() { int ch,val; stack s; init(&s); printf("Enter Stack Size :"); scanf("%d", &size); do { printf("\nSTACK PRIMITIVE OPERATIONS"); printf("\n1.PUSH"); printf("\n2.POP"); printf("\n3.DISPLAY"); printf("\n4.QUIT"); printf("\nEnter your option : "); scanf("%d", &ch); switch(ch) { case 1: if(full(&s)) { printf("Sorry STACK is Full(i.e.'STACK OVERFLOW')...."); } else { printf("\nEnter the element into stack:"); scanf("%d", &val); push(&s, val); } break; case 2: if(empty(&s)) { printf("Sorry STACK is Empty(i.e.'STACK UNDERFLOW')...."); } else { val = pop(&s); printf("\nThe element popped from stack is : %d",val); } break; case 3: if(empty(&s)) { printf("Sorry their is no any element in the STACK....."); } else { printf("\nThe current stack elements are:"); display(&s); } break; case 4:exit(0); default: printf("\nEnter correct option!Try again."); } }while(1); return 0; }
Output:
Enter Stack Size :3
STACK PRIMITIVE OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option : 1
Enter the element into stack:20
STACK PRIMITIVE OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option : 1
Enter the element into stack:30
STACK PRIMITIVE OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option : 3
The current stack elements are:
30
20
Enter the element into stack:30
STACK PRIMITIVE OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option : 2
The element popped from stack is : 30
STACK PRIMITIVE OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option : 2
The element popped from stack is : 20
STACK PRIMITIVE OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option : 4
Comments
Post a Comment