Linked List
Linked List
.Linked list is a dynamic linear data structure which contains two fields:
INFO LINK
Figure 4.1
a node
FIRST
INFO INK
NULL
.Linked list is a
self referential structure, in which structure having one
the member variables
o
pointer pointing to the
as same type of structure.
. Linked list can be described in following way.
struct node
int INFO;
srtuct node * link; / / referential to itself
2000 2002
1998
Difficulties arise in
linked list at the time ofreverse traversing.
Data Structures (3330704)
100
Availability List
a new
node whenever a1
A used to acquire
pool of free node is
to be inserted, is called availability list.
Management.
is Memory
ne
advantage of availability list
In operation, the inserting node
would from availat
availah
be available fYom
insertion
list and in deletion operation deleted node put into the avallabilitylist,lability list.
ference between Linked list and Sequential list
Linked list
Sequential list
complex.
Sorting a list.
Concatenating two lists.
linked list, here we see a
the different algorithms of
Before understanding different operations
will be in understanding the
helpful
basic concept that
on linked list.
free node from
node in linked list, we
have
to take a
Whenever we inserta the chain of free nodes in
which
list contains
availability list. Availabilityfirst
AVAL pointer points
to the noder
to make a node free
from availability
following process
We have to do the
list.
Data Structures (3330704)
102
NEW NFO LNK to the lis
AVAIL NO LANK
AVWIL
(after
Tbefore)
from availability list
free node
Figure 4.3 remove
if AVAIL = NULL
then "AVAILABILITYSTACK UNDERFLOW".
If there free nodes available, then NEW pointer pointing to the AVA
are
Step1. [underflow?)
ifAVAIL= NULL
then write ("AVAILABILITY LIST UNDERFLOW")
return (FIRST)
INFO(NEW)6X
ifFIRST NULL (checking for insertion in the empty list)
thenLINK(NEW) NULL
return(NEw
else
LINK(NEW) FIRST if list is not empty)
FIRST NEW
Description
I n first step, checking for the free node in the availability list. If not,
availability list is underflow.
I n second and third step get a free node from availability list.
Data Structuns (3330704)
104
of the
node
which serted and link
is to be insert
given
below for
better
erstanding
understandihe
Figurative description is going to insert
rt a new
which we are n
Initial linked list is shown belowin
the starting position.
FIRST
not empty. It
looks like:
list is checked and it is
intially availability
H
AVAIL
LINK(AVAIL). The follow
When we write NEw AVAIL and AVAIL
procedure is done:
NEW
is done:
following procedure
FIRST H H
NEW
s t a r t n g
position.
Linked List 1 0 5
Step1. funderlow?]
if AVAIL = NULL
retum (FIRST)
Description y
in the availability list
list. If
the free node n
for
ln first step, checking
underflow.
availability list is list.
from availability
free node
I n second and third step get a
which is to be inserted.
Step 4, initialize the fields ofthe node
node is there
in the list?
one
In Step 5 checking that only
to first node
and check for the last nod
node
I n step 6 and 7, SAVE pointer
set
new node is
inserted there.
I n step 8, when found the last node,
Figurative description.
When in step 6 we write
Up to step 4 we have seen in the above algorithm.
SAVE FIRST, the SAVE pointer is pointing to the FRST node. And in step
7 we are traversing the linked list up to the last node (where the link par
of any node is NULL, it is the last node of the list). At this time the situation
is:
FIRST H
SAVE
FIRST
SAVE
NEW
Return (NEW)
FIRST NEW
Data Structures (3330704)
108
Return (FIRST)
desired address1ound
|Step7. search the list until
SAVE FTRST
SAVE #X
Tepeat while LINK (SAVE) # NULL
and
PRED SAVE
Step9. [finished]
return (FIRST)
Description
I n first step, checking for the free node in the availability list. I not
.In step 6, if only one node is there in the list or node X is the first node
then inserting a new node at first position.
. I n step 7, initialize SAVE pointer and finding for the address of node .
After finding the address X, in step 8, LINK field of new node is setthe
return the linked list in step9.
Figurative description.
from
We already know what happens up to step 4. (removing a free noa
availability list).
Linked List109
. I n step 5, 1f we have only one node or the X node is the first node, then
NEW node is inserted at the starting position of the list. Like:
FIRSO
NEW
I n step 1, using two temporary pointers PRED and SAVE, we are finding
our targeted node X up to the end of the list. Suppose the node X is there
at position 3, so the situation of PRED and SAVE pointer will be like:
FIRST
PRED SAVE
FIRST L
PRED SAVE
NEW
Now our linked list is ready with a NEW node inserted at position specified
by X.
return (FIRST)
return (NEW)
LINK(NEVw) LINK(SAVE)
LINK(SAVE) - NEWv
Step10. [return first node]
return (FIRST)
Description
In first step, checking for the free node in the availability list. If not,
availability list is underlow.
In second and third step get a free node from availability list.
In step 7 and 8 initialize SAVE pointer and checking for the proper place
to be inserted.
After finding the proper place, in step 9 set the LINK feld of new node and
in step 10 return the linked list.
Figurative Description
Suppose we have the ordered linked list as given below:
FIRSL0T -0 | 30 40
We already know what happens up to step 4. In step 5, if the list is empty
FIRSO10
LO
NEW
SAVE 2 5
NEW
Now our linked list is ready with a node 25 inserted at its proper
sition
positi
in ordered linked list.
Stepl.[list is empty?]
ifFIRST NULL
then write ("LIST
UNDERFLOW")
return()
Step2. [initialize temporary variable]
TEMP - FIRST
Step3.[delete node]
if LINK (FIRST) NUJL*
=
return0
Description
Step 1, check for the underflow.
Step 2, initialize TEMP pointer to first node.
In step 3, delete the first node and set the LINK field of deleted node to the
next node.
Figurative description.
After step 2 is executed, TEMP pointer is pointing to FIRST, and in step 3,
if there is only one node in linked list it is removed and put into availability
list. Ifthere are more than nodes in linked list, the situation after FIRST
AVAIL will be
LINK (FIRST) and LINK(TEMP -
Data Structures (3330704)
114
FIRST
TEMP
AVAIL
de is
node. removed and it is
From above figure, we can
see that first
the availability list.
how to reach at th
atg t
seen the logic
end of linked list. We have already
Here we wil use TEMP poi
of the list.
(using SAVE and PRED pointer).
instead of SAVE pointer.
END (FIRST)
Algorithm: DELETE
linked list.
from the end of a singly
This procedure deletes a node
element of linked list
which contain INFO a
FIRST is a pointer to the first
LINK fields.
list.
AVAIL is a pointer to the first node in availability
temporary pointer
variables. PRED keeping trackd|
.TEMP and PRED are
predecessor node.
Step1.[List is empty?]
i fFIRST NULL
return)
LINK(PRED) NULL
Step5.[Peturn node to the availability list]
LINK(TEMP) +AVAIL
AVAIL TEMP
return0
Description
Step 1, check for the underlow.
In step2, TEMP variable is pointing to FIRST node.
I n step3, we are reaching up to the last node. If only one node is there,
then it is removed. Else using TEMP and PRED pointers reaching at the last
node.
After reaching at the last node, in step4, delete the last node and in step5,
NULL is set to the predecessor node and deleted node will be inserted in
the availability list.
Figurative description
After step 2 is executed, TEMP pointer is pointing to FIRST, and in step 3,
if there is only one node in linked list it is removed and put into availability
list. And if more than nodes are there, we have to reach at the lat node
track of the predecessor node
using TEMP pointer, PRED pointer keeping
of the TEMP node. When we reach at the last node, the situation is looks
like
16Data Structurs(3330704)
FIRSL
PRED
TEMP
FIRST
PRED
. TEMP
AVAIL
Step1. initialize]
POS 1
SAVE FIRST
Step2.[Linkedlist empty?]
if(FIRST= NUL)
then write ("List is empty")
Linked List117
return()
Step3./Search element
repeat while (INFO(SAVE) # X AND LINK(SAVE) # NULD)
Description
FIRST is a pointer points to the first node oflinked list which contain INFOo
and LINK fields.
Step2. [initialize]
count1
SAVE FIRST
Step3. process the list until end of the list)
step4. [finished]
return (count)
Description
I n step 1, check for empty list.
Every node in the list can be accessible from the given node. As in singly
linked list we can't go to previous node.
There is no need to give the address of the FIRST node in the list.
Every node
is not accessible like||Every node is accessible ole from
given node.
circular linked list.
here is a need of first node's address| There isn't any need to have the s
the fire
while deleting a node. node's address while deleting
node
ing
Not so efficient in this type of list. Splitting and
concatenatioa
operations are more efficient in th
list.
No chance to get into infinite loop. There is a chance of getting into
infinite loop.
This list should be empty. This list never be
empty due to|
HEAD node. So no need to checkfor|
empty list in its operations
FPST
HEAD
traverse in Dot
.This property of doubly linked list implies that we can
directions. For that each node in this list contains two LINK fields inste aa
of one.
way chains."
linked list
Figure 4.5 Doubly
list are:
Disadvantages of doubly linked
node because one exra pointer 13
needed
It requires space per
more
linked list.
Difference between Singly linked list and Doubly
Doubly Linlked List
Singly Linked List
directions.
direction Traversing in both
Traversing in only
one
| (forward).