forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
333 lines (264 loc) · 7.93 KB
/
LinkedList.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
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
#include <iostream>
using namespace std;
class LinkedList {
private:
struct Node {
int value;
Node* next;
Node(int val) : value(val), next(nullptr) {}
Node(int val, Node* nxt) : value(val), next(nxt) {}
};
Node* head;
Node* tail;
int size;
public:
LinkedList() : head(nullptr), tail(nullptr), size(0) {}
void display() {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->value << " -> ";
temp = temp->next;
}
std::cout << "END" << std::endl;
}
void insert(int value, int index) {
if (index < 0 || index > size) {
std::cerr << "Invalid index for insertion" << std::endl;
return;
}
Node* newNode = new Node(value);
if (index == 0) {
// Insert at the beginning
newNode->next = head;
head = newNode;
if (tail == nullptr) {
tail = newNode;
}
} else if (index == size) {
// Insert at the end
if (tail == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
} else {
// Insert at a specific index
Node* prev = nullptr;
Node* current = head;
for (int i = 0; i < index; i++) {
prev = current;
current = current->next;
}
prev->next = newNode;
newNode->next = current;
}
size++;
}
void insertFirst(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
if (tail == nullptr) {
tail = newNode;
}
size++;
}
void insertLast(int value) {
Node* newNode = new Node(value);
if (tail == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
size++;
}
int deleteFirst() {
if (head == nullptr) {
std::cerr << "The list is empty. Cannot delete from an empty list." << std::endl;
return -1; // Return a default value or handle the error as needed.
}
int deletedValue = head->value;
Node* temp = head;
head = head->next;
delete temp;
if (head == nullptr) {
tail = nullptr;
}
size--;
return deletedValue;
}
int deleteLast() {
if (head == nullptr) {
std::cerr << "The list is empty. Cannot delete from an empty list." << std::endl;
return -1; // Return a default value or handle the error as needed.
}
int deletedValue;
if (head == tail) {
// If there's only one element in the list
deletedValue = head->value;
delete head;
head = tail = nullptr;
} else {
// Find the second-to-last node
Node* secondLast = head;
while (secondLast->next != tail) {
secondLast = secondLast->next;
}
deletedValue = tail->value;
delete tail;
tail = secondLast;
tail->next = nullptr;
}
size--;
return deletedValue;
}
int deleteNode(int index) {
if (index < 0 || index >= size) {
std::cerr << "Invalid index for deletion" << std::endl;
return -1; // Return a default value or handle the error as needed.
}
int deletedValue;
if (index == 0) {
// Deleting the first node
deletedValue = head->value;
Node* temp = head;
head = head->next;
delete temp;
if (head == nullptr) {
tail = nullptr;
}
} else {
// Deleting a node other than the first one
Node* prev = nullptr;
Node* current = head;
for (int i = 0; i < index; i++) {
prev = current;
current = current->next;
}
deletedValue = current->value;
prev->next = current->next;
if (current == tail) {
tail = prev;
}
delete current;
}
size--;
return deletedValue;
}
void reverse() {
if (size < 2) {
return;
}
Node* prev = nullptr;
Node* current = head;
Node* nextNode = current->next;
while (current != nullptr) {
current->next = prev;
prev = current;
current = nextNode;
if (nextNode != nullptr) {
nextNode = nextNode->next;
}
}
head = prev;
}
Node* findMiddle() {
if (head == nullptr) {
return nullptr;
}
Node* slow = head;
Node* fast = head;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
int getMiddleValue() {
Node* middle = findMiddle();
if (middle != nullptr) {
return middle->value;
} else {
cerr << "The list is empty." << endl;
return -1; // Return a default value or handle the error as needed.
}
}
// Function to check if the linked list is a palindrome
bool isPalindrome() {
if (head == nullptr) {
return true; // An empty list is considered a palindrome
}
Node* middle = findMiddle();
// Reverse the second half of the list
Node* prev = nullptr;
Node* current = middle;
Node* nextNode;
while (current != nullptr) {
nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
}
// Compare the first and reversed second halves
Node* left = head;
Node* right = prev;
while (left != nullptr && right != nullptr) {
if (left->value != right->value) {
return false;
}
left = left->next;
right = right->next;
}
// Restore the original list
current = prev;
prev = nullptr;
while (current != nullptr) {
nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
}
return true;
}
};
int main() {
LinkedList myList; // Create an instance of the LinkedList
// Insert elements into the list
myList.insertLast(1);
myList.insertLast(2);
myList.insertLast(3);
myList.insertLast(4);
// Display the initial list
myList.display(); // Output: 1 -> 2 -> 3 -> 4 -> END
//Display middle value of linked list
int middleValue = myList.getMiddleValue();
if (middleValue != -1) {
cout << "Middle element: " << middleValue << endl;
}
// Delete elements from the list
myList.deleteFirst();
myList.deleteLast();
// Display the modified list
myList.display(); // Output: 2 -> 3 -> END
// Insert an element at a specific index
myList.insert(5, 1);
// Display the modified list
myList.display(); // Output: 2 -> 5 -> 3 -> END
// Delete an element at a specific index
myList.deleteNode(1);
// Display the modified list
myList.display(); // Output: 2 -> 3 -> END
// Reverse the list
myList.reverse();
// Display the reversed list
myList.display();
// Check if the list is a palindrome
if (myList.isPalindrome()) {
cout << "The list is a palindrome." << endl;
} else {
cout << "The list is not a palindrome." << endl;
}
return 0;
}