-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
181 lines (155 loc) · 3.73 KB
/
LinkedList.h
File metadata and controls
181 lines (155 loc) · 3.73 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
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
#ifndef LINKEDLIST_HEADER
#define LINKEDLIST_HEADER
#include <functional>
template <class T>
class LinkedListNode
{
private:
T data;
LinkedListNode *prev;
LinkedListNode *next;
public:
LinkedListNode(T data, LinkedListNode *prev, LinkedListNode *next): data(data), prev(prev), next(next){}
LinkedListNode *getNext(){return this->next;}
LinkedListNode *getPrev(){return this->prev;}
T &getData(){return this->data;}
void setNext(LinkedListNode *next){this->next = next;}
void setPrev(LinkedListNode *prev){this->prev = prev;}
LinkedListNode &operator+(int n)
{
auto res = this;
for(int i = 0; i < n; i++)
{
if(res == nullptr)
break;
res = res->getNext();
}
return *res;
}
};
template <class T>
class LinkedList
{
private:
size_t size;
LinkedListNode<T> *head;
LinkedListNode<T> *tail;
public:
LinkedList();
~LinkedList();
LinkedList(const LinkedList &list);
bool is_empty();
bool contains(const T &value);
bool contains(std::function<bool(T)> function);
void push_tail(const T &data);
void push_head(const T &data);
LinkedList &operator=(const LinkedList &list);
T &find(std::function<bool(T)> function);
LinkedListNode<T> *getHead()
{
return this->head;
}
LinkedListNode<T> *getTail()
{
return this->tail;
}
};
template <typename T>
T &LinkedList<T>::find(std::function<bool(T)> function)
{
for(auto it = getTail(); it != nullptr; it = it->getNext())
if(function(it->getData()))
return it->getData();
}
template <typename T>
LinkedList<T>::LinkedList()
{
this->head = nullptr;
this->tail = nullptr;
}
template <typename T>
LinkedList<T>::LinkedList(const LinkedList &list)
{
for(auto it = list.getTail(); it != nullptr; it = it->getNext())
{
this.pushHead(it->getData());
}
}
template <typename T>
LinkedList<T> &LinkedList<T>::operator=(const LinkedList &list)
{
for(auto it = list.getTail(); it != nullptr; it = it->getNext())
{
this.pushHead(it->getData());
}
return this;
}
template <typename T>
LinkedList<T>::~LinkedList()
{
LinkedListNode<T> *iter_node = this->tail;
while(iter_node != this->head)
{
iter_node = iter_node->getNext();
delete iter_node->getPrev();
}
delete iter_node;
}
template <typename T>
bool LinkedList<T>::is_empty()
{
return this->head == nullptr;
}
template <typename T>
bool LinkedList<T>::contains(const T &value)
{
for(auto it = getTail(); it != nullptr; it = it->getNext())
{
if(it->getData() == value)
return true;
}
return false;
}
template <typename T>
bool LinkedList<T>::contains(std::function<bool(T)> func)
{
for(auto it = getTail(); it != nullptr; it = it->getNext())
{
if(func(it->getData()))
return true;
}
return false;
}
template <typename T>
void LinkedList<T>::push_tail(const T &data)
{
LinkedListNode<T> *node;
if(this->tail != nullptr)
{
node = new LinkedListNode<T>(data, nullptr, this->tail);
this->tail->setPrev(node);
this->tail = node;
}
else {
node = new LinkedListNode<T>(data, nullptr, nullptr);
this->tail = node;
this->head = node;
}
}
template <typename T>
void LinkedList<T>::push_head(const T &data)
{
LinkedListNode<T> *node;
if(this->head != nullptr)
{
node = new LinkedListNode<T>(data, this->head, nullptr);
this->head->setNext(node);
this->head = node;
}
else {
node = new LinkedListNode<T>(data, nullptr, nullptr);
this->tail = node;
this->head = node;
}
}
#endif