-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.hpp
More file actions
151 lines (133 loc) · 3.37 KB
/
linkedList.hpp
File metadata and controls
151 lines (133 loc) · 3.37 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
/*
* linkedList.hpp
*
* Created on: 7 במרץ 2016
* Author: yonatanyeshanov
*/
#ifndef LINKEDLIST_HPP_
#define LINKEDLIST_HPP_
#include <cstdlib>
using std::cout;
using std::endl;
template <class T>
class listNode {
T* _data;
listNode<T>* _next;
public:
listNode<T>() : _data(new T), _next(NULL) {}
listNode<T>(const T& data) : _data(new T(data)), _next(NULL) {}
~listNode<T>() { delete _data; }
listNode<T>(listNode<T>& node) : _data(new T(node.get_data())), _next(NULL) {}
listNode<T>& operator=(const listNode<T>&) = delete;
const T& get_data() { return *_data; }
void set_data(const T& data) {
if (_data) {
delete _data;
}
_data = new T(data);
}
listNode<T>* get_next() { return _next; }
void set_next(listNode<T>* next) { _next = next; }
};
template <class T>
class linkedList {
// Dummy node - points to the first node.
listNode<T>* _head;
int _size;
listNode<T>* copy_node(listNode<T>* node) {
if (!node) {
return NULL;
}
listNode<T>* one = new listNode<T>(node->get_data());
listNode<T>* two = copy_node(node->get_next());
one->set_next(two);
return one;
}
public:
linkedList<T>() : _head(new listNode<T>()), _size(0) {}
linkedList<T>(linkedList& list) : _head(new listNode<T>()),
_size(list.get_size()) {
listNode<T>* node = list.get_first();
_head->set_next(copy_node(node));
}
~linkedList() {
listNode<T>* current = get_head();
while (current) {
listNode<T>* to_remove = current;
current = current->get_next();
delete to_remove;
}
}
// Insert to end of list.
void insert(const T& data) {
listNode<T>* node = get_head();
while (node->get_next()) {
node = node->get_next();
}
node->set_next(new listNode<T>(data));
_size++;
}
// Insert after a node.
void insert_after(listNode<T>* node, const T& data) {
listNode<T>* to_insert = new listNode<T>(data);
to_insert->set_next(node->get_next());
node->set_next(to_insert);
_size++;
}
// Assertion: each node has unique data (because the use of this list is
// for exporting a binSearchTree to it using <key,value> pairs which are
// unique in a dictionary).
listNode<T>* find(const T& data) {
listNode<T>* node = get_head();
while (node->get_next()) {
node = node->get_next();
if (node->get_data() == data) {
return node;
}
}
return NULL;
}
//TODO: make it merge-sort and NOT bubble-sort.
template <class COMPARATOR>
void sort(const COMPARATOR& compare) {
if (get_size() <= 1) {
return;
}
for (int i = 0; i < get_size(); i++) {
for (listNode<T>* curr = get_first(); curr->get_next();
curr = curr->get_next()) {
listNode<T>* next = curr->get_next();
if (!compare(curr->get_data(), next->get_data())) {
T temp = curr->get_data();
curr->set_data(next->get_data());
next->set_data(temp);
}
}
}
}
void remove(listNode<T>* node) {
if (!node) {
return;
}
listNode<T>* current = get_head();
while (current->get_next() != node) {
current = current->get_next();
}
current->set_next(node->get_next());
delete node;
_size--;
}
listNode<T>* get_head() { return _head; }
listNode<T>* get_first() { return _head->get_next(); }
int get_size() { return _size; }
void printList() {
listNode<T>* node = get_first();
cout << endl;
while (node) {
cout << node->get_data() << " ";
node = node->get_next();
}
cout << endl;
}
};
#endif /* LINKEDLIST_HPP_ */