-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.cpp
More file actions
71 lines (55 loc) · 1.22 KB
/
queue.cpp
File metadata and controls
71 lines (55 loc) · 1.22 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
#include <iostream>
using namespace std;
struct queue {
typedef int value_type;
struct node {
value_type value;
node *next;
node(value_type value) {
this->value = value;
next = nullptr;
}
};
//! last може залишитись непорожнім, навіть якщо черга порожня
node *first, *last;
queue() {
first = nullptr;
last = nullptr;
}
void push(value_type value) {
node *new_node = new node(value);
if (empty()) {
first = new_node;
} else {
last->next = new_node;
}
last = new_node;
}
value_type pop() {
if (empty()) {
throw "Can't pop";
}
value_type value = first->value;
node *old_first = first;
first = first->next;
delete old_first;
return value;
}
bool empty() {
return first == nullptr;
}
};
int main() {
queue q;
size_t n;
cin >> n;
for (size_t i = 0; i < n; i++) {
int r;
cin >> r;
q.push(r);
}
while (!q.empty()) {
cout << q.pop() << ' ';
}
return 0;
}