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

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since aa22c60 was 4c80a75, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

update evaluation programs

  • Property mode set to 100644
File size: 974 bytes
RevLine 
[604e76d]1#pragma once
2#include <utility>
3
[81e8ab0]4template<typename T> struct stack {
[604e76d]5        struct node {
6                T value;
[81e8ab0]7                node * next;
8                node( const T & v, node * n = nullptr ) : value( v ), next( n ) {}
[604e76d]9        };
[81e8ab0]10        node * head;
11
[4c80a75]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        }
[604e76d]20
21        void clear() {
[ac4dad2]22                for ( node * nx = head; nx; ) {
23                        node * cr = nx;
24                        nx = cr->next;
25                        delete cr;
[604e76d]26                }
27                head = nullptr;
28        }
29
[4c80a75]30        stack() : head( nullptr ) {}
31        stack( const stack<T> & o ) { copy( o ); }
[604e76d]32        ~stack() { clear(); }
33
[4c80a75]34        stack & operator=( const stack<T> & o ) {
[604e76d]35                if ( this == &o ) return *this;
36                clear();
[81e8ab0]37                copy( o );
[604e76d]38                return *this;
39        }
40
[4c80a75]41        bool empty() const {
42                return head == nullptr;
43        }
[604e76d]44
[4c80a75]45        void push( const T & value ) {
46                head = new node{ value, head };  /***/
47        }
[604e76d]48
49        T pop() {
[81e8ab0]50                node * n = head;
[604e76d]51                head = n->next;
[81e8ab0]52                T v = std::move( n->value );
[604e76d]53                delete n;
[81e8ab0]54                return v;
[604e76d]55        }
56};
Note: See TracBrowser for help on using the repository browser.