-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.cpp
More file actions
63 lines (49 loc) · 989 Bytes
/
stack.cpp
File metadata and controls
63 lines (49 loc) · 989 Bytes
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
#include <iostream>
using namespace std;
struct stack {
typedef int value_type;
struct node {
value_type value;
node *prev;
node(value_type value) {
this->value = value;
prev = nullptr;
}
};
node *top;
stack() {
top = nullptr;
}
void push(value_type value) {
node *new_node = new node(value);
new_node->prev = top;
top = new_node;
}
value_type pop() {
if (empty()) {
throw "Can't pop";
}
value_type value = top->value;
node *old_top = top;
top = top->prev;
delete old_top;
return value;
}
bool empty() {
return top == nullptr;
}
};
int main() {
stack s;
size_t n;
cin >> n;
for (size_t i = 0; i < n; i++) {
int r;
cin >> r;
s.push(r);
}
while (!s.empty()) {
cout << s.pop() << ' ';
}
return 0;
}