forked from CodingWithAmit-07/Hacktoberfest-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Push_or_Pop_or_Display_the_stack.cpp
76 lines (63 loc) · 1.4 KB
/
Push_or_Pop_or_Display_the_stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#define MAX 100
int arr[MAX],n,i,top=-1,choice=0;
void push(){
printf("\n Enter an element to be pushed: ");
scanf("%d",&n);
top++;
arr[top] = n;
printf("\n");
}
void pop(){
if(top==-1){
printf("\n Underflow error! The stack is empty.");
}
else{
printf("\n The popped element is : %d",arr[top]);
top--;
}
printf("\n");
}
void display(){
if(top==-1){
printf("\n Underflow error! The stack is empty.");
}
else{
printf("\n-----Displaying stack-----");
printf("\n The elements of stack: ");
for(i=0;i<top+1;i++){
printf("%d, ",arr[i]);
}
}
printf("\n");
}
int main() {
// stack implementation
while(choice!=4){
printf("\n ----STACK IMPLEMENTATION------");
printf("\n 1.Push an element");
printf("\n 2. Pop stack");
printf("\n 3.Display stack");
printf("\n 4. End");
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Good Bye!");
break;
default:
printf("Wrong option!");
}
}
printf("\n The program has been terminated!");
return 0;
}