menustack.c
76 linesc
DOWNLOAD
1// Aim: Program to perform Menu Driven operations on stack (Push, Pop, Display, Status).
2#include<stdio.h>
3int a[100],size,top;
4void push(){
5    int ele;
6    if(top==size-1){
7        printf("Stack is full\n");
8    }
9    else{
10        printf("Enter the element to be pushed :");
11        scanf("%d",&ele);
12        top++;
13        a[top]=ele;
14    }
15}
16void pop(){
17    if(top==-1){
18        printf("Stack is empty\n");
19    }
20    else{
21        printf("The element popped is %d\n",a[top]);
22        top--;
23    }
24}
25void display(){
26    int i;
27    if(top==-1){
28        printf("Stack is empty\n");
29    }
30    else{
31        printf("The elements of the stack are :\n");
32        for(i=top;i>=0;i--){
33            printf("%d\n",a[i]);
34        }
35    }
36}
37void status(){
38    if(top==-1){
39        printf("Stack is empty\n");
40    }
41    else if(top==size-1){
42        printf("Stack is full\n");
43    }
44    else{
45        printf("Stack is neither empty nor full\n");
46    }
47}
48void main(){
49    int ch;
50    printf("Enter the size of the stack :");
51    scanf("%d",&size);
52    top=-1;
53    do{
54        printf("\nCHOOSE THE OPERATION\n1.PUSH\n2.POP\n3.DISPLAY\n4.STATUS\n5.EXIT");
55        printf("\nEnter your choice :");
56        scanf("%d",&ch);
57        switch(ch){
58            case 1 :
59                push();
60                break;
61            case 2 :
62                pop();
63                break;
64            case 3 :
65                display();
66                break;
67            case 4 :
68                status();
69                break;
70            case 5 :
71                break;
72            default :
73                printf("Invalid choice\n");
74        }
75    }while(ch!=4);
76}