-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack-Functions.cpp
More file actions
171 lines (154 loc) · 3.57 KB
/
Stack-Functions.cpp
File metadata and controls
171 lines (154 loc) · 3.57 KB
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
164
165
166
167
168
169
170
171
#include <iostream>
#include <string>
using namespace std;
class Stack
{
private:
int top;
int arr[10];
bool empty;
bool full;
public:
//---------------------------------------//
//-----------------STACK-----------------//
//---------------------------------------//
Stack()
{
top = -1;
for (int i = 0; i < 10; i++)
{
arr[i] = 0;
}
cout << "\n>>-------------------------------<<\n";
cout << ">----------STACK CREATED----------<\n";
cout << ">>-------------------------------<<\n\n";
}
//---------------------------------------//
//--------------EMPTYorFULL--------------//
//---------------------------------------//
void EMPTYorFULL()
{
if (top == -1)
{
cout << ">>STACK_EMPTY<<\n";
}
else if (top == 9)
{
cout << ">>STACK_FULL<<\n";
}
else
{
cout << ">>STACK_HAS_" << top + 1 << "_ELEMENTS<<\n";
}
}
//---------------------------------------//
//-----------------STATUS----------------//
//---------------------------------------//
void Status()
{
if (top == -1)
{
empty=true;
full=false;
}
else if (top == 9)
{
empty=false;
full=true;
}
else
{
empty=true;
full=true;
}
}
//---------------------------------------//
//----------------DISPLAY----------------//
//---------------------------------------//
void display()
{
cout << "\n------------------------------------\n STACK->>";
for (int i = 0; i <= 9; i++)
{
cout << " [" << arr[i] << "]";
}
cout << "\n------------------------------------\n";
}
//---------------------------------------//
//-----------------PUSH------------------//
//---------------------------------------//
void push(int n)
{
Status();
if (empty==true)
{
top++;
arr[top] = n;
cout << "-->[" << n << "] added to STACK..\n";
}
else
{
cout << "--> '" << n << "' cannot be added to STACK.\n";
}
}
//---------------------------------------//
//------------------POP------------------//
//---------------------------------------//
int pop()
{
Status();
if (full==true)
{
int n = arr[top];
arr[top] = 0;
top--;
cout<<"--> POPPED..!";
return n;
}
else
{
cout << "--> Cannot be popped out.\n";
return 0;
}
}
//---------------------------------------//
//------------------PEEK-----------------//
//---------------------------------------//
int peek(int pos){
Status();
if(full==true){
int n=arr[pos-1];
cout << "--> PEEK>> [";
return n;
}
else
{
cout << "--> Cannot be peeked at.\n";
return 0;
}
}
};
int main(int argc, char **argv)
{
Stack s;
s.EMPTYorFULL();
s.push(10);
s.push(20);
s.push(30);
s.EMPTYorFULL();
s.push(40);
s.push(50);
s.EMPTYorFULL();
s.push(60);
s.push(70);
s.push(80);
s.push(90);
s.push(100);
s.EMPTYorFULL();
s.push(110);
s.display();
cout<<s.peek(2)<<"]\n";
s.pop();
s.display();
return 0;
}