-
Notifications
You must be signed in to change notification settings - Fork 0
/
29-04
69 lines (69 loc) · 1.65 KB
/
29-04
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
#include <stdio.h>
int top = -1;
void printMenu(){
printf("\n1-Push\n");
printf("2-Pop\n");
printf("3-Peek\n");
printf("0-exit\n");
}
int push(int ele, int stack[], int n){
if(top == n - 1){
printf("\nCannot insert, no space");
return 0;
}
stack[++top] = ele;
return 1;
}
int peek(int stack[], int* flag){
if(top == -1){
*flag = 1;
printf("\nCannot read, since stack is empty");
return -1;
}
return stack[top];
}
int pop(int stack[], int* f){
if(top == -1){
*f = 1;
printf("\nCannot remove, since stack is empty");
return 0;
}
return stack[top--];
}
int main(){
int n; scanf("%d", &n);
int stack[n];
int choice = -1;
while(choice != 0){
printMenu();
scanf("%d", &choice);
int response;
switch(choice){
case 0:
break;
case 1:
int ele; printf("\nEnter the Ele = "); scanf("%d", &ele);
int response = push(ele, stack, n);
break;
case 2:
int f = 0;
response = pop(stack, &f);
if(f == 0){
printf("\nLast/Top most ele = %d", response);
}
break;
case 3:
int flag = 0;
response = peek(stack, &flag);
if(flag == 0){
printf("\nLast/Top most ele = %d", response);
}
break;
default :
printf("\nEnter valid Choice");
break;
}
}
printf("\nBye");
return 0;
}