-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuBasedStack.cpp
163 lines (157 loc) · 3.91 KB
/
menuBasedStack.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream>
using namespace std;
struct Stack{
//Size of stack
int size;
//Top most pointer which will initially be -1
int top = -1;
//An array which will have same size as stack
int *stackArray;
};
void push(Stack *st,int element);
int pop(Stack *st);
int peek(Stack st,int position);
int stackTop(Stack st);
bool isEmpty(Stack st);
bool isFull(Stack st);
bool Palindrome(Stack st);
struct Stack st;
void mainMenu(){
cout<<"\nWhich task you want to perform"<<endl;
cout<<"0 - Exit \n";
cout<<"1 - Push \n";
cout<<"2 - Pop \n";
cout<<"3 - Peek \n";
cout<<"4 - Stack Top \n";
cout<<"5 - Stack Is Empty? \n";
cout<<"6 - Stack Is Full? \n";
cout<<"7 - Palindrome \n";
cout<<"Enter Your Option: ";
int option;
cin>>option;
switch(option){
case 0:
break;
case 1:
st.stackArray = new int[st.size];
cout << "Enter Element to Pass: ";
int x;
cin >> x;
push(&st, x);
mainMenu();
break;
case 2:
cout<<"The Element Deleted Is: "<<pop(&st);
mainMenu();
break;
case 3:
cout<<"Enter Position to View At: ";
int position;
cin>>position;
cout<<"The Element at "<<position<<" Is: "<<peek(st,position);
mainMenu();
break;
case 4:
cout<<"The Element at Stack Top: "<<stackTop(st);
mainMenu();
break;
case 5:
if(isEmpty(st)) {
cout << "Stack Is Empty?: " << "true";
} else{
cout << "Stack Is Empty?: " << "false";
}
mainMenu();
break;
case 6:
if(isFull(st)) {
cout << "Stack Is Full?: " << "true";
} else{
cout << "Stack Is Full?: " << "false";
}
mainMenu();
break;
case 7:
if(Palindrome(st)){
cout << "Stack Is Palindrome: " << "true";
} else{
cout << "Stack Is Palindrome: " << "false";}
mainMenu();
break;
default:
cout<<"*** Invalid Option ***\n";
cout<<"*** Enter Again ***\n";
mainMenu();
}
}
int main(){
cout<<"Enter Stack Size: ";
cin>>st.size;
mainMenu();
}
void push(Stack *st,int element){
if(st->top==st->size-1){
cout<<"Stack Is Full (STACK-OVER-FLOW)";
}
else{
st->top++;
st->stackArray[st->top] = element;
}
}
int pop(Stack *st){
int x = -1;
if(st->top==-1){
cout<<"Stack Is Empty (STACK-UNDER-FLOW)";
}
else{
x = st->stackArray[st->top];
st->top--;
}
return x;
}
int peek(Stack st,int position){
int x = -1;
if(st.top-position+1<0){
cout<<"\n\n<<<<<<<<<<<<<< INVALID POSITION >>>>>>>>>>>>>>>\n\n";
}else{
x = st.stackArray[(st.top - position) + 1];
return x;
}
}
int stackTop(Stack st){
if(st.top==-1){
cout<<"Stack Is Empty (STACK-UNDER-FLOW)";
} else{
return st.stackArray[st.top];
}
}
bool isEmpty(Stack st){
if(st.top==-1){
return true;
} else{
return false;
}
}
bool isFull(Stack st){
if(st.top==st.size-1){
return true;
} else{
return false;
}
}
bool Palindrome(Stack st){
if(isEmpty(st)){
cout<<"Stack Is Empty (STACK-UNDER-FLOW)";
}
else {
int j = st.size-1;
for (int i = 0; i < st.size; i++) {
if(st.stackArray[i] != st.stackArray[j]){
return false;
} else{
j--;
}
}
}
return true;
}