ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change
on this file since 1e2de89 was
4c80a75,
checked in by Peter A. Buhr <pabuhr@…>, 7 years ago
|
update evaluation programs
|
-
Property mode set to
100644
|
File size:
974 bytes
|
Line | |
---|
1 | #pragma once |
---|
2 | #include <utility> |
---|
3 | |
---|
4 | template<typename T> struct stack { |
---|
5 | struct node { |
---|
6 | T value; |
---|
7 | node * next; |
---|
8 | node( const T & v, node * n = nullptr ) : value( v ), next( n ) {} |
---|
9 | }; |
---|
10 | node * head; |
---|
11 | |
---|
12 | void copy( const stack<T> & o ) { |
---|
13 | node ** cr = &head; |
---|
14 | for ( node * nx = o.head; nx; nx = nx->next ) { |
---|
15 | *cr = new node{ nx->value }; /***/ |
---|
16 | cr = &(*cr)->next; |
---|
17 | } |
---|
18 | *cr = nullptr; |
---|
19 | } |
---|
20 | |
---|
21 | void clear() { |
---|
22 | for ( node * nx = head; nx; ) { |
---|
23 | node * cr = nx; |
---|
24 | nx = cr->next; |
---|
25 | delete cr; |
---|
26 | } |
---|
27 | head = nullptr; |
---|
28 | } |
---|
29 | |
---|
30 | stack() : head( nullptr ) {} |
---|
31 | stack( const stack<T> & o ) { copy( o ); } |
---|
32 | ~stack() { clear(); } |
---|
33 | |
---|
34 | stack & operator=( const stack<T> & o ) { |
---|
35 | if ( this == &o ) return *this; |
---|
36 | clear(); |
---|
37 | copy( o ); |
---|
38 | return *this; |
---|
39 | } |
---|
40 | |
---|
41 | bool empty() const { |
---|
42 | return head == nullptr; |
---|
43 | } |
---|
44 | |
---|
45 | void push( const T & value ) { |
---|
46 | head = new node{ value, head }; /***/ |
---|
47 | } |
---|
48 | |
---|
49 | T pop() { |
---|
50 | node * n = head; |
---|
51 | head = n->next; |
---|
52 | T v = std::move( n->value ); |
---|
53 | delete n; |
---|
54 | return v; |
---|
55 | } |
---|
56 | }; |
---|
Note: See
TracBrowser
for help on using the repository browser.