-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.cpp
69 lines (64 loc) · 1.26 KB
/
stack.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
#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("stack.in", "r", stdin);
freopen("stack.out", "w", stdout);
}
template <typename T>
struct new_knot {
T x;
new_knot *next;
};
template <typename T>
class _stack {
private:
new_knot<T> *head;
public:
_stack():head(nullptr){};
void add(T x) {
new_knot<T> *tmp;
tmp = new new_knot<T>;
tmp -> x = x;
tmp -> next = head;
head = tmp;
}
void pop_back() {
if (head != nullptr) {
new_knot<T> *tmp = head;
cout << head -> x << endl;
head = head -> next;
delete tmp;
}
}
T top() {
if(head != nullptr)
return head -> x;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
file();
_stack<int> my_stack;
int n; cin >> n;
while(n--) {
char xx; cin >> xx;
if(xx == '+') {
int x; cin >> x;
my_stack.add(x);
}
else {
my_stack.pop_back();
}
}
}