forked from ucsb-cs24-mirza-s21/lab03_data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.hpp
125 lines (105 loc) · 2.76 KB
/
list.hpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef LIST_HPP_
#define LIST_HPP_
#include <cstddef>
#include <utility>
#include <iostream>
#include <vector>
template<typename T>
struct Node;
template<typename T>
class List{
private:
Node<T>* head;
Node<T>* tail;
public:
// Constructors/Assigners
List();
List(List const& o);
List(List&& o);
List& operator=(List const& o);
List& operator=(List&& o);
// Destructor
~List();
// Accessors
bool empty() const;
size_t size() const;
T& front() const;
T& back() const;
T& at(size_t index) const;
// Mutators
void clear();
void insert(size_t index, T const& value);
void pop_front();
void push_front(T const& value);
void pop_back();
void push_back(T const& value);
void erase(size_t index);
void erase(size_t first, size_t count);
// Equality Operator
template<typename C>
friend bool operator==(List<C> const& lhs, List<C> const& rhs);
// Debugging Tools
void render(std::ostream& out) const;
static size_t alloc_cnt(){return Node<T>::alloc_cnt;}
};
template<typename T>
void List<T>::render(std::ostream& out) const{
out<<'[';
if(this->head != NULL){
out<<this->head->val;
Node<T>* p = this->head->next;
while(p != NULL){
out<<','<<p->val;
p = p->next;
}
}
out<<']'<<'\n';
}
template<typename T>
std::ostream& operator<<(std::ostream& o, List<T> const& v){
v.render(o);
return o;
}
template<typename T>
struct Node{
T val;
Node* next;
static size_t alloc_cnt;
Node(T const& val, Node* const next = NULL);
Node(Node const& o);
Node(Node&& o);
~Node();
Node& operator=(Node const& o);
Node& operator=(Node&& o);
};
template<typename T>
Node<T>::Node(T const& value, Node<T>* const next_node):val(value),next(next_node){++alloc_cnt;}
template<typename T>
Node<T>::Node(Node<T> const& o):val(o.val),next(o.next == NULL ? NULL : new Node(*o.next)){++alloc_cnt;}
template<typename T>
Node<T>::Node(Node<T>&& o):val(std::move(o.val)),next(o.next){
o.next = NULL;
++alloc_cnt;
}
template<typename T>
Node<T>::~Node(){
if(this->next != NULL)
delete next;
--alloc_cnt;
}
template<typename T>
Node<T>& Node<T>::operator=(Node<T> const& o){
if(this->next != NULL) delete this->next;
this->val = o.val;
this->next = o.next == NULL ? NULL : new Node(*o.next);
return *this;
}
template<typename T>
Node<T>& Node<T>::operator=(Node<T>&& o){
if(this->next != NULL) delete this->next;
this->val = std::move(o.val);
this->next = o.next;
o.next = NULL;
return *this;
}
#endif //LIST_HPP_