-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LinkedList.h
377 lines (352 loc) · 6.91 KB
/
LinkedList.h
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include<iostream>
#include<string>
typedef long long int LLI;
struct array
{
int size;
int *arr;
};
class Node
{
public:
int data;
Node *next;
};
class LinkedList : public Node
{
private:
/* data */
Node *head;
public:
LinkedList();
~LinkedList();
void add(int new_data);
void add(int pos, int new_data);
void clear();
bool contains(int data);
LLI get(int index);
LLI getFirst();
LLI getLast();
LLI getMiddle();
int printList();
void removeFirst();
void removeLast();
void removeAt(int pos);
void reverse();
int size();
array toArray();
std::string toString();
};
LinkedList::LinkedList()
{
this->head = NULL;
}
LinkedList::~LinkedList()
{
if(!this->head)
free(this->head);
}
/*
Inserts the element at the last(append) of the list
*/
void LinkedList::add(int new_data)
{
Node *new_node = new Node();
new_node->data = new_data;
if(this->head==NULL)
this->head = new_node;
else{
Node *temp;
temp = this->head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = new_node;
new_node->next = NULL;
}
}
/*
Inserts at last when the required position is larger than the linked list size, otherwise inserts at
the required position
*/
void LinkedList::add(int pos, int new_data)
{
Node *new_node = new Node();
new_node->data = new_data;
int ctr=0;
if(this->head == NULL)
this->head = new_node;
else{
if(pos==1)
{
new_node->next = this->head;
this->head = new_node;
}
else{
Node *temp;
temp = this->head;
while(temp->next!=NULL)
{
ctr++;
if(ctr == (pos-1))
{
new_node->next = temp->next;
temp->next = new_node;
}
temp = temp->next;
}
if(pos >= ctr+1)
{
add(new_data);
}
}
}
}
/*
Deletes the entire linked list
*/
void LinkedList::clear()
{
Node *temp, *next_node;
temp = this->head;
while(temp!=NULL)
{
next_node = temp->next;
free(temp);
temp = next_node;
}
this->head = NULL;
}
/*
Returns true if the list contains the specified element
*/
bool LinkedList::contains(int data)
{
Node *temp;
temp = this->head;
while(temp!=NULL)
{
if(temp->data == data)
return true;
temp = temp->next;
}
return false;
}
/*
Returns the element at the specified position
*/
LLI LinkedList::get(int index)
{
int ctr=0;
Node *temp;
temp = this->head;
while(temp!=NULL)
{
ctr++;
if(ctr==index)
return temp->data;
temp = temp->next;
}
return -1;
}
/*
Returns the First element of the list
*/
LLI LinkedList::getFirst()
{
if(!this->head) return -1;
return this->head->data;
}
/*
Returns the Last element of the list
*/
LLI LinkedList::getLast()
{
Node *temp;
temp = head;
while(temp->next!=NULL)
temp = temp->next;
return temp->data;
}
/*
Returns the middle element of the linked list
*/
LLI LinkedList::getMiddle()
{
Node *first=NULL, *second=NULL, *temp;
temp = this->head;
if(!temp) return -1;
first = second = temp;
while(second && second->next)
{
first = first->next;
second = second->next;
if(second)
second = second->next;
}
return first->data;
}
/*
prints the data in the linked list
*/
int LinkedList::printList()
{
Node *temp;
int ctr = 0;
temp = this->head;
if(temp==NULL)
std::cout<<"Error: Nothing to print. LinkedList is empty!\n";
else{
while(temp!=NULL)
{
std::cout<<temp->data<<" ";
ctr++;
temp = temp->next;
}
}
return ctr;
}
/*
Removes the first element of the linked list
*/
void LinkedList::removeFirst()
{
if(this->head==NULL)
std::cout<<"Error: Nothing to delete. LinkedList is empty!\n";
else if((this->head)->next==NULL)
{
this->head = NULL;
free(this->head);
}
else
this->head = this->head->next;
}
/*
Removes the last element of the linked list
*/
void LinkedList::removeLast()
{
Node *temp;
temp = this->head;
if(temp == NULL)
std::cout<<"Error: Nothing to delete. LinkedList is empty!\n";
else if(temp->next == NULL)
{
temp = NULL;
free(temp);
}
else{
while(temp->next->next!=NULL)
{
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
}
/*
Removes the last element when the specified position is larger than the linked list size, otherwise removes the
element at the specified position
*/
void LinkedList::removeAt(int pos)
{
Node *temp, *store;
temp = this->head;
int ctr=0;
if(temp==NULL)
std::cout<<"Error: Nothing to delete. LinkedList is empty!\n";
else if(temp->next == NULL)
{
temp=NULL;
free(temp);
}
else{
if(pos==1)
{
removeFirst();
}
else{
while(temp!=NULL)
{
ctr++;
if(ctr == (pos-1))
{
store = temp->next->next;
free(temp->next);
temp->next = store;
}
temp = temp->next;
}
}
}
}
/*
Reverses the linked list
*/
void LinkedList::reverse()
{
Node *curr, *prev=NULL, *next=NULL;
curr = this->head;
while(curr!=NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
this->head = prev;
}
/*
Returns the size of the linked list
*/
int LinkedList::size()
{
Node *temp;
temp = this->head;
int ctr = 0;
while(temp!=NULL)
{
ctr++;
temp = temp->next;
}
return ctr;
}
/*
Stores the elements of the linked list in an array and
returns the array
*/
array LinkedList::toArray()
{
int *res_arr, res_arr_ind=0;
array arrObj;
Node *temp;
temp = this->head;
res_arr = (int *)malloc((this->size()) * sizeof(int));
while(temp!=NULL)
{
res_arr[res_arr_ind++] = temp->data;
temp = temp->next;
}
arrObj.arr = res_arr;
arrObj.size = this->size();
return arrObj;
}
/*
Returns a string containing all of the elements in the list in
proper sequence (from first to last), each element is separated
by commas and the string is enclosed in square brackets
*/
std::string LinkedList::toString()
{
std::string resStr = "[";
Node *temp;
temp = this->head;
while(temp!=NULL)
{
resStr = resStr + std::to_string(temp->data)+", ";
temp = temp->next;
}
resStr += "]";
return resStr;
}