- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/evaluation/cpp-stack.hpp
r81e8ab0 rf86c8e5 2 2 #include <utility> 3 3 4 template<typename T> structstack {4 template<typename T> class stack { 5 5 struct node { 6 6 T value; 7 node * next; 8 node( const T & v, node * n = nullptr ) : value( v ), next( n ) {} 7 node* next; 8 9 node( const T& v, node* n = nullptr ) : value(v), next(n) {} 9 10 }; 10 node 11 node* head; 11 12 12 stack() : head( nullptr ) {} 13 stack( const stack<T> & o) { copy( o ); } 14 stack( stack<T> && o ) : head( o.head ) { o.head = nullptr; } 15 16 void copy( const stack<T> & o ) { 17 node ** crnt = &head; 18 for ( node * next = o.head; next; next = next->next ) { 13 void copy(const stack<T>& o) { 14 node** crnt = &head; 15 for ( node* next = o.head; next; next = next->next ) { 19 16 *crnt = new node{ next->value }; /***/ 20 17 crnt = &(*crnt)->next; … … 22 19 *crnt = nullptr; 23 20 } 24 21 public: 25 22 void clear() { 26 for ( node* next = head; next; ) {27 node 23 for ( node* next = head; next; ) { 24 node* crnt = next; 28 25 next = crnt->next; 29 26 delete crnt; … … 32 29 } 33 30 31 stack() : head(nullptr) {} 32 stack(const stack<T>& o) { copy(o); } 34 33 ~stack() { clear(); } 35 34 36 stack & operator= ( const stack<T> & o) {35 stack& operator= (const stack<T>& o) { 37 36 if ( this == &o ) return *this; 38 37 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; 38 copy(o); 47 39 return *this; 48 40 } … … 50 42 bool empty() const { return head == nullptr; } 51 43 52 void push( const T & value) { head = new node{ value, head }; /***/ }44 void push(const T& value) { head = new node{ value, head }; /***/ } 53 45 54 46 T pop() { 55 node 47 node* n = head; 56 48 head = n->next; 57 T v = std::move( n->value);49 T x = std::move(n->value); 58 50 delete n; 59 return v;51 return x; 60 52 } 61 53 };
Note: See TracChangeset
for help on using the changeset viewer.