Linked list
Linked list
list
List as ADT
limitation:
Function Task
Traversin This operation is used to visit all nodes in the linked list
from either the start or the end of the list.
g:
Searchin This operation is used to find the specific node data is
present in the linked list or not.
g:
Display: This operation is used to print all node data in the linked
list from the start or end of the node.
Insert
Adding the new element
1 2 3
Inserting the Inserting the Inserting the
new element at new element at new element
the beginning of the end of the somewhere at
the list list the middle of the
list
Explanation
1 2
Inserting a new element at Inserting a new element at
the beginning or end the middle of the list
• requires resetting the respective • search operation is required to be
NEXT fields. performed to identify the point of
insertion.
Insertion at the BEGINNING
Condition 1: Condition 2:
Start Start
100
ne INF
NULL INFO
O \0
Step2: Else xt
a. Set NEXT[PTR] =
START
b. Set START = PTR
200
Step Operation
Step 1 Start
[check whether the list is empty or not ]
If START = NULL, Then /*meaning the list is empty
Step 2 a. set START := PTR , where PTR is the new
node, and
b. set NEXT[PTR] := NULL, /*making it the only
node in the list.
Else
a. set NEXT[PTR] : = START and/*the new node
(PTR) will point to the current first node.
Step 3
b. set Start :=PTR /* updating the head pointer to
the new node.
[end of If structure]
Insertion at the END
Condition 1: Condition 2:
Start Start temp
100
ne 200
INF
NULL INFO
O \0
Step2:xt
Else
a. Set temp:= START
b. Repeat while
next[temp]!=Null
Set temp :=
INF
10 200 20 300
O \0
100 200 300
Step1:
a. Set temp:= START
b. Repeat while next[temp]!=Null
Set temp := NEXT[Temp]
End
c. Exit
Algorithm to insert an element at the END of a linked list.
Step Operation
Step
Start
1
a. Set temp:= START
Step b. Repeat while next[temp]!=Null
2 Set temp := NEXT[Temp]
End of while
Step
Stop
3
Insertion at some specific
position
1: Searching2: Insertion
requires two operations
Start
NULL
10 200 20 300 30 \0
100 200 300
temp temp
Item = 20 21 300
500
PTR
Algorithm to insert an element at the END of a linked list.
Step Operation
[check whether the list is empty or not ]
If START = NULL, Then
Step 1
a. set START := PTR and
b. set NEXT[PTR] := NULL
Else
a. Set temp := Start
b. Set item := [Read From User]
c. Repeat while NEXT[temp] != NULL
If INFO[temp] = item, then
set NEXT[PTR] :=NEXT[temp]
Step 2 set NEXT[temp]:= PTR
Break
Else
set temp := NEXT[Temp
[end of If]
[End of Loop]
Step 3 Stop
C Program for various Operations in Linked List
#include<stdio.h>
#include<stdlib.h>
}
break;
//Algorithm Ends