-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked list class
442 lines (391 loc) · 8.49 KB
/
Linked list class
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//Name:Aakash Kamleshkumar Panchal
//Student Id:201801459
//Date:14/03/2019
//Program:Implementation of Linked List
#include <iostream>
using namespace std;
template<typename E>
struct node {
E data;
node<E>* nextElement;
};
template <typename E>
class List
{
void operator = (const List&) {}
List(const List&) {}
public:
//constructors and destructors
List() {}
virtual ~List() {}
//Return the number of elements in the List
virtual int NumberOfElements() const = 0;
//Return the Location of current element
virtual int CurrentLocation() const = 0;
//Return the data of current element
virtual const E& getData() const = 0;
//clear all the data from the List
virtual void clear() = 0;
//insert a data in the List at current location
virtual void insert(const E& data) = 0;
//insert a data in the end of the List
virtual void append(const E& data) = 0;
//delete a data at the current Location
virtual E remove() = 0;
//set the current Location to the start of the List
virtual void setListLocationToStart() = 0;
//set the current Location to the end of the List
virtual void setListLocationToEnd() = 0;
//set the current Location to the element on left side
// of current element
virtual void previousElement() = 0;
//set the current Location to the element on left side
// of current element
virtual void nextElement() = 0;
//Set current Location to a new Location
virtual int setToNewLocation(int location) = 0;
};
template <typename E>
class linkList : public List<E>
{
public:
node<E>*firstElement;
node<E>*lastElement;
node<E>*currentElement;
int sizeofList;
int currentLocation;
//constructors and destructors
linkList()
{
firstElement = lastElement = currentElement = new node < E >;
firstElement = lastElement = currentElement = NULL;
currentLocation = 0;
sizeofList = 0;
}
~linkList()
{
while (firstElement != NULL)
{
currentElement = firstElement;
firstElement = firstElement->nextElement;
delete currentElement;
}
}
int NumberOfElements() const
{
return sizeofList;
}
int CurrentLocation() const
{
return currentLocation;
}
const E& getData() const
{
int i{ currentLocation };
node<E>*temp = new node<E>;
temp = firstElement;
while (i-- > 1)
{
temp = temp->nextElement;
}
return temp->data;
}
void clear()
{
while (firstElement != NULL)
{
currentElement = firstElement;
firstElement = firstElement->nextElement;
delete currentElement;
}
firstElement = currentElement = lastElement = NULL;
sizeofList = 0;
currentLocation = 0;
}
void insert(const E& data)
{
node<E>*p = new node<E>;
p->data = data;
sizeofList++;
if (currentElement == firstElement)
{
if (firstElement == NULL && lastElement == NULL)
{
lastElement = p;
}
p->nextElement = firstElement;
currentElement = p;
firstElement = p;
currentLocation = 1;
return;
}
node<E>* tN = new node<E>;
tN = firstElement;
while (tN->nextElement != currentElement)
{
tN = tN->nextElement;
}
tN->nextElement = p;
p->nextElement = currentElement;
currentElement = p;
}
void append(const E& data)
{
node<E>* k = new node<E>;
k->data = data;
k->nextElement = NULL;
if(sizeofList==0)
{
firstElement = k;
currentElement = k;
lastElement = k;
sizeofList++;
currentLocation = 1;
}
else {
lastElement->nextElement = k;
lastElement = k;
sizeofList++;
}
}
E remove()
{
node<E>*rmv = new node<E>;
E removed{ currentElement->data};
rmv = firstElement;
int lo{ 1 };
while (lo++ < currentLocation-1)
{
rmv = rmv->nextElement;
}
if (sizeofList == 1)
{
currentElement = firstElement = lastElement = NULL;
sizeofList = 0;
currentLocation = 0;
}
else if (currentLocation==1 && sizeofList!=1) {
firstElement = currentElement->nextElement;
currentElement = firstElement;
sizeofList--;
}
else {
rmv->nextElement = currentElement->nextElement;
currentElement=rmv->nextElement;
sizeofList--;
}
if (currentLocation == sizeofList + 1) {
setToNewLocation(currentLocation - 1);
}
return removed;
}
void setListLocationToStart()
{
setToNewLocation(1);
}
void setListLocationToEnd()
{
setToNewLocation(sizeofList);
}
void previousElement()
{
setToNewLocation(currentLocation - 1);
}
void nextElement()
{
setToNewLocation(currentLocation + 1);
}
int setToNewLocation(int location)
{
currentLocation = location;
int i{ 1 };
currentElement = firstElement;
while (i++ < currentLocation)
{
currentElement = currentElement->nextElement;
}
return currentLocation;
}
void print()
{
if (sizeofList == 0)
{
cout << "\nLinked List has no nodes, it's empty" << endl;
}
else {
node<E>* pr = new node<E>;
pr = firstElement;
int i{ sizeofList };
cout << " Linked List is :";
while (i-->0)
{
cout << pr->data << " ";
pr = pr->nextElement;
}
}
cout << "\n\n";
}
};
int main()
{
linkList<int> L;
do {
int operation{ 0 };
cout << "\n*******Enter number according to operation you want to perform:\n\n";
cout << " 0-EXIT\n 1-Size of List\n 2-Current Location-\n 3-Data of current Location\n"
" 4-Clear all the data from the list\n 5-Insert at current Location\n"
" 6-Append\n 7-remove current Location Node data\n 8-Set current Location to the starting of list"
"\n 9-Set current Location to the ending of list\n 10-Set current Location to Previous element"
"\n 11-Set current Location to next element\n 12-set Location to input Location"
"\n 13-Print Linked List\n\n********************"
"********************************************\n\n";
cout << "Attention!! Linked List index starts from 1 in this code\n";
cout << "\nChoice Operation:";
cin >> operation;
switch (operation)
{
case 0: {
return 0;
}
case 1: {
cout << "\nSize of List:" << L.NumberOfElements() << "\n";
break;
}
case 2: {
if (L.firstElement == NULL)
{
cout << "\nLinked List is empty, NO DATA\n";
break;
}
cout << L.CurrentLocation() << endl;
break;
}
case 3: {
if (L.firstElement == NULL)
{
cout << "\nLinked List is empty, NO DATA\n";
break;
}
if (L.currentLocation > L.sizeofList)
{
cout << "\nNO data at that location\n";
break;
}
cout << "\nCurrent Element Data is:" << L.getData() << endl;
L.print();
break;
}
case 4: {
if (L.firstElement == NULL)
{
cout << "\nLinked List is already empty:\n";
break;
}
L.clear();
cout << " \n \"Every data is cleared\" \n ";
break;
}
case 5: {
cout << "\nEnter element you want to insert:";
int data{ 0 };
cin >> data;
L.insert(data);
cout << "\nNode Inserted\n\n";
L.print();
break;
}
case 6: {
cout << "\nEnter data of node you want to append: \n";
int dataa;
cin >> dataa;
L.append(dataa);
cout << "\nNode Appended\n\n";
L.print();
break;
}
case 7: {
if (L.sizeofList == 0 || L.currentLocation<0)
{
cout << "\nLinked List is empty you can not remove further\n";
break;
}
cout<<"\nNode of data "<<L.remove()<<" is removed.\n"<<endl;
L.print();
break;
}
case 8: {
if (L.sizeofList == 0)
{
cout << "\nLinked List is empty\n";
break;
}
L.setListLocationToStart();
break;
}
case 9: {
if (L.sizeofList == 0)
{
cout << "\nLinked List is empty\n";
break;
}
L.setListLocationToEnd();
break;
}
case 10: {
if (L.sizeofList == 0)
{
cout << "\nLinked List is empty\n";
break;
}
if (L.currentLocation == 1 || L.currentLocation==0)
{
cout << "\nNO previous node present in Linked list\n";
break;
}
L.previousElement();
break;
}
case 11: {
if (L.sizeofList == 0)
{
cout << "\nLinked List is empty\n";
break;
}
if (L.currentLocation == L.sizeofList)
{
cout << "\nNo next node present in Linked list\n";
break;
}
L.nextElement();
break;
}
case 12: {
if (L.sizeofList == 0)
{
cout << "\nLinked List is empty\n";
break;
}
cout << "\nEnter Location you want to set:" << endl;
int o{ 0 };
do {
cout << "\nEnter Valid Location i.e.,between:1 & "<<L.sizeofList<<endl;
cin >> o;
} while (o > L.sizeofList || o<=0);
cout<<"\nCurrent location is set to "<<L.setToNewLocation(o);
L.print();
break;
}
case 13: {
if (L.sizeofList == 0)
{
cout << "\nLinked List is empty\n";
break;
}
L.print();
break;
}
default:
cout << "\nEnter valid number choice\n";
}
} while (1);
}