-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.h
249 lines (228 loc) · 6.27 KB
/
LinkedList.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once
#include "utility.h"
#include <cstddef>
// Declaration
template <class T>
class LinkedList {
public:
LinkedList(T* items, const int& count);
LinkedList();
LinkedList(const LinkedList<T>& list);
T GetFirst() const;
T GetLast() const;
T Get(const int& index) const;
//LinkedList<T>* GetSubList(const int& startIndex, const int& endIndex) const;
int GetLength() const;
void Append(const T& item);
void Prepend(const T& item);
void InsertAt(const T& item, const int& index);
void Set(const T& item, const int& index);
void EraseAt(const int& index);
Node<T>* GetFirstPointer() const;
Node<T>* GetLastPointer() const;
Node<T>* GetPointer(int index) const;
LinkedList<T>* Concat(LinkedList<T> *list) const;
private:
Node<T>* first_;
Node<T>* last_;
size_t size_;
};
// Definition
template <class T>
LinkedList<T>::LinkedList(T *items, const int &count) : size_(count) {
if (size_ > 0) {
first_ = new Node<T>;
Node<T>* current_element = first_;
for (int i = 0; i < size_; i++) {
current_element->data = items[i];
if (i != size_-1) {
current_element->next = new Node<T>;
current_element = current_element->next;
}
else {
current_element->next = nullptr;
}
}
last_ = current_element;
}
else {
first_ = nullptr;
last_ = nullptr;
}
}
template <class T>
LinkedList<T>::LinkedList() : first_(nullptr), last_(nullptr), size_(0) {}
template <class T>
LinkedList<T>::LinkedList(const LinkedList<T>& list) : size_(list.GetLength()) {
if (size_ > 0) {
first_ = new Node<T>;
Node<T>* current_element = first_;
for (int i = 0; i < size_; i++) {
current_element->data = list.Get(i);
if (i != size_-1) {
current_element->next = new Node<T>;
current_element = current_element->next;
}
else {
current_element->next = nullptr;
}
}
last_ = current_element;
}
else {
first_ = nullptr;
last_ = nullptr;
}
}
template <class T>
T LinkedList<T>::GetFirst() const {
OutOfRangeCheck(0, size_);
return first_->data;
}
template <class T>
T LinkedList<T>::GetLast() const {
OutOfRangeCheck(0, size_);
return last_->data;
}
template <class T>
T LinkedList<T>::Get(const int& index) const {
OutOfRangeCheck(index, size_);
Node<T>* current_element = first_;
for (int i = 0; i < index; i++) {
current_element = current_element->next;
}
return current_element->data;
}
/*
template <class T>
LinkedList<T>* LinkedList<T>::GetSubList(const int& startIndex, const int& endIndex) const {}
*/
template <class T>
int LinkedList<T>::GetLength() const {
return size_;
}
template <class T>
void LinkedList<T>::Append(const T& item) {
if (first_ == nullptr) {
first_ = new Node<T>;
last_ = first_;
first_->data = item;
}
else {
last_->next = new Node<T>;
last_ = last_->next;
last_->data = item;
last_->next = nullptr;
}
size_++;
}
template <class T>
void LinkedList<T>::Prepend(const T& item) {
if (first_ == nullptr) {
first_ = new Node<T>;
last_ = first_;
first_->data = item;
}
else {
auto new_first_ = new Node<T>;
new_first_->next = first_;
new_first_->data = item;
first_ = new_first_;
}
size_++;
}
template <class T>
void LinkedList<T>::InsertAt(const T& item, const int& index) {
OutOfRangeCheck(index, size_);
if (first_ == nullptr) {
first_ = new Node<T>;
last_ = first_;
first_->data = item;
}
else if (index == 0) {
auto new_first_ = new Node<T>;
new_first_->next = first_;
new_first_->data = item;
first_ = new_first_;
}
else {
Node<T>* current_element = first_;
for (int i = 0; i < index-1; i++) {
current_element = current_element->next;
}
Node<T>* prev_element = current_element;
Node<T>* next_element = prev_element->next;
prev_element->next = new Node<T>;
prev_element->next->data = item;
prev_element->next->next = next_element;
}
size_++;
}
template<class T>
Node<T> *LinkedList<T>::GetFirstPointer() const {
return first_;
}
template<class T>
Node<T> *LinkedList<T>::GetLastPointer() const {
return last_;
}
template<class T>
LinkedList<T> *LinkedList<T>::Concat(LinkedList<T> *list) const {
auto new_list = new(LinkedList);
size_t size_1_ = list->GetLength();
for (size_t i = 0; i < size_; i++) {
new_list->Append(this->Get(i));
}
for (size_t i = 0; i < size_1_; i++) {
new_list->Append(list->Get(i));
}
return new_list;
}
template<class T>
void LinkedList<T>::EraseAt (const int &index) {
OutOfRangeCheck(index, size_);
if (first_ == last_) {
first_ = nullptr;
last_ = nullptr;
}
else if (index == 0) {
first_ = first_->next;
}
else if (index == size_-1) {
Node<T>* current_element = first_;
for (int i = 0; i < size_-2; i++) {
current_element = current_element->next;
}
Node<T>* new_last = current_element;
last_ = new_last;
}
else {
Node<T>* current_element = first_;
for (int i = 0; i < index-1; i++) {
current_element = current_element->next;
}
Node<T>* prev_element = current_element;
Node<T>* this_element = prev_element->next;
Node<T>* next_element = this_element->next;
prev_element->next = next_element;
}
size_--;
}
template<class T>
Node<T> *LinkedList<T>::GetPointer(int index) const {
OutOfRangeCheck(index, size_);
Node<T>* current_element = first_;
for (int i = 0; i < index; i++) {
current_element = current_element->next;
}
return current_element;
}
template<class T>
void LinkedList<T>::Set(const T &item, const int &index) {
OutOfRangeCheck(index, size_);
Node<T>* current_element = first_;
for (int i = 0; i < index; i++) {
current_element = current_element->next;
}
current_element->data = item;
}