-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.cpp
173 lines (153 loc) · 3.14 KB
/
linked-list.cpp
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
#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
template<class TYPE>
class LinkedList {
private:
struct Node {
TYPE value;
Node* link;
};
Node* first;
Node* last;
int size;
Node* createNode(TYPE value) {
Node* temp = new Node();
temp->Node::value = value;
temp->Node::link = nullptr;
return temp;
};
void init() {
first = nullptr;
last = nullptr;
size = 0;
}
public:
LinkedList() {
init();
};
LinkedList(const TYPE* arr_start, const TYPE* arr_end) {
init();
int n = arr_end - arr_start;
for (int i = 0; i < n; ++i) {
insert(arr_start[i]);
}
};
void insert(TYPE element) {
if (first == nullptr) {
first = createNode(element);
last = first;
}
else {
if (last == first) {
last = createNode(element);
if (size == 1)
first->Node::link = last;
}
else {
last->Node::link = createNode(element);
last = last->Node::link;
}
}
++size;
};
void insert(int index,TYPE element) {
Node* temp = createNode(element);
if(index==0){
temp->Node::link = first;
if (size == 1) {
last = first;
}
first = temp;
}
else {
Node* iter = first;
while (index != 1) {
iter = iter->Node::link;
--index;
}
temp->Node::link = iter->Node::link;
iter->Node::link = temp;
}
++size;
};
void removeAll() {
this->eraseAll(&first);
}
TYPE& get(int index) {
if (index == size - 1)
return last->Node::value;
else {
Node* temp = first;
while (index > 0) {
temp = temp->Node::link;
--index;
}
return temp->Node::value;
}
}
void swap(const int index1, const int index2) {
int idx = 0, found = 0;
Node** toSwap = (Node**) calloc(2,sizeof(Node*));
Node* temp = first;
while (found < 2 && idx < size) {
if (idx == index1 || idx == index2) {
toSwap[found++] = temp;
}
temp = temp->Node::link;
++idx;
}
if (found == 2) {
TYPE t = toSwap[0]->Node::value;
toSwap[0]->Node::value = toSwap[1]->Node::value;
toSwap[1]->Node::value = t;
}
}
TYPE& getFirst() {
return first->Node::value;
};
TYPE& getLast() {
return last->Node::value;
};
void print() {
for (Node* i = first; i != last->Node::link; i = i->Node::link) {
std::cout << i->Node::value << " ";
}
std::cout << std::endl;
};
void custom_print(void printFunction(TYPE& x)) {
for (Node* i = first; i != last->Node::link; i = i->Node::link) {
printFunction(i->Node::value);
}
std::cout << std::endl;
}
int getSize() {
return size;
}
void operator = (const LinkedList<TYPE>& list) {
if (this->size > 0) {
init();
}
if (list.first != nullptr) {
for (Node* i = list.first; i != list.last->Node::link; i = i->Node::link) {
this->insert(i->Node::value);
}
}
}
template<class T>
LinkedList<T> operator + (const LinkedList<T>& a) {
LinkedList<T>* temp = new LinkedList<T>();
if (this->first != nullptr) {
for (Node* i = this->first; i != this->last->Node::link; i = i->Node::link) {
temp->insert(i->Node::value);
}
}
if (a.first != nullptr) {
for (Node* i = a.first; i != a.last->Node::link; i = i->Node::link) {
temp->insert(i->Node::value);
}
}
return *temp;
}
};