ADT
ast-experimental
Last change
on this file since 056bee8 was f4e3419d, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago |
restructure paper documents
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | #include "cpp-vstack.hpp"
|
---|
2 | #include <utility>
|
---|
3 |
|
---|
4 | stack::node::node( const object& v, node* n ) : value( v.new_copy() ), next( n ) {}
|
---|
5 |
|
---|
6 | void stack::copy(const stack& o) {
|
---|
7 | node** crnt = &head;
|
---|
8 | for ( node* next = o.head; next; next = next->next ) {
|
---|
9 | *crnt = new node{ *next->value };
|
---|
10 | crnt = &(*crnt)->next;
|
---|
11 | }
|
---|
12 | *crnt = nullptr;
|
---|
13 | }
|
---|
14 |
|
---|
15 | stack::stack() : head(nullptr) {}
|
---|
16 | stack::stack(const stack& o) { copy(o); }
|
---|
17 | stack::stack(stack&& o) : head(o.head) { o.head = nullptr; }
|
---|
18 | stack::~stack() { clear(); }
|
---|
19 |
|
---|
20 | stack& stack::operator= (const stack& o) {
|
---|
21 | if ( this == &o ) return *this;
|
---|
22 | clear();
|
---|
23 | copy(o);
|
---|
24 | return *this;
|
---|
25 | }
|
---|
26 |
|
---|
27 | stack& stack::operator= (stack&& o) {
|
---|
28 | if ( this == &o ) return *this;
|
---|
29 | head = o.head;
|
---|
30 | o.head = nullptr;
|
---|
31 | return *this;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void stack::clear() {
|
---|
35 | for ( node* next = head; next; ) {
|
---|
36 | node* crnt = next;
|
---|
37 | next = crnt->next;
|
---|
38 | delete crnt;
|
---|
39 | }
|
---|
40 | head = nullptr;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | bool stack::empty() const { return head == nullptr; }
|
---|
45 |
|
---|
46 | void stack::push(const object& value) { head = new node{ value, head }; /***/ }
|
---|
47 |
|
---|
48 | ptr<object> stack::pop() {
|
---|
49 | node* n = head;
|
---|
50 | head = n->next;
|
---|
51 | ptr<object> x = std::move(n->value);
|
---|
52 | delete n;
|
---|
53 | return x;
|
---|
54 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.