-
Notifications
You must be signed in to change notification settings - Fork 0
/
24-04
103 lines (97 loc) · 2.42 KB
/
24-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
bool ispar(string x){
if(x.length() & 1){
return false;
}
stack<char> s;
for(int i = 0; i < x.length(); i++){
if(x[i] == '(' or x[i] == '{' or x[i] == '['){
s.push(x[i]);
}
else{
if(s.empty()){
return false;
}
char top = s.top();
s.pop();
if(x[i] == ')' and top == '('){
}
else if(x[i] == '}' and top == '{'){
}
else if(x[i] == ']' and top == '['){
}
else{
return false;
}
}
}
if(s.size() != 0){
return false;
}
return true;
}
#include <stdio.h>
int top = -1;
void printMenu(){
printf("1-Push");
printf("\n2-Pop");
printf("\n3-Peek");
printf("\n0-Exit");
}
int peek(int stack[]){
if(top == -1){
printf("Stack is Empty cannot read\n");
return -1;
}
return stack[top];
}
int pop(int stack[]){
if(top == -1){
printf("Cannot delete since its underflow\n");
return -1;
}
return stack[top--];
}
int push(int stack[], int newData, int N){
if(top == N - 1){
printf("Cannot add data since its overflow\n");
return 0;
}
stack[++top] = newData;
return 1;
}
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:
printf("\nEnter Data Value : ");
int d; scanf("%d", &d);
response = push(stack, d, n);
break;
case 2:
response = pop(stack);
if(response != -1){
printf("\nRemoved Element = %d", response);
}
break;
case 3:
response = peek(stack);
if(response != -1){
printf("\nTop Element = %d", response);
}
break;
default :
printf("Kind;y enter the valid Choice");
break;
}
}
printf("\nBye");
return 0;
}