-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuBasedDynamicStack.cpp
132 lines (127 loc) · 2.8 KB
/
menuBasedDynamicStack.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
#include<iostream>
using namespace std;
struct Stack{
int data;
struct Stack* next;
};
Stack* top = NULL;
void push(int element);
int pop();
void mainMenu();
int peek(int position);
bool isEmpty();
bool isFull();
int stackTop();
int main(){
mainMenu();
}
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<<"Enter Your Option: ";
int option;
cin>>option;
switch(option){
case 0:
break;
case 1:
cout<<"Enter A Element to Push: ";
int element;
cin>>element;
push(element);
break;
case 2:
cout<<"The Element Poped Is: "<<pop();
mainMenu();
break;
case 3:
cout<<"Enter Position to View At: ";
int position;
cin>>position;
cout<<"The Element Is: "<<peek(position);
mainMenu();
break;
case 4:
cout<<"Stack Top: "<<stackTop();
mainMenu();
break;
case 5:
cout<<"Stack Is Empty: : "<<isEmpty();
mainMenu();
break;
case 6:
cout<<"Stack Is Full: "<<isFull();
mainMenu();
break;
default:
cout<<"*** Invalid Option ***\n";
cout<<"*** Enter Again ***\n";
mainMenu();
}
}
void push(int element){
Stack *newNode = new Stack;
if(newNode==NULL){
cout<<"Stack Is Full (STACK-OVER-FLOW)";
}else {
newNode->data = element;
newNode->next = top;
top = newNode;
}
}
int pop(){
int x = -1;
Stack *p = top;
if(top==NULL){
cout<<"Stack Is Empty (STACK-UNDER-FLOW)";
}else{
top = top->next;
x = p->data;
delete p;
}
return x;
}
int peek(int position){
if(top==NULL){
cout<<"Stack Is Empty (STACK-UNDER-FLOW)";
}else{
Stack *p = top;
for(int i=0;p!=NULL && i<position-1;i++){
p = p->next;
}
if(p!=NULL){
return p->data;
}else{
return -1;
}
}
}
bool isEmpty(){
if(top==NULL){
return true;
}else{
return false;
}
}
bool isFull(){
//when p is not created then stack is full
Stack *p = new Stack;
if(p==NULL){
return true;
}else{
return false;
}
}
int stackTop(){
if(top==NULL){
return -1;
}else{
return top->data;
}
}