-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
333 lines (292 loc) · 6.98 KB
/
Copy pathLinkedList.cpp
File metadata and controls
333 lines (292 loc) · 6.98 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "LinkedList.h"
static string emptyString = "";
static Node* emptyNode = new Node("");
LinkedList::LinkedList() :head(nullptr)
{
}
LinkedList::LinkedList(const LinkedList& other)
{
Node* Temp;
if (other.head != nullptr)
{
this->head = new Node(other.head->data); // YENİ HEAD OLUŞTURDU
Node* wc = this->head; // WALK1 HEADİMİZ OLDU
Node* w = other.head->next; // WALK2 OTHER HEADİNDEN DEVAM ETTİ
while (w != nullptr) {
wc->next = new Node(w->data); // WALK1İN NEXTİ YENİ WALK2NİN DATASI OLDU
w = w->next; // WALK2 NEXTE GEÇİLDİ
wc = wc->next; //WALK1, WALK1İN NEXTİNE GEÇİLDİ
}
}
}
LinkedList::~LinkedList()
{
this->clear();
}
string& LinkedList::at(int index) { //TAMAM
//1. İndex sayısını kontrol et 2.başlangıç null mu kontrol et 3. Gidene kadar null mu kontrol et
Node* walk;
if (index < 0) return emptyString;
if (this->head == nullptr) return emptyString;
walk = this->head;
int count = 0;
while (walk != nullptr) {
if (index == count)return walk->data;
count++;
walk = walk->next;
}
return emptyString;
}
string& LinkedList::front() { //TAMAM
if (this->head != nullptr)
{
return head->data;
}
return emptyString;
}
string& LinkedList::back() { //TAMAM
if (this->head == nullptr) return emptyString;
if (this->head->next == nullptr) return this->head->data;
Node* walk = this->head;
while (walk->next != nullptr) {
walk = walk->next;
}
return walk->data;
}
void LinkedList::push_back(const string& value) { //TAMAM
Node* newNode = new Node(value);
if (this->head == nullptr)
{
this->head = newNode;
return;
}
Node* walk = this->head;
if (walk->next == nullptr) {
walk->next = newNode;
return;
}
while (walk->next != nullptr) {
walk = walk->next;
}
walk->next = newNode;
}
void LinkedList::pop_back() { // ÇALIŞMIYOR
if (this->head == nullptr) return;
if (this->head->next == nullptr) { this->head == nullptr; return; }
Node* walk = this->head;
Node* walk1 = this->head->next;
while (walk1->next != nullptr) {
walk = walk->next;
walk1 = walk1->next;
}
walk->next == nullptr;
}
void LinkedList::push_front(const string& value) //TAMAM
{
Node* newNode = new Node(value);
if (this->head != nullptr){
newNode->next = this->head;
this->head = newNode;
}
else this->head = newNode;
return;
}
void LinkedList::pop_front() //TAMAM
{
if (this->head != nullptr) this->head = this->head->next;
return;
}
void LinkedList::insert_at(int index, const string& value) { //SON İNDEXTE YERİNE KOYMUYOR. ONA BAK
if (this->size() < index) return;
Node* newNode = new Node(value);
Node* walk = this->head;
if (index == 0) {
newNode->next = this->head;
this->head = newNode;
return;
}
if (walk->next == nullptr && index == 1) {
walk->next = newNode;
return;
}
int count = 2; //INDEX 0 ŞARTI KOY
while (walk != nullptr) {
if (count > index) {
newNode->next = walk->next;
walk->next = newNode;
return;
}
walk = walk->next;
count++; //BUNA DA BAK
}
return;
}
void LinkedList::insert_after(int index, const string& val) // INDEX 1 DE HATA, DAHA SONRA BAK
{
if (index < 0 || head == nullptr) return;
Node* newNode = new Node(val);
Node* walk = this->head;
int count = 0;
while (walk != nullptr && count < index) {
walk = walk->next;
count++;
}
if (walk != nullptr)
{
newNode->next = walk->next;
walk->next = newNode;
}
return;
}
void LinkedList::erase_at(int index) { //BOZUK DÜZELTİLDİ
if (index < 0 || head == nullptr) return;
Node* temp = head;
if (index == 0) {
head = head->next;
delete temp;
return;
}
Node* walk = nullptr;
int count = 0;
while (temp != nullptr && count < index) {
walk = temp;
temp = temp->next;
count++;
}
if (temp == nullptr) return;
walk->next = temp->next;
delete temp;
}
void LinkedList::erase(const string& e) // DOĞRU ÇALIŞMIYOR
{
if (head == nullptr) return;
if (head->data == e) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* walk1 = head;
Node* walk2 = head->next;
while (walk2 != nullptr) {
if (walk2->data == e) {
walk1->next = walk2->next;
delete walk2;
return;
}
walk1 = walk2;
walk2 = walk2->next;
}
}
void LinkedList::clear() //TAMAM
{
Node* walk = head;
while (walk != nullptr) {
Node* nextNode = walk->next;
delete walk;
walk = nextNode;
}
head = nullptr;
}
int LinkedList::size() const // TAMAM
{
if (this->head == nullptr) return 0;
Node* walk = this->head;
int count = 1;
while (walk->next != nullptr) {
count++;
walk = walk->next;
}
return count;
}
bool LinkedList::empty() const { //TAMAM
return head == nullptr;
}
Node* LinkedList::findMiddleNode() // TAMAM
{
int siz = this->size();
if (siz == 0) return emptyNode;
Node* walk = head;
if (siz % 2) {
for (int i = 0; i < siz/2 ; i++) {
walk = walk->next;
}
return walk;
}
else
{
for (int i = 0; i < (siz / 2); i++) {
walk = walk->next;
}
return walk;
}
return emptyNode;
}
Node* LinkedList::getSmallestNode() // SMALLI ÇEK
{
if (head == nullptr) return nullptr;
Node* smallest = head;
Node* walk = head->next;
while (walk != nullptr) {
if (walk->data < smallest->data)smallest = walk;
walk = walk->next;
}
return smallest;
}
void LinkedList::moveSmallestToFront()
{
Node* smallestNode = getSmallestNode(); // SMALLESTİN DEVAMINDAN ÇEKİP ÇIKARTMAK LAZIM, LİNKİ DEVAM ETTİR
if (this->head == nullptr) return; // HEAD ZATEN BOŞSA SG
Node* beforesmallest = head;
if (this->head->next != nullptr) {
while (beforesmallest->next != nullptr) {
if (beforesmallest->next == smallestNode) break;
beforesmallest = beforesmallest->next;
}
beforesmallest->next = smallestNode->next;
smallestNode->next = head;
head = smallestNode;
}
return;
}
LinkedList& LinkedList::operator=(const LinkedList& lhs) //TAMAMIMSI
{
Node* newHead = new Node(lhs.head->data);
Node* lhsWalk = lhs.head;
clear();
this->head = newHead;
while (lhsWalk != nullptr) {
Node* newNode = new Node(lhsWalk->data);
newHead->next = newNode;
newHead = newHead->next;
lhsWalk = lhsWalk->next;
}
this->pop_front(); //çok üşendim bunu kaldırmayı unutma
return *this;
}
bool LinkedList::operator==(const LinkedList& lhs) const // TAMAM
{
Node* walk1 = this->head;
Node* walk2 = lhs.head;
if (walk1 == nullptr || walk2 == nullptr) return false;
while (walk1 != nullptr && walk2 != nullptr) {
if (walk1->data != walk2->data) {
return false;
}
walk1 = walk1->next;
walk2 = walk2->next;
}
return true;
}
ostream& operator<<(ostream& out, const LinkedList& list) { // TAMAM
Node* walk = list.head;
if (walk == nullptr) {
out << "";
return out;
}
while (walk != nullptr) {
out << "->" << walk->data;
walk = walk->next;
}
return out;
}