-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.cpp
77 lines (70 loc) · 1.6 KB
/
queue.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
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
using namespace std;
void file() {
freopen("queue.in", "r", stdin);
freopen("queue.out", "w", stdout);
}
template <typename T>
struct new_knot {
T x;
new_knot *next;
};
template <typename T>
class _queue{
private:
new_knot<T> *head, *tail;
public:
_queue():head(nullptr), tail(nullptr){};
void add(int x) {
new_knot<T> *tmp;
tmp = new new_knot<T>;
tmp -> x = x;
tmp -> next = nullptr; // [] -> [] -> [tmp] -> null
if(head != nullptr) {
tail -> next = tmp; // tail have address o tmp
tail = tmp; // move tail
}
else {
head = tail = tmp; // queue.empty()
}
}
void pop_forward() {
if(head != nullptr) {
new_knot<T> *out = head; // get head
cout << out -> x << endl; // print head element
head = head -> next; // move head
delete out;
}
}
T top() {
if(head != nullptr)
return head -> x;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
file();
_queue<int> my_queue;
int n; cin >> n;
while(n--) {
char xx; cin >> xx;
if(xx == '+') {
int x; cin >> x;
my_queue.add(x);
}
else {
my_queue.pop_forward();
}
}
}