-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked list, stack, queue, bst, graph.cpp
276 lines (241 loc) · 7.05 KB
/
linked list, stack, queue, bst, graph.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#define NODE_SIZE 100
using namespace std;
struct Node {
int val = -1;
Node *next;
} nodePool[NODE_SIZE];
struct List {
private:
Node head = Node();
int nodeCnt = 0;
public:
int get(int _idx) {
Node *ptr = &head;
int idx = _idx;
while(ptr->next && idx >= 0) {
ptr = ptr->next;
idx--;
}
if (idx > -1) {
cout << "list is smaller than " << _idx << '\n';
return -1;
} else {
return ptr->val;
}
}
void push(int val) {
Node *ptr = &head;
while(ptr->next) {
ptr = ptr->next;
}
nodePool[nodeCnt].val = val;
ptr->next = &nodePool[nodeCnt++];
}
void print() {
if (!head.next) {
cout << "null list\n";
return;
}
Node *ptr = &head;
while(ptr->next) {
ptr = ptr->next;
cout << ptr->val << " ";
}
cout << '\n';
}
} list;
void listTest() {
cout << "================ list example ================\n";
list.print();
int temp;
for (int i = 0; i < 10; i++) {
temp = rand() % 100 + 1;
cout << "list push number : " << temp << '\n';
list.push(temp);
}
cout << "----------------------------------------------\n";
for (int i = 0; i < 10; i++) {
temp = rand() % 10;
cout << "list get " << temp <<" node : " << list.get(temp) << '\n';
}
cout << "====== list print all ======\n";
list.print();
cout << "================ END ================\n\n\n";
}
void stackTest() {
stack<int> st;
cout << "================ stack example ================\n";
int temp;
for (int i = 0; i < 10; i++) {
temp = rand() % 100 + 1;
cout << "stack push number : " << temp << '\n';
st.push(temp);
}
cout << "----------------------------------------------\n";
while (!st.empty()) {
cout << "stack pop number : " << st.top() << '\n';
st.pop();
}
cout << "================ END ================\n\n\n";
}
void queueTest() {
queue<int> que;
cout << "================ queue example ================\n";
int temp;
for (int i = 0; i < 10; i++) {
temp = rand() % 100 + 1;
cout << "queue push number : " << temp << '\n';
que.push(temp);
}
cout << "----------------------------------------------\n";
while (!que.empty()) {
cout << "queue pop number : " << que.front() << '\n';
que.pop();
}
cout << "================ END ================\n\n\n";
}
void priorityQueueTest() {
priority_queue<int> que;
cout << "================ priority queue example ================\n";
int temp;
for (int i = 0; i < 10; i++) {
temp = rand() % 100 + 1;
cout << "priority queue push number : " << temp << '\n';
que.push(temp);
}
cout << "----------------------------------------------\n";
while (!que.empty()) {
cout << "priority queue pop number : " << que.top() << '\n';
que.pop();
}
cout << "================ END ================\n\n\n";
}
struct TreeNode {
int val = -1;
TreeNode *left = nullptr, *right = nullptr;
} treeNodePool[100];
struct BST {
private:
TreeNode root = TreeNode();
int size = 0, idx;
int treeNodeCnt = 0;
void search(TreeNode *node) {
if (size < idx) {
return;
}
if (node->left != nullptr) search(node->left);
// if (node->val != -1) {
cout << "idx : " << idx++ << ", val : " << node->val << '\n';
// }
if (node->right != nullptr) search(node->right);
}
public:
void push(int val) {
size++;
TreeNode *ptr = &root;
while (1) {
if (ptr->val > val) {
if (ptr->left) {
ptr = ptr->left;
} else {
treeNodePool[treeNodeCnt].val = val;
ptr->left = &treeNodePool[treeNodeCnt++];
return;
}
} else {
if (ptr->right) {
ptr = ptr->right;
} else {
treeNodePool[treeNodeCnt].val = val;
ptr->right = &treeNodePool[treeNodeCnt++];
return;
}
}
}
}
void search() {
idx = 0;
if (root.right) {
search(root.right);
return;
}
cout << "null search tree\n";
}
} bst;
void binarySearchTreeExample() {
cout << "================ binary search tree example ================\n";
bst.search();
int temp;
for (int i = 0; i < 10; i++) {
temp = rand() % 100 + 1;
cout << "binary search tree push number : " << temp << '\n';
bst.push(temp);
}
cout << "====== binary search tree inorder search! ======\n";
bst.search();
cout << "================ END ================\n\n\n";
}
int board[5][5] = {
{0, 1, 0,0,0},
{0, 1, 0,1,0},
{0, 1, 0,1,0},
{0, 1, 0,1,0},
{0, 0, 0,1,0},
};
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
void graphExample() {
cout << "================ graph example - bfs maze ================\n";
cout << "======== MAZE ========\n";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << board[i][j] << " ";
}
cout << '\n';
}
bool check[5][5];
memset(check, false, sizeof (check));
queue<pair<pair<int, int>, int>> que;
pair<int, int> start = {0, 0};
pair<int, int> end = {4, 4};
que.push({start, 0});
check[start.first][start.second] = true;
pair<int, int> nodeNow, nextNode;
int distNow;
while (!que.empty()) {
nodeNow = que.front().first;
distNow = que.front().second;
que.pop();
if (nextNode.first == end.first && nextNode.second == end.second) {
cout << "arrived! " << end.first << ", " << end.second << " -> dist : " << distNow << '\n';
cout << "================ END ================\n";
return;
}
for (int i = 0; i < 4; i++) {
nextNode.first = nodeNow.first + dy[i];
nextNode.second = nodeNow.second + dx[i];
if (0 > nextNode.first || nextNode.first >= 5
|| 0 > nextNode.second || nextNode.second >= 5) continue;
if (check[nextNode.first][nextNode.second]) continue;
cout << nodeNow.first << ", " << nodeNow.second << ", dist : " << distNow << " -> " << distNow + 1 << '\n';
check[nextNode.first][nextNode.second] = true;
que.push({nextNode, distNow + 1});
}
}
cout << "can't reach to destination!\n";
cout << "================ END ================\n";
}
int main() {
srand((unsigned int) (time(0)));
listTest();
stackTest();
queueTest();
priorityQueueTest();
binarySearchTreeExample();
graphExample();
}