stack_using_structure
unknown
c_cpp
a year ago
2.3 kB
3
Indexable
#include <stdio.h> #include <stdlib.h> #define max_size 10 int n; struct Stack { int array[max_size]; int top; }stk; void initialiser(int n) { stk.top = -1; } int overflow() { if(stk.top >= n-1) { return(1); } return(0); } int underflow() { if(stk.top < 0) { return(1); } return(0); } void push(int value) { if(overflow()) { printf("\nCaution! Stack Overflow Condition!!!\n"); } else { stk.array[++(stk.top)] = value; printf("\n"); } } int pop() { if(underflow()) { printf("\nCaution! Stack Underflow Condition!!!\n"); } else { return(stk.array[(stk.top)--]); printf("\n"); } } int peek() { if(underflow()) { printf("\nCaution! Stack Underflow Condition!!!\n"); } else { return(stk.array[stk.top]); } } void display() { if(underflow()) { printf("\nStack is Empty!!!\n"); } else { printf("\nItems in the stack are:\n"); for(int i = stk.top; i >= 0; i--) { printf("%d ",stk.array[i]); } printf("\n"); } } void main() { int user_input,push_ele; printf("\nEnter the Stack size: "); scanf("%d",&n); initialiser(n); start: printf("\nSelect any of the following options:\n"); printf("PUSH - 1\nPOP - 2\nPEEK - 3\nDISPLAY - 4\nEXIT CODE - 5\n"); scanf("%d",&user_input); switch (user_input) { case 1: printf("\nEnter the element to be Pushed inside the Stack: "); scanf("%d",&push_ele); push(push_ele); goto start; break; case 2: printf("\nPoped element from the Stack: %d",pop()); printf("\n"); goto start; break; case 3: printf("\nElement on the Top of the Stack: %d",peek()); printf("\n"); goto start; break; case 4: display(); goto start; break; case 5: break; default: printf("\nPlease Select A Valid Option!!!"); goto start; break; } }
Editor is loading...