-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.hpp
180 lines (150 loc) · 4.97 KB
/
LinkedList.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
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
#pragma once
#include <iostream>
template <class T> class ListItem
{
private:
T value;
ListItem<T>* prev;
ListItem<T>* next;
public:
ListItem() : value(NULL), prev(NULL), next(NULL) {}
ListItem(T value) : value(value), prev(NULL), next(NULL) {}
public:
T GetValue() { return this->value; }
void SetValue(T value) { this->value = value; }
ListItem<T>* GetPrev() { return this->prev; }
ListItem<T>* GetNext() { return this->next; }
void SetPrev(ListItem<T>* ptr) { this->prev = ptr; }
void SetNext(ListItem<T>* ptr) { this->next = ptr; }
};
template <class T> class LinkedList {
private:
ListItem<T>* head;
ListItem<T>* tail;
size_t size;
public:
LinkedList (const T* items, size_t count) : size(count)
{
if(items == NULL || count == 0) throw "Linked list created from empty array";
ListItem<T>* tmp = new ListItem<T>(items[0]);
this->head = tmp;
for (int i = 1; i < count; i++)
{
ListItem<T>* item = new ListItem<T>(items[i]);
item->SetPrev(tmp);
tmp->SetNext(item);
tmp = item;
}
this->tail = tmp;
}
LinkedList() : head(NULL), tail(NULL), size(0) {}
LinkedList (const LinkedList <T>* list)
{
this->size = list->GetSize();
if(!this->size) {
this->head = NULL;
this->tail = NULL;
return;
}
ListItem<T>* temp = list->GetHead();
ListItem<T>* item = new ListItem<T>(temp->GetValue());
this->head = item;
while(temp->GetNext() != NULL) {
temp = temp->GetNext();
ListItem<T>* prev = item;
item = new ListItem<T>(temp->GetValue());
prev->SetNext(item);
item->SetPrev(prev);
}
this->tail = item;
}
~LinkedList()
{
ListItem<T>* item = this->head;
while (item != NULL) {
ListItem<T>* next = item->GetNext();
delete item;
item = next;
}
}
public:
ListItem<T>* GetHead() const { return this->head; }
size_t GetSize() const { return this->size; }
T GetFirst()
{
if(!this->size) throw std::out_of_range ("LinkedList is empty");
return this->head->GetValue();
}
T GetLast()
{
if(!this->size) throw std::out_of_range ("LinkedList is empty");
return this->tail->GetValue();
}
T Get(int index)
{
if (index < 0 || index >= this->size) throw std::out_of_range("LinkedList::Get(int) index out of range");
ListItem<T>* item = this->head;
ListItem<T>* next = item->GetNext();
for (int i = 0; i < index; i++) item = item->GetNext();
return item->GetValue();
}
LinkedList<T>* GetSubList(int startIndex, int endIndex)
{
if (startIndex < 0 || startIndex >= this->size || endIndex < 0 || endIndex >= this->size || startIndex >= endIndex) throw std::out_of_range("LinkedList::GetSubList() index out of range");
LinkedList<T>* sublist = new LinkedList<T>();
for (int i = startIndex; i < endIndex; i++) sublist->Append(this->Get(i));
return sublist;
}
void Prepend(T item) { this->InsertAt(0, item); }
void Append(T item) { this->InsertAt(this->size, item); }
void InsertAt(int index, T item)
{
if (index < 0 || index > this->size) throw std::out_of_range("LinkedList::InsertAt() index out of range");
ListItem<T>* newItem = new ListItem<T>(item);
if(!this->size) {
this->head = newItem;
this->tail = newItem;
this->size++;
return;
} else if(index == 0) {
newItem->SetNext(this->head);
this->head->SetPrev(newItem);
this->head = newItem;
} else if(index == this->size) {
this->tail->SetNext(newItem);
newItem->SetPrev(this->tail);
this->tail = newItem;
} else {
ListItem<T>* iterator = this->head;
for(int i = 0; i < index; i++) iterator = iterator->GetNext();
iterator->GetPrev()->SetNext(newItem);
newItem->SetPrev(iterator->GetPrev());
newItem->SetNext(iterator);
iterator->SetPrev(newItem);
}
this->size++;
}
void Remove(int index)
{
if(index >= this->size || index < 0) throw std::out_of_range("LinkedList::Remove() index out of range");
ListItem<T>* item = this->head;
for(int i = 0; i < index; i++) item = item->GetNext();
if(!index && this->size > 1) {
ListItem<T>* next = item->GetNext();
next->SetPrev(item->GetPrev());
this->head = next;
} else if (!index && this->size == 1) {
this->head = NULL;
this->tail = NULL;
} else if (index == this->size - 1) {
ListItem<T>* prev = item->GetPrev();
prev->SetNext(item->GetNext());
this->tail = prev;
} else {
(item->GetPrev())->SetNext(item->GetNext());
(item->GetNext())->SetPrev(item->GetPrev());
}
delete item;
this->size--;
}
};