forked from eceuwaterloo/ece650-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
107 lines (86 loc) · 1.87 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
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
// Compile: c++ -c -o stack.o stack.cpp -std=c++11
#include <iostream>
class Stack;
class StackEntry {
private:
int data;
StackEntry *next;
StackEntry(int v, StackEntry *n);
friend class Stack;
};
class Stack {
private:
int size;
StackEntry *top;
public:
Stack();
virtual ~Stack();
bool empty();
void push(int v);
int pop();
};
void print_stack(std::ostream &out, Stack *s);
StackEntry::StackEntry(int v, StackEntry *n) : data(v), next(n) {}
Stack::Stack() : size(0), top(nullptr) {}
Stack::~Stack() {
while (!empty()) {
pop();
}
}
bool Stack::empty() { return size == 0 || top == nullptr; }
void Stack::push(int v) {
StackEntry *newEntry = new StackEntry(v, top);
if (newEntry == nullptr) {
std::cerr << "[push()] out of memory\n";
return;
}
top = newEntry;
size++;
}
int Stack::pop() {
if (empty()) {
std::cerr << "Error: [pop()] stack is empty\n";
return 0;
}
StackEntry *popped = top;
int res = popped->data;
top = popped->next;
size--;
delete popped;
return res;
}
void print_stack(std::ostream &out, Stack *s) {
// same as (s!=nullptr) or (s!=NULL)
if (!s) {
out << "nil\n";
return;
}
// the only way to observe what is in the stack is to pop from it.
// to preserve the values, we store what was popped into a local
// stack, and then reconstruct the original stack.
// local storage
Stack local;
// print stack while popping
out << "[ ";
while (!s->empty()) {
int v = s->pop();
out << v << " ";
local.push(v);
}
out << "]\n";
// restore the stack from local copy
while (!local.empty()) {
s->push(local.pop());
}
}
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
TEST_CASE("stack example") {
Stack st;
st.push(5);
st.push(6);
print_stack(std::cout, &st);
CHECK(st.pop() == 6);
CHECK(st.pop() == 5);
CHECK(st.empty() == true);
}