-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list-operations.html
More file actions
1505 lines (1099 loc) · 69.4 KB
/
Copy pathlinked-list-operations.html
File metadata and controls
1505 lines (1099 loc) · 69.4 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-HCERBDV76D"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-HCERBDV76D');
</script>
<meta content="B0487B46A104E90209E8A3BEA24ECA0E" name="msvalidate.01"/>
<meta content="f044b3a12c7918f1" name="yandex-verification"/>
<!--end-->
<meta content="learnPython" name="author"/>
<meta content="Learn Python for free,learn python for beginners,Core Python,Web frameworks,Multiprocess architecture,Serverside templating language,python tutorials,python4" name="keywords"/>
<meta content="website" property="og:type"/>
<meta content="learn about data types, variables, lists, tuples, dictionaries,if else,DSA,loops,user-defined functions, oop, threading and scripting." name="description"/>
<meta content="US-CA" name="”geo.region”"/>
<meta content="353 Jane Stanford Way, Stanford, CA 94305, United States" name="”geo.placename”"/>
<meta content="37.430089898615456;-122.17332683124829" name="”geo.position”"/>
<meta content="37.430089898615456, -122.17332683124829" name="”ICBM”"/>
<link href="https://pythonread.github.io/?m=1" rel="alternate"/>
<link href="/favicon.png" rel="icon"/>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="/style1.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style2.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style0.css" media="all" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="nav-wrapper">
<div class="container">
<nav>
<div class="logo wave">
<a href="/" id="logo">
Python Tutorial
</a>
</div>
<div class="nav-toggle-icon" id="nav-toggle-icon" onclick="mobileMenu()">
<div class="material-hamburger">
<span>
</span>
<span>
</span>
<span>
</span>
</div>
</div>
<div class="menu-wrapper" id="menu-wrapper">
<div class="nav-indicator">
</div>
<ul class="menus">
<li>
<a class="wave" href="/">
Home
</a>
</li>
<li>
<a class="wave" href="/projects.html">
Projects
</a>
</li>
<li>
<a class="wave" href="/free-course.html" target="_blank">
Free Course
</a>
</li>
<li>
<a class="wave" href="/dsa.html">
DSA
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="contents contents--neg" style="margin-top: 60px">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="d-flex">
<div class="left-bar d-none d-lg-block">
<div class="card-alt mb-10x">
<h3>Page Index</h3>
<div class="list">
<ul>
<li><a href="/dsa.html#data-structure-1" title="Data Structures (I)">Data Structures
(I)</a></li>
<li><a href="/dsa.html#data-structure-2" title="Data Structures (II)">Data Structures
(II)</a></li>
<li><a href="/dsa.html#tree-1" title="Tree based DSA (I)">Tree based DSA (I)</a></li>
<li><a href="/dsa.html#tree-2" title="Tree based DSA (II)">Tree based DSA (II)</a></li>
<li><a href="/dsa.html#graph" title="Graph Data Structures and Algorithm">Graph based
DSA</a></li>
<li><a href="/dsa.html#sorting-searching" title="Sorting and Searching">Sorting and
Searching</a></li>
<li><a href="/dsa.html#greedy-algorithm" title="Greedy Algorithms">Greedy Algorithms</a>
</li>
<li><a href="/dsa.html#dynamic-programming" title="Dynamic Programming">Dynamic
Programming</a></li>
<li><a href="/dsa.html#other-algorithms" title="Other Algorithms">Other Algorithms</a>
</li>
</ul>
</div>
</div>
</div>
<div class="right-bar">
<!--first part end-------------------------------------------->
<iframe width="560" height="315" src="https://www.youtube.com/embed/SRTO0VpsrFQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div class="editor-contents">
<h1>Linked List Operations: Traverse, Insert and Delete</h1>
<p class="editor-contents__short-description">In this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java.</p>
<div id="node-962" class="node node-algorithm clearfix" about="/dsa/linked-list-operations" typeof="sioc:Item foaf:Document">
<span property="dc:title" content="Linked List Operations: Traverse, Insert and Delete" class="rdf-meta element-hidden"></span>
<div class="content">
<p id="definition">There are various linked list operations that allow us to perform different actions on linked lists. For example, the insertion operation adds a new element to the linked list.</p>
<p>Here's a list of basic linked list operations that we will cover in this article.</p>
<ul><li><a href="#traverse">Traversal</a> - access each element of the linked list</li>
<li><a href="#add">Insertion</a> - adds a new element to the linked list</li>
<li><a href="#delete">Deletion</a> - removes the existing elements</li>
<li><a href="#search">Search</a> - find a node in the linked list</li>
<li><a href="#sort">Sort</a> - sort the nodes of the linked list</li>
</ul><p>Before you learn about linked list operations in detail, make sure to know about <a href="/dsa/linked-list.html">Linked List</a> first.</p>
<h3>Things to Remember about Linked List</h3>
<ul><li><var>head</var> points to the first node of the linked list</li>
<li><var>next</var> pointer of the last node is <var>NULL</var>, so if the next current node is <var>NULL</var>, we have reached the end of the linked list.</li>
</ul><p>In all of the examples, we will assume that the linked list has three nodes <code>1 --->2 --->3</code> with node structure as below:</p>
<pre style="max-height: 600px;"><code class="dsa hljs cpp"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> {</span>
<span class="hljs-keyword">int</span> data;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">next</span>;</span>
};</code></pre>
<hr><h2 id="traverse">Traverse a Linked List</h2>
<p>Displaying the contents of a linked list is very simple. We keep moving the temp node to the next one and display its contents.</p>
<p>When <var>temp</var> is <var>NULL</var>, we know that we have reached the end of the linked list so we get out of the while loop.</p>
<pre style="max-height: 600px;"><code class="dsa hljs cpp"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">temp</span> = <span class="hljs-title">head</span>;</span>
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n\nList elements are - \n"</span>);
<span class="hljs-keyword">while</span>(temp != <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d --->"</span>,temp->data);
temp = temp->next;
}</code></pre>
<p>The output of this program will be:</p>
<pre><samp>List elements are -
1 --->2 --->3 ---></samp></pre>
<hr><h2 id="add">Insert Elements to a Linked List</h2>
<p></p><div class="clearfix"></div><p>You can add elements to either the beginning, middle or end of the linked list.</p>
<h3>1. Insert at the beginning</h3>
<ul><li>Allocate memory for new node</li>
<li>Store data</li>
<li>Change next of new node to point to head</li>
<li>Change head to point to recently created node</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span> *newNode;
newNode = malloc(sizeof(<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span>));
newNode->data = <span class="hljs-number">4</span>;
newNode->next = head;
head = newNode;</code></pre>
<h3>2. Insert at the End</h3>
<ul><li>Allocate memory for new node</li>
<li>Store data</li>
<li>Traverse to last node</li>
<li>Change next of last node to recently created node</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span> *newNode;
newNode = malloc(sizeof(<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span>));
newNode->data = <span class="hljs-number">4</span>;
newNode->next = NULL;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span> *temp = head;
<span class="hljs-keyword">while</span>(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;</code></pre>
<h3>3. Insert at the Middle</h3>
<ul><li>Allocate memory and store data for new node</li>
<li>Traverse to node just before the required position of new node</li>
<li>Change next pointers to include new node in between</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span> *newNode;
newNode = malloc(sizeof(<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span>));
newNode->data = <span class="hljs-number">4</span>;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span></span> *temp = head;
<span class="hljs-keyword">for</span>(int i=<span class="hljs-number">2</span>; i < position; i++) {
<span class="hljs-keyword">if</span>(temp->next != NULL) {
temp = temp->next;
}
}
newNode->next = temp->next;
temp->next = newNode;</code></pre>
<hr><h2 id="delete">Delete from a Linked List</h2>
<p>You can delete either from the beginning, end or from a particular position.</p>
<h3>1. Delete from beginning</h3>
<ul><li>Point head to the second node</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs ini"><span class="hljs-attr">head</span> = head->next<span class="hljs-comment">;</span></code></pre>
<h3>2. Delete from end</h3>
<ul><li>Traverse to second last element</li>
<li>Change its next pointer to null</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs php">struct node* temp = head;
<span class="hljs-keyword">while</span>(temp->next->next!=<span class="hljs-keyword">NULL</span>){
temp = temp->next;
}
temp->next = <span class="hljs-keyword">NULL</span>;</code></pre>
<h3>3. Delete from middle</h3>
<ul><li>Traverse to element before the element to be deleted</li>
<li>Change next pointers to exclude the node from the chain</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs perl"><span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">2</span>; i< position; i++) {
<span class="hljs-keyword">if</span>(temp-><span class="hljs-keyword">next</span>!=NULL) {
temp = temp-><span class="hljs-keyword">next</span>;
}
}
temp-><span class="hljs-keyword">next</span> = temp-><span class="hljs-keyword">next</span>-><span class="hljs-keyword">next</span>;</code></pre>
<hr><h2 id="search">Search an Element on a Linked List</h2>
<p>You can search an element on a linked list using a loop using the following steps. We are finding <code>item</code> on a linked list.</p>
<ul><li>Make <code>head</code> as the <code>current</code> node.</li>
<li>Run a loop until the <code>current</code> node is <code>NULL</code> because the last element points to <code>NULL</code>.</li>
<li>In each iteration, check if the key of the node is equal to <code>item</code>. If it the key matches the item, return <code>true</code> otherwise return <code>false</code>.</li>
</ul><pre style="max-height: 600px;"><code class="dsa hljs rust"><span class="hljs-comment">// Search a node</span>
<span class="hljs-built_in">bool</span> searchNode(<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span></span>** head_ref, int key) {
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span></span>* current = *head_ref;
<span class="hljs-keyword">while</span> (current != NULL) {
<span class="hljs-keyword">if</span> (current->data == key) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
current = current->next;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}</code></pre>
<hr><h2 id="sort">Sort Elements of a Linked List</h2>
<p>We will use a simple sorting algorithm, <a href="/dsa/bubble-sort.html">Bubble Sort</a>, to sort the elements of a linked list in ascending order below.</p>
<ol><li>Make the <code>head</code> as the <code>current</code> node and create another node <code>index</code> for later use.</li>
<li>If <code>head</code> is null, return.</li>
<li>Else, run a loop till the last node (i.e. <code>NULL</code>).</li>
<li>In each iteration, follow the following step 5-6.</li>
<li>Store the next node of <code>current</code> in <code>index</code>.</li>
<li>Check if the data of the current node is greater than the next node. If it is greater, swap <code>current</code> and <code>index</code>.</li>
</ol><p>Check the article on <a href="/dsa/bubble-sort.html">bubble sort</a> for better understanding of its working.</p>
<pre style="max-height: 600px;"><code class="dsa hljs php"><span class="hljs-comment">// Sort the linked list</span>
void sortLinkedList(struct Node** head_ref) {
struct Node *current = *head_ref, *index = <span class="hljs-keyword">NULL</span>;
int temp;
<span class="hljs-keyword">if</span> (head_ref == <span class="hljs-keyword">NULL</span>) {
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">while</span> (current != <span class="hljs-keyword">NULL</span>) {
<span class="hljs-comment">// index points to the node next to current</span>
index = current->next;
<span class="hljs-keyword">while</span> (index != <span class="hljs-keyword">NULL</span>) {
<span class="hljs-keyword">if</span> (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}</code></pre>
<hr><h2 id="operations">LinkedList Operations in Python, Java, C, and C++</h2>
<div class="tabbed-editor">
<div id="py1" onclick="changepy()" class="tabbed-editor__node tabbed-editor__node--active"><a href="#python-code">Python</a></div>
<div id="java1" onclick="changejava()" class="tabbed-editor__node"><a href="#java-code">Java</a></div>
<div id="c1" onclick="changec()" class="tabbed-editor__node"><a href="#c-code">C</a></div>
<div id="cpp1" onclick="changecpp()" class="tabbed-editor__node"><a href="#cpp-code">C++</a></div>
</div>
<div class="code-editor code-editor--tabbed">
<div class="code-editor__area code-editor__area--active" id="python-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="python hljs"><span class="hljs-comment"># Linked list operations in Python</span>
<span class="hljs-comment"># Create a node</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, data)</span>:</span>
self.data = data
self.next = <span class="hljs-literal">None</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LinkedList</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
self.head = <span class="hljs-literal">None</span>
<span class="hljs-comment"># Insert at the beginning</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insertAtBeginning</span><span class="hljs-params">(self, new_data)</span>:</span>
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
<span class="hljs-comment"># Insert after a node</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insertAfter</span><span class="hljs-params">(self, prev_node, new_data)</span>:</span>
<span class="hljs-keyword">if</span> prev_node <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">print</span>(<span class="hljs-string">"The given previous node must inLinkedList."</span>)
<span class="hljs-keyword">return</span>
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
<span class="hljs-comment"># Insert at the end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insertAtEnd</span><span class="hljs-params">(self, new_data)</span>:</span>
new_node = Node(new_data)
<span class="hljs-keyword">if</span> self.head <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
self.head = new_node
<span class="hljs-keyword">return</span>
last = self.head
<span class="hljs-keyword">while</span> (last.next):
last = last.next
last.next = new_node
<span class="hljs-comment"># Deleting a node</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deleteNode</span><span class="hljs-params">(self, position)</span>:</span>
<span class="hljs-keyword">if</span> self.head <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span>
temp = self.head
<span class="hljs-keyword">if</span> position == <span class="hljs-number">0</span>:
self.head = temp.next
temp = <span class="hljs-literal">None</span>
<span class="hljs-keyword">return</span>
<span class="hljs-comment"># Find the key to be deleted</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(position - <span class="hljs-number">1</span>):
temp = temp.next
<span class="hljs-keyword">if</span> temp <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">break</span>
<span class="hljs-comment"># If the key is not present</span>
<span class="hljs-keyword">if</span> temp <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">if</span> temp.next <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span>
next = temp.next.next
temp.next = <span class="hljs-literal">None</span>
temp.next = next
<span class="hljs-comment"># Search an element</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">search</span><span class="hljs-params">(self, key)</span>:</span>
current = self.head
<span class="hljs-keyword">while</span> current <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">if</span> current.data == key:
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
current = current.next
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
<span class="hljs-comment"># Sort the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sortLinkedList</span><span class="hljs-params">(self, head)</span>:</span>
current = head
index = Node(<span class="hljs-literal">None</span>)
<span class="hljs-keyword">if</span> head <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">while</span> current <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
<span class="hljs-comment"># index points to the node next to current</span>
index = current.next
<span class="hljs-keyword">while</span> index <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">if</span> current.data > index.data:
current.data, index.data = index.data, current.data
index = index.next
current = current.next
<span class="hljs-comment"># Print the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">printList</span><span class="hljs-params">(self)</span>:</span>
temp = self.head
<span class="hljs-keyword">while</span> (temp):
<span class="hljs-keyword">print</span>(str(temp.data) + <span class="hljs-string">" "</span>, end=<span class="hljs-string">""</span>)
temp = temp.next
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
llist = LinkedList()
llist.insertAtEnd(<span class="hljs-number">1</span>)
llist.insertAtBeginning(<span class="hljs-number">2</span>)
llist.insertAtBeginning(<span class="hljs-number">3</span>)
llist.insertAtEnd(<span class="hljs-number">4</span>)
llist.insertAfter(llist.head.next, <span class="hljs-number">5</span>)
<span class="hljs-keyword">print</span>(<span class="hljs-string">'linked list:'</span>)
llist.printList()
<span class="hljs-keyword">print</span>(<span class="hljs-string">"\nAfter deleting an element:"</span>)
llist.deleteNode(<span class="hljs-number">3</span>)
llist.printList()
<span class="hljs-keyword">print</span>()
item_to_find = <span class="hljs-number">3</span>
<span class="hljs-keyword">if</span> llist.search(item_to_find):
<span class="hljs-keyword">print</span>(str(item_to_find) + <span class="hljs-string">" is found"</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">print</span>(str(item_to_find) + <span class="hljs-string">" is not found"</span>)
llist.sortLinkedList(llist.head)
<span class="hljs-keyword">print</span>(<span class="hljs-string">"Sorted List: "</span>)
llist.printList()</code></pre></div>
</div>
<div class="code-editor__area" id="java-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="java hljs"><span class="hljs-comment">// Linked list operations in Java</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LinkedList</span> </span>{
Node head;
<span class="hljs-comment">// Create a node</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>{
<span class="hljs-keyword">int</span> data;
Node next;
Node(<span class="hljs-keyword">int</span> d) {
data = d;
next = <span class="hljs-keyword">null</span>;
}
}
<span class="hljs-comment">// Insert at the beginning</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertAtBeginning</span><span class="hljs-params">(<span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-comment">// insert the data</span>
Node new_node = <span class="hljs-keyword">new</span> Node(new_data);
new_node.next = head;
head = new_node;
}
<span class="hljs-comment">// Insert after a node</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertAfter</span><span class="hljs-params">(Node prev_node, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-keyword">if</span> (prev_node == <span class="hljs-keyword">null</span>) {
System.out.println(<span class="hljs-string">"The given previous node cannot be null"</span>);
<span class="hljs-keyword">return</span>;
}
Node new_node = <span class="hljs-keyword">new</span> Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
<span class="hljs-comment">// Insert at the end</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertAtEnd</span><span class="hljs-params">(<span class="hljs-keyword">int</span> new_data)</span> </span>{
Node new_node = <span class="hljs-keyword">new</span> Node(new_data);
<span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span>) {
head = <span class="hljs-keyword">new</span> Node(new_data);
<span class="hljs-keyword">return</span>;
}
new_node.next = <span class="hljs-keyword">null</span>;
Node last = head;
<span class="hljs-keyword">while</span> (last.next != <span class="hljs-keyword">null</span>)
last = last.next;
last.next = new_node;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Delete a node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deleteNode</span><span class="hljs-params">(<span class="hljs-keyword">int</span> position)</span> </span>{
<span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span>;
Node temp = head;
<span class="hljs-keyword">if</span> (position == <span class="hljs-number">0</span>) {
head = temp.next;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Find the key to be deleted</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; temp != <span class="hljs-keyword">null</span> && i < position - <span class="hljs-number">1</span>; i++)
temp = temp.next;
<span class="hljs-comment">// If the key is not present</span>
<span class="hljs-keyword">if</span> (temp == <span class="hljs-keyword">null</span> || temp.next == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-comment">// Remove the node</span>
Node next = temp.next.next;
temp.next = next;
}
<span class="hljs-comment">// Search a node</span>
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">search</span><span class="hljs-params">(Node head, <span class="hljs-keyword">int</span> key)</span> </span>{
Node current = head;
<span class="hljs-keyword">while</span> (current != <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">if</span> (current.data == key)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
current = current.next;
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
}
<span class="hljs-comment">// Sort the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sortLinkedList</span><span class="hljs-params">(Node head)</span> </span>{
Node current = head;
Node index = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">int</span> temp;
<span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">while</span> (current != <span class="hljs-keyword">null</span>) {
<span class="hljs-comment">// index points to the node next to current</span>
index = current.next;
<span class="hljs-keyword">while</span> (index != <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">if</span> (current.data > index.data) {
temp = current.data;
current.data = index.data;
index.data = temp;
}
index = index.next;
}
current = current.next;
}
}
}
<span class="hljs-comment">// Print the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">()</span> </span>{
Node tnode = head;
<span class="hljs-keyword">while</span> (tnode != <span class="hljs-keyword">null</span>) {
System.out.print(tnode.data + <span class="hljs-string">" "</span>);
tnode = tnode.next;
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
LinkedList llist = <span class="hljs-keyword">new</span> LinkedList();
llist.insertAtEnd(<span class="hljs-number">1</span>);
llist.insertAtBeginning(<span class="hljs-number">2</span>);
llist.insertAtBeginning(<span class="hljs-number">3</span>);
llist.insertAtEnd(<span class="hljs-number">4</span>);
llist.insertAfter(llist.head.next, <span class="hljs-number">5</span>);
System.out.println(<span class="hljs-string">"Linked list: "</span>);
llist.printList();
System.out.println(<span class="hljs-string">"\nAfter deleting an element: "</span>);
llist.deleteNode(<span class="hljs-number">3</span>);
llist.printList();
System.out.println();
<span class="hljs-keyword">int</span> item_to_find = <span class="hljs-number">3</span>;
<span class="hljs-keyword">if</span> (llist.search(llist.head, item_to_find))
System.out.println(item_to_find + <span class="hljs-string">" is found"</span>);
<span class="hljs-keyword">else</span>
System.out.println(item_to_find + <span class="hljs-string">" is not found"</span>);
llist.sortLinkedList(llist.head);
System.out.println(<span class="hljs-string">"\nSorted List: "</span>);
llist.printList();
}
}</code></pre></div>
</div>
<div class="code-editor__area" id="c-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="c hljs cpp"><span class="hljs-comment">// Linked list operations in C</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span>
<span class="hljs-comment">// Create a node</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> {</span>
<span class="hljs-keyword">int</span> data;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">next</span>;</span>
};
<span class="hljs-comment">// Insert at the beginning</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertAtBeginning</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-comment">// Allocate memory to a node</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">new_node</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>*)<span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>));</span>
<span class="hljs-comment">// insert the data</span>
new_node->data = new_data;
new_node->next = (*head_ref);
<span class="hljs-comment">// Move head to new node</span>
(*head_ref) = new_node;
}
<span class="hljs-comment">// Insert a node after a node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertAfter</span><span class="hljs-params">(struct Node* prev_node, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-keyword">if</span> (prev_node == <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"the given previous node cannot be NULL"</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">new_node</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>*)<span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>));</span>
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
<span class="hljs-comment">// Insert the the end</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertAtEnd</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">new_node</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>*)<span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>));</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">last</span> = *<span class="hljs-title">head_ref</span>;</span> <span class="hljs-comment">/* used in step 5*/</span>
new_node->data = new_data;
new_node->next = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">if</span> (*head_ref == <span class="hljs-literal">NULL</span>) {
*head_ref = new_node;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">while</span> (last->next != <span class="hljs-literal">NULL</span>) last = last->next;
last->next = new_node;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Delete a node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deleteNode</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> *<span class="hljs-title">temp</span> = *<span class="hljs-title">head_ref</span>, *<span class="hljs-title">prev</span>;</span>
<span class="hljs-keyword">if</span> (temp != <span class="hljs-literal">NULL</span> && temp->data == key) {
*head_ref = temp->next;
<span class="hljs-built_in">free</span>(temp);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Find the key to be deleted</span>
<span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">NULL</span> && temp->data != key) {
prev = temp;
temp = temp->next;
}
<span class="hljs-comment">// If the key is not present</span>
<span class="hljs-keyword">if</span> (temp == <span class="hljs-literal">NULL</span>) <span class="hljs-keyword">return</span>;
<span class="hljs-comment">// Remove the node</span>
prev->next = temp->next;
<span class="hljs-built_in">free</span>(temp);
}
<span class="hljs-comment">// Search a node</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">searchNode</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">current</span> = *<span class="hljs-title">head_ref</span>;</span>
<span class="hljs-keyword">while</span> (current != <span class="hljs-literal">NULL</span>) {
<span class="hljs-keyword">if</span> (current->data == key) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
current = current->next;
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
<span class="hljs-comment">// Sort the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sortLinkedList</span><span class="hljs-params">(struct Node** head_ref)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> *<span class="hljs-title">current</span> = *<span class="hljs-title">head_ref</span>, *<span class="hljs-title">index</span> = <span class="hljs-title">NULL</span>;</span>
<span class="hljs-keyword">int</span> temp;
<span class="hljs-keyword">if</span> (head_ref == <span class="hljs-literal">NULL</span>) {
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">while</span> (current != <span class="hljs-literal">NULL</span>) {
<span class="hljs-comment">// index points to the node next to current</span>
index = current->next;
<span class="hljs-keyword">while</span> (index != <span class="hljs-literal">NULL</span>) {
<span class="hljs-keyword">if</span> (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
<span class="hljs-comment">// Print the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(struct Node* node)</span> </span>{
<span class="hljs-keyword">while</span> (node != <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">" %d "</span>, node->data);
node = node->next;
}
}
<span class="hljs-comment">// Driver program</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">head</span> = <span class="hljs-title">NULL</span>;</span>
insertAtEnd(&head, <span class="hljs-number">1</span>);
insertAtBeginning(&head, <span class="hljs-number">2</span>);
insertAtBeginning(&head, <span class="hljs-number">3</span>);
insertAtEnd(&head, <span class="hljs-number">4</span>);
insertAfter(head->next, <span class="hljs-number">5</span>);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Linked list: "</span>);
printList(head);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nAfter deleting an element: "</span>);
deleteNode(&head, <span class="hljs-number">3</span>);
printList(head);
<span class="hljs-keyword">int</span> item_to_find = <span class="hljs-number">3</span>;
<span class="hljs-keyword">if</span> (searchNode(&head, item_to_find)) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n%d is found"</span>, item_to_find);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n%d is not found"</span>, item_to_find);
}
sortLinkedList(&head);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nSorted List: "</span>);
printList(head);
}</code></pre></div>
</div>
<div class="code-editor__area" id="cpp-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="cpp hljs"><span class="hljs-comment">// Linked list operations in C++</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-comment">// Create a node</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> {</span>
<span class="hljs-keyword">int</span> data;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">next</span>;</span>
};
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertAtBeginning</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-comment">// Allocate memory to a node</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">new_node</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>*)<span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>));</span>
<span class="hljs-comment">// insert the data</span>
new_node->data = new_data;
new_node->next = (*head_ref);
<span class="hljs-comment">// Move head to new node</span>
(*head_ref) = new_node;
}
<span class="hljs-comment">// Insert a node after a node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertAfter</span><span class="hljs-params">(struct Node* prev_node, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-keyword">if</span> (prev_node == <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"the given previous node cannot be NULL"</span>;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">new_node</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>*)<span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>));</span>
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
<span class="hljs-comment">// Insert at the end</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertAtEnd</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> new_data)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">new_node</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>*)<span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">Node</span>));</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">last</span> = *<span class="hljs-title">head_ref</span>;</span> <span class="hljs-comment">/* used in step 5*/</span>
new_node->data = new_data;
new_node->next = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">if</span> (*head_ref == <span class="hljs-literal">NULL</span>) {
*head_ref = new_node;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">while</span> (last->next != <span class="hljs-literal">NULL</span>) last = last->next;
last->next = new_node;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Delete a node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deleteNode</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> *<span class="hljs-title">temp</span> = *<span class="hljs-title">head_ref</span>, *<span class="hljs-title">prev</span>;</span>
<span class="hljs-keyword">if</span> (temp != <span class="hljs-literal">NULL</span> && temp->data == key) {
*head_ref = temp->next;
<span class="hljs-built_in">free</span>(temp);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Find the key to be deleted</span>
<span class="hljs-keyword">while</span> (temp != <span class="hljs-literal">NULL</span> && temp->data != key) {
prev = temp;
temp = temp->next;
}
<span class="hljs-comment">// If the key is not present</span>
<span class="hljs-keyword">if</span> (temp == <span class="hljs-literal">NULL</span>) <span class="hljs-keyword">return</span>;
<span class="hljs-comment">// Remove the node</span>
prev->next = temp->next;
<span class="hljs-built_in">free</span>(temp);
}
<span class="hljs-comment">// Search a node</span>
<span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">searchNode</span><span class="hljs-params">(struct Node** head_ref, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">current</span> = *<span class="hljs-title">head_ref</span>;</span>
<span class="hljs-keyword">while</span> (current != <span class="hljs-literal">NULL</span>) {
<span class="hljs-keyword">if</span> (current->data == key) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
current = current->next;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-comment">// Sort the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sortLinkedList</span><span class="hljs-params">(struct Node** head_ref)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> *<span class="hljs-title">current</span> = *<span class="hljs-title">head_ref</span>, *<span class="hljs-title">index</span> = <span class="hljs-title">NULL</span>;</span>
<span class="hljs-keyword">int</span> temp;
<span class="hljs-keyword">if</span> (head_ref == <span class="hljs-literal">NULL</span>) {
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">while</span> (current != <span class="hljs-literal">NULL</span>) {
<span class="hljs-comment">// index points to the node next to current</span>
index = current->next;
<span class="hljs-keyword">while</span> (index != <span class="hljs-literal">NULL</span>) {
<span class="hljs-keyword">if</span> (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
<span class="hljs-comment">// Print the linked list</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printList</span><span class="hljs-params">(struct Node* node)</span> </span>{
<span class="hljs-keyword">while</span> (node != <span class="hljs-literal">NULL</span>) {
<span class="hljs-built_in">cout</span> << node->data << <span class="hljs-string">" "</span>;
node = node->next;
}
}
<span class="hljs-comment">// Driver program</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">head</span> = <span class="hljs-title">NULL</span>;</span>
insertAtEnd(&head, <span class="hljs-number">1</span>);
insertAtBeginning(&head, <span class="hljs-number">2</span>);
insertAtBeginning(&head, <span class="hljs-number">3</span>);
insertAtEnd(&head, <span class="hljs-number">4</span>);
insertAfter(head->next, <span class="hljs-number">5</span>);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Linked list: "</span>;
printList(head);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\nAfter deleting an element: "</span>;
deleteNode(&head, <span class="hljs-number">3</span>);
printList(head);
<span class="hljs-keyword">int</span> item_to_find = <span class="hljs-number">3</span>;
<span class="hljs-keyword">if</span> (searchNode(&head, item_to_find)) {
<span class="hljs-built_in">cout</span> << <span class="hljs-built_in">endl</span> << item_to_find << <span class="hljs-string">" is found"</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">cout</span> << <span class="hljs-built_in">endl</span> << item_to_find << <span class="hljs-string">" is not found"</span>;
}
sortLinkedList(&head);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\nSorted List: "</span>;
printList(head);
}</code></pre></div>
</div>
</div>
</div>
</div>
<div class="tutorial-toc"><div class="tutorial-toc__inner"><h3 class="tutorial-toc__title">Table of Contents
<button class="btn btn--clear align-items-center">
<svg class="programiz-icon"><use xlink:href="/sites/all/themes/programiz/assets/feather-sprite.svg#x"></use></svg></button></h3><div class="tutorial-toc__links"><ul><li><a href="#definition">Introduction</a></li>
<li><a href="#traverse">Traverse a Linked List</a></li>
<li><a href="#add">Insert Elements to a Linked List</a></li>
<li><a href="#delete">Delete from a Linked List</a></li>
<li><a href="#search">Search an Element on a Linked List</a></li>
<li><a href="#sort">Sort Elements of a Linked List</a></li>
<li><a href="#operations">LinkedList Operations in Python, Java, C, and C++</a></li>
</ul></div></div></div> </div>
<!--second------------------------------------------->
<div>
<ul>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/stack.html">
Stack</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/queue.html">
Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/types-of-queue.html">