source: doc/papers/general/evaluation/cpp-stack.hpp @ e84382b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since e84382b was 3d8f2f8, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

more changes

  • Property mode set to 100644
File size: 1.0 KB
Line 
1#pragma once
2#include <utility>
3
4template<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        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 clear() {
17                for ( node * next = head; next; ) {
18                        node * crnt = next;
19                        next = crnt->next;
20                        delete crnt;
21                }
22                head = nullptr;
23        }
24
25        void copy( const stack<T> & o ) {
26                node ** crnt = &head;
27                for ( node * next = o.head; next; next = next->next ) {
28                        *crnt = new node{ next->value }; /***/
29                        crnt = &(*crnt)->next;
30                }
31                *crnt = nullptr;
32        }
33
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        bool empty() const { return head == nullptr; }
44
45        void push( const T & value ) { head = new node{ value, head };  /***/ }
46
47        T pop() {
48                node * n = head;
49                head = n->next;
50                T v = std::move( n->value );
51                delete n;
52                return v;
53        }
54};
Note: See TracBrowser for help on using the repository browser.