-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.h
More file actions
72 lines (56 loc) · 1.06 KB
/
Stack.h
File metadata and controls
72 lines (56 loc) · 1.06 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
72
#ifndef STACK_H
#define STACK_H
#include <utility>
template <typename Object>
class Stack {
public:
// disallow copying and moving stacks
Stack() : s(0), top(NULL) {}
Stack(const Stack &) = delete;
Stack(Stack &&) = delete;
Stack & operator=(const Stack &) = delete;
Stack & operator=(Stack &&) = delete;
~Stack() {
while (!empty())
pop();
}
const Object & peek() const {
return top->data;
}
Object pop() {
Node * a = top;
top = top->next;
Object temp = std::move(a->data);
delete a;
--s;
return temp;
}
void push(const Object & d) {
Node * temp = new Node(d, top);
top = temp;
++s;
}
void push(Object && d) {
Node * temp = new Node(d, top);
top = temp;
++s;
}
bool empty() const {
return s == 0;
}
int size() const {
return s;
}
private:
struct Node {
Object data;
Node *next;
Node(const Object & d = Object(), Node * p = NULL)
: data(d), next(p) {}
Node(Object && d, Node * p = NULL)
: data(std::move(d)), next(p) {}
};
Node *top;
int s;
};
#endif