-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.hpp
More file actions
48 lines (39 loc) · 1.12 KB
/
Copy pathstack.hpp
File metadata and controls
48 lines (39 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
template <typename T> Stack<T>::Stack(const Stack<T> &s) {
head = new Node { *s.head }; // duplicate head
Node *_head { head }; // copy curr head
Node *__head { s.head }; // copy s.head
while ((__head = __head->next)) {
_head->next = new Node { *__head }; // duplicate
_head = _head->next;
}
}
template <typename T> Stack<T>::~Stack() {
std::cout << "stack destructor: ";
while (head) {
Node *next { head->next };
std::cout << head->item << ": ";
std::cout << &(head->item) << " ";
delete head;
head = next;
}
std::cout << std::endl;
}
template <typename T> void Stack<T>::push(const T &item) {
Node *node { new Node { item } };
node->next = head;
head = node;
}
template <typename T> T Stack<T>::pop() {
if (is_empty())
throw new std::logic_error { "Stack empty" };
T item { head->item };
Node *next { head->next };
delete head;
head = next;
return item;
}
template <typename T> Stack<T> &Stack<T>::operator=(const Stack<T> &s) {
Stack<T> _s { s };
std::swap(head, _s.head);
return *this;
}