-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.h
207 lines (176 loc) · 4.58 KB
/
linked_list.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Created by Ben Roberts on 2024.
//
#pragma once
#include <cassert> // For std::assert
#include <cstddef> // For std::ptrdiff_t
#include <iterator> // For std::forward_iterator_tag
template <typename ValueType> class LinkedList {
private:
struct Node {
ValueType value;
Node *next;
Node *prev;
explicit Node(const ValueType &val)
: value(val), next(nullptr), prev(nullptr) {}
};
Node *head_;
Node *tail_;
size_t size_;
void clear() {
Node *current = head_;
while (current != nullptr) {
Node *tmp = current;
current = current->next;
delete tmp;
}
head_ = tail_ = nullptr;
size_ = 0;
}
public:
LinkedList() : head_(nullptr), tail_(nullptr), size_(0) {}
// Rule of 5: destructor, copy constructor, copy assignment, move constructor,
// move assignment
~LinkedList() { clear(); }
// Copy constructor
LinkedList(const LinkedList &other)
: head_(nullptr), tail_(nullptr), size_(0) {
for (const auto &value : other) {
pushBack(value);
}
}
// Copy assignment
LinkedList &operator=(const LinkedList &other) noexcept {
if (this != &other) {
clear();
for (const auto &value : other) {
pushBack(value);
}
}
return *this;
}
// Move constructor
LinkedList(const LinkedList &&other) noexcept
: head_(other.head_), tail_(other.tail_), size_(other.size_) {
// this != other guaranteed by the fact that the rvalue ref cannot be the
// same ref as this in std::move
other.head_ = nullptr;
other.tail_ = nullptr;
other.size_ = 0;
}
// Move assignment
LinkedList &operator=(LinkedList &&other) noexcept {
if (this != &other) {
clear();
// Transfer pointers from other
head_ = other.head_;
tail_ = other.tail_;
size_ = other.size_;
// Release other ownership
other.head_ = nullptr;
other.tail_ = nullptr;
other.size_ = 0;
}
return *this;
}
void pushFront(const ValueType &value) {
Node *new_node = new Node(value);
if (!head_) {
head_ = tail_ = new_node;
} else {
new_node->next = head_;
head_->prev = new_node;
head_ = new_node;
}
++size_;
}
void pushBack(const ValueType &value) {
Node *new_node = new Node(value);
if (!tail_) {
head_ = tail_ = new_node;
} else {
tail_->next = new_node;
new_node->prev = tail_;
tail_ = new_node;
}
++size_;
}
void popFront() {
assert(!empty());
auto temp = head_;
head_ = head_->next;
if (head_) {
head_->prev = nullptr;
} else {
tail_ = nullptr;
}
delete temp;
--size_;
}
void popBack() {
assert(!empty());
auto temp = tail_;
tail_ = tail_->prev;
if (tail_) {
tail_->next = nullptr;
} else {
head_ = nullptr;
}
delete temp;
--size_;
}
ValueType &front() {
assert(!empty());
return head_->value;
}
const ValueType &front() const {
assert(!empty());
return head_->value;
}
ValueType &back() {
assert(!empty());
return tail_->value;
}
const ValueType &back() const {
assert(!empty());
return tail_->value;
}
size_t size() const { return size_; }
bool empty() const { return size_ == 0; }
class Iterator {
private:
Node *current_;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = ValueType;
using difference_type = std::ptrdiff_t;
using pointer = ValueType *;
using reference = ValueType &;
explicit Iterator(Node *node) : current_(node) {}
reference operator*() const { return current_->value; }
pointer operator->() const { return &(current_->value); }
// Prefix increment
Iterator &operator++() {
if (current_) {
current_ = current_->next;
}
return *this;
}
// Postfix increment
Iterator operator++(int) {
Iterator temp = *this;
++(*this);
return temp;
}
bool operator==(const Iterator &other) const {
return current_ == other.current_;
}
bool operator!=(const Iterator &other) const {
return current_ != other.current_;
}
};
// Returns an iterator to the first element of the list.
Iterator begin() const { return Iterator(head_); }
// Returns an iterator to one past the last element of the list.
Iterator end() const { return Iterator(nullptr); }
};