ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change
on this file since 6926a6d was
f4e3419d,
checked in by Peter A. Buhr <pabuhr@…>, 7 years ago
|
restructure paper documents
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | #pragma once |
---|
2 | #include <utility> |
---|
3 | |
---|
4 | template<typename T> class stack { |
---|
5 | struct node { |
---|
6 | T value; |
---|
7 | node* next; |
---|
8 | |
---|
9 | node( const T& v, node* n = nullptr ) : value(v), next(n) {} |
---|
10 | }; |
---|
11 | node* head; |
---|
12 | |
---|
13 | void copy(const stack<T>& o) { |
---|
14 | node** crnt = &head; |
---|
15 | for ( node* next = o.head; next; next = next->next ) { |
---|
16 | *crnt = new node{ next->value }; /***/ |
---|
17 | crnt = &(*crnt)->next; |
---|
18 | } |
---|
19 | *crnt = nullptr; |
---|
20 | } |
---|
21 | public: |
---|
22 | void clear() { |
---|
23 | for ( node* next = head; next; ) { |
---|
24 | node* crnt = next; |
---|
25 | next = crnt->next; |
---|
26 | delete crnt; |
---|
27 | } |
---|
28 | head = nullptr; |
---|
29 | } |
---|
30 | |
---|
31 | stack() : head(nullptr) {} |
---|
32 | stack(const stack<T>& o) { copy(o); } |
---|
33 | stack(stack<T>&& o) : head(o.head) { o.head = nullptr; } |
---|
34 | ~stack() { clear(); } |
---|
35 | |
---|
36 | stack& operator= (const stack<T>& o) { |
---|
37 | if ( this == &o ) return *this; |
---|
38 | clear(); |
---|
39 | copy(o); |
---|
40 | return *this; |
---|
41 | } |
---|
42 | |
---|
43 | stack& operator= (stack<T>&& o) { |
---|
44 | if ( this == &o ) return *this; |
---|
45 | head = o.head; |
---|
46 | o.head = nullptr; |
---|
47 | return *this; |
---|
48 | } |
---|
49 | |
---|
50 | bool empty() const { return head == nullptr; } |
---|
51 | |
---|
52 | void push(const T& value) { head = new node{ value, head }; /***/ } |
---|
53 | |
---|
54 | T pop() { |
---|
55 | node* n = head; |
---|
56 | head = n->next; |
---|
57 | T x = std::move(n->value); |
---|
58 | delete n; |
---|
59 | return x; |
---|
60 | } |
---|
61 | }; |
---|
Note: See
TracBrowser
for help on using the repository browser.