-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Stack.cpp
More file actions
216 lines (209 loc) · 5.56 KB
/
Array_Stack.cpp
File metadata and controls
216 lines (209 loc) · 5.56 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <bits/stdc++.h>
using namespace std;
class STACK
{
private:
int size;
int top;
int *Data;
public:
STACK(){};
void KhoiTao(int size);
bool IsEmpty();
bool IsFull();
void Push(int &value);
bool Pop(int &value);
bool Top(int &value);
void Output();
void Delete();
};
void STACK::KhoiTao(int size)
{
this->size = size; // Khoi tao size cho STACK
Data = new int[size]; // Cap phat mang
this->top = -1; // Khai bao chi so dau tien cua STACK dang rong la -1
}
bool STACK::IsEmpty()
{
if (top == -1)
{
return true; // Neu chi so ngan xep dau tien cua STACK la -1 thi STACK rong
}
return false;
}
bool STACK::IsFull()
{
if (top >= size - 1)
{
return true; // Neu chi so cua STACK bang size - 1 thi STACK da day
}
return false;
}
void STACK::Push(int &value)
{
if (IsFull())
{
return; // Neu STACK da day thi khong the them
}
// Nguoc lai
top++; // Tang chi so STACK len
Data[top] = value; // Gan vi tri tang len = value
}
bool STACK::Pop(int &value)
{
if (IsEmpty())
{
return false; // Neu STACK rong thi ko the lay duoc gia tri
}
// Nguoc lai
value = Data[top]; // Cho gia tri value bang gia tri cua Ngan xep o vi tri dinh top
top--; // Giam chi so cua STACK di
return true; // Tra ve kq true vi STACK ko rong, co the lay duoc gia tri o top
}
bool STACK::Top(int &value)
{
if (IsEmpty())
{
return false; // Neu STACK rong thi ko the lay duoc gia tri
}
value = Data[top]; // Cho gia tri value bang gia tri cua Ngan xep o vi tri dinh top
return true; // Tra ve kq true vi STACK ko rong, co the lay duoc gia tri o top
}
void STACK::Output()
{
if (IsEmpty())
{
return; // Neu STACK rong => thoat
}
while (!(IsEmpty()))
{
cout << Data[top] << " "; // In ra gia tri o dinh STACk
top--; // Giam chi so cua dinh STACK
}
}
void STACK::Delete()
{
delete[] Data; // Xoa cap phat mang dong
}
void MENU(STACK s)
{
string choice;
while (1)
{
system("cls");
cout << "\t\t===========================MENU=============================";
cout << "\n\t\t| MOI BAN NHAP TUY CHON |";
cout << "\n\t\t| 1. Xuat STACK ra man hinh |";
cout << "\n\t\t| 2. Them gia tri vao STACK |";
cout << "\n\t\t| 3. Lay phan tu dau STACK dong thoi xoa di (Push) |";
cout << "\n\t\t| 4. Lay phan tu dau STACK va khong xoa (Top) |";
cout << "\n\t\t| 5. Kiem tra STACK co rong hay khong |";
cout << "\n\t\t| 6. Kiem tra STACK da day hay chua |";
cout << "\n\t\t| 0. Thoat Menu va Xoa bo nho STACK |";
cout << "\n\t\t============================================================";
cout << "\n\t\tNhap lua chon: ";
cin >> choice;
if (choice == "0")
{
cout << "\t\tNhan ENTER de thoat\n\t\t";
s.Delete();
system("pause");
return;
}
else if (choice == "1")
{
if (!s.IsEmpty())
{
cout << "\t\tCac phan tu trong STACK: ";
s.Output();
cout << "\n\t\t";
}
else
{
cout << "\t\tSTACK dang rong\n\t\t";
}
system("pause");
}
else if (choice == "2")
{
if (s.IsFull())
{
cout << "\t\tSTACK da day, khong the them!\n\t\t";
system("pause");
continue;
}
int value;
cout << "\t\tNhap gia tri muon them vao STACK: ";
cin >> value;
s.Push(value);
}
else if (choice == "3")
{
int value;
if (!s.Pop(value))
{
cout << "\t\tSTACK dang rong!\n\t\t";
system("pause");
}
else
{
cout << "\t\tPhan tu o dinh STACK la: " << value << "\n\t\t";
system("pause");
}
}
else if (choice == "4")
{
int value;
if (!s.Top(value))
{
cout << "\t\tSTACK dang rong!\n\t\t";
system("pause");
}
else
{
cout << "\t\tPhan tu o dinh STACK la: " << value << "\n\t\t";
system("pause");
}
}
else if (choice == "5")
{
if (s.IsEmpty())
{
cout << "\t\tSTACK dang rong\n\t\t";
system("pause");
}
else
{
cout << "\t\tSTACK dang chua phan tu\n\t\t";
system("pause");
}
}
else if (choice == "6")
{
if (s.IsFull())
{
cout << "\t\tSTACK da day\n\t\t";
system("pause");
}
else
{
cout << "\t\tSTACK chua day\n\t\t";
system("pause");
}
}
else
{
cout << "\t\tBan da nhap sai cu phap\n\t\t";
system("pause");
}
}
}
int main()
{
STACK s;
int size;
cout << "\t\tNhap kich thuoc cho STACK: ";
cin >> size;
s.KhoiTao(size);
MENU(s);
}