0% found this document useful (0 votes)
16 views

Linked List

This document discusses linked lists and their basic operations. It defines a linked list as a dynamic linear data structure with two fields - INFO and LINK. It describes the representation of a linked list with a FIRST pointer pointing to the first node. Each node contains data in the INFO field and a pointer to the next node in the LINK field. The last node's LINK contains a NULL value. Common operations on singly linked lists like insertion and deletion are also introduced.

Uploaded by

Urvesh Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Linked List

This document discusses linked lists and their basic operations. It defines a linked list as a dynamic linear data structure with two fields - INFO and LINK. It describes the representation of a linked list with a FIRST pointer pointing to the first node. Each node contains data in the INFO field and a pointer to the next node in the LINK field. The last node's LINK contains a NULL value. Common operations on singly linked lists like insertion and deletion are also introduced.

Uploaded by

Urvesh Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

4.5 Linked list presentation.

.Linked list is a dynamic linear data structure which contains two fields:

1. INFO and 2. LINK

.The representation of linked list is shown as below:

INFO LINK

Figure 4.1

a node

FIRST
INFO INK
NULL

.FIRST is a pointer which points to the first node


of the linked list.
.INFO field contains actual data and LINK field contains the pointer 10 the
next node.

LINK filed of the last node contains


special value as NULL value,
indicates end of linked list or termination of list.
In the linked list 2 elements are logically icauy

adjacent in the memory.


adjacent need not be phy
Linked List 9 9

.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

Each node contain link OR pointer to the next node.

.Alinked list is looking like:


NULL
2002
FIRST
A 2000 B

2000 2002
1998

Figure 4.2 Singly linked list

Advantages of Linked list


and save memory space.
It is a dynamic allocation of memory
and fast.
Insertion and deletion is easy
are easily executes with a
as stacks and queues
Linear data structure
linked list.
time without memory overhead.
Linked list can reduce access
Linked list
Disadvantage of
difficult.
Address calculation is very

is time consuming and complex.


Searching and sorting
in order from the beginning as linked
Nodes in a linked list must be read
access.
lists are inherently sequential

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

Dynamic in size. Static in size.

Number of elements is not fixed. Number of elements is fixed.

Allocation of memory at runtime. Allocation of memory at


compile
time.

Insertion and deletion are easy. Insertion and deletion are

complex.

Searching is time consuming. Searching is easy and take less


time.

Require pointer variable which| No need ofpointer variable.


occupy extra memory space.

Random access is not allowed.Random access is allowed.

4.6 Types of Linked List


Following are the types of linked list.
Singly linked list

Singly circular linked list

Doubly liked list


Linked List 101

. Doubly circular linked list


. Ordered linked list

4.7 Basic operation on Singly Linked List.


pifferent operations that can be performed on linked list are:
.Create a linked list.

Traversing a linked list.

.Insert a node at beginning (atfirst position).


Insert a node at the end.

Insert a node at any position.

.Insert a node in ordered linked list.

.Delete a node from beyinning.


Delete a node from end.

Delete a node (at any position).

Searching the linked list.


Copy a list.

Merging two lists.

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

have availability list


in which AVAIL pointer points:
nts to the f
Initially we
node of it.

When free node from it, first


we have to check that ava.
we want a

list is empty or not by:

if AVAIL = NULL
then "AVAILABILITYSTACK UNDERFLOW".
If there free nodes available, then NEW pointer pointing to the AVA
are

(first node of availability list) and AVAIL is now


pointing to t
LINK(AVAIL) that means AVAIL pointer points to the second node
nowa
first node been free to insert. This code written
by:
NEW AVAIL
AVAlIL 6 LINK (AYAL)
INFO(NEW) EX X is the value of the new node that we
want to insert).

4.7.1 Insert a new node at the


When we want to insert a new beginning of the linked list.
node is available or node in node, first we have to check whether au
.f
availability stack or not.
available, then we have to
and link it at the remove the first node from y stacd
starting position
of the availabiuy
.The detailed linked list.
algorithm to insert a node
description given below.
is at
beginning of the linkeu ked list mi
Linked Cut103

Algorithm: INSERT_START (X, FIRST)


the first element or
X is a new element to be inserfed, FIRST is a pointer to
linked list which contain INFO and LINK fields.

.AVAIL is a pointer to the first node in availability list.

.NEW is a temporary pointer variable.

Step1. [underflow?)
ifAVAIL= NULL
then write ("AVAILABILITY LIST UNDERFLOW")

return (FIRST)

Step2.fobtain address of next free node]


NEW AVAIL

Step3. [remove free node irom availabilitystack]


AVAIL LINK (AVAIL)
Step4. [initialize field of new node new node and insert

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

|Step5. return address of new node]


return (FIRST)

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

Step 4, initialize the fields


the linked list. linked list.
of the
at the
beginning
is inserted
tep 5, node

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

and ready to be inserted at the


Now, new node is free from availabiliy list
beginning of linked list.
write INFONEW) E X, LINK(NEW) FIRST and FIRST NEW.e
When we

is done:
following procedure

FIRST H H
NEW
s t a r t n g

node inserted at the


Now, our new
linked list is ready with new

position.
Linked List 1 0 5

4.7.2 Insert a node at the end of the linked list.

algorithm: INSERT_END (X, FIRST)


first element ot
. Xis a new element to be inserted, FIRST is a pointer to the
linked list which contain INFO and LINK fields.

AVAIL is a pointer to the node in availability list.


.NEW and SAVE are temporary pointer variables.

Step1. funderlow?]
if AVAIL = NULL

then write ("AVAILABILITY STACK UNDERFLOW")

retum (FIRST)

Step2. fobtain address of next free node]


NEW AVAIL
Step3. [remove free node from availability stack]

AVAIL LINK (AVAIL)


Step4.initialize fields of new node]
INFO (NEW) X

LINK (NEW) NULL

Step8. is the list empty?


if FIRST = NULL

then renurn (NEW)


| Step6. [initiate search for last node]
SAVE FIRST
Step7. [search for end of the list)
repeat while LINK (SAVE) # NUL
SAVE LINK (SAVE)
|Step8. (set LINK field of last node to NEW
LINK (SAVE) NEW
Data Structurns (3330704)
106

Step9.return first node pointer/


return (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,

I n step 9, return the linked list.

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

When we write LINK(SAVE) - NEW in step 8, the link pointer of SAVD


node which is having NULL pointer
right now, it is now pointing to the N
node and at that time NEW node is
inserted at the end of the linked s
Like:

FIRST

SAVE

NEW

Now our linked list is


ready with a new node inserted at the end o
LinkgdCist 107
4.7.3 Insert a node at specific
position.
We can insert a new node in the linked list at particular position we want.

algorithm: INSERT_POS (K, FIRST)


This procedure inserts a new node N into linked list at specifed by
address X.

.FIRST 1s a pointer to the first element of linked list which contain


LINK fields.
INroa
.AVAILis a pointer to the node in availability list.

.NEW and SAVE are temporary pointer variables.

Stepl.availability list underflow?]


ifAVAIL= NULL
then write ("AVAlLABILITY LIST UNDERFLOW")
return (FIRST)

Step2.(obtain address ofnextfree node]


NEW AVAIL
Step3. fremove free node from availabilitystacx]
AVAIL LINK (AVAIL)
Step4.[initialize INFO content of NEW node]
NFO (NEW) N
Step5.[is thelist empty?) (ut)
if FIRST = NUL

then LINK(NEW) E NULL

Return (NEW)

Step6. only first node?]8 6


NULl OR X = FIRST
if LINK (FIRST) =

then LINK (NEW) E FIRST

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

SAVE LINK (SAVE)


Step8. [node found?
LAINK (PRED) +NEW
LINK(NEW) SAVE

Step9. [finished]
return (FIRST)

Description
I n first step, checking for the free node in the availability list. I not

availability list is underflow.


I n second and third step get a free node from availability list.

I n step 4, initialize INFO field of new node.


I n step 5, check that list is empty or 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

After finding the targeted node, in step 8 we are writing: JINK(PRED)


the
NEW and LINK (NEW) 4-SAVE. At that time the NEW node is inserted at
specified position ahead of node X. like:

FIRST L
PRED SAVE

NEW

Now our linked list is ready with a NEW node inserted at position specified

by X.

4.7.4 Insert a node in sorted (ordered) linked list.


in the list are arranged in particular order
I n sorted linked list, all the data
or descending). It is also called ordered linked list.
(either ascending
position where the new node to be inserted.
W e have to find the proper
of each node.
For that we have to compare the value reside in INFO field
110 Data Structures (330704)

Algorithm: INSERT_ORD (X, FIRST)


X is a new element to be inserted, FIRST is a pointer to the first el.
rst elemem
anked list which contain INFO and LINK fields.

AVAIL is a pointer to the node in availability list.

NEW and SAVE are temporary


pointer varnables.
Step1. [availability list underlow?
if AVAIL =
NULL
then write ("AVAlLABILITY STACK UNDERFLOW")

return (FIRST)

Step2. [obtain address of next free node]


NEW AVAL

Step3. [remove free node from availability stack]


AVAILLNK (AVAIL)
Step4. [initialize INFO content of NEW node]
INFO (NEW Ex
Steps. [is the list empty?
if FIRST = NULL

then LINK (NEW) - NULL

return (NEW)

Step6. [does the new node precede all other


nodes?]
if INFO (NEW) INFO (FTRST)
S
then LINK (NEW) FIRST
return (NEW)

Step7. [initialize temporary pointer]


SAVE FIRST
Step8. [search for predecessor of new
node]
Linked List

repeat while LINK(SAVE) # NULL and INFO (LINK (SAVE)) |


INFO (NEW)
SAVE LINK (SAVE)
Step9. [set LINK fields of NEW node and its predecessor]

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 4, initialize the fields of the node which is to be inserted.


In Step 5 checking that only one node is there in the list?
In step 6, the value of the NEW node is less than first node, so it is inserted
at the beginning of the 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

then the NEW node will be the list.


If suppose the value of the node to be inserted is 8. So it will be inserted at

the first position in the list. Like:


Data Stnuctuns (3330704)
112

(to the list)

FIRSO10
LO
NEW

value step we are 25. In 8


insert a node having
&
Suppose we want to
the list. When we write LINK
ne position to insert a node in
NEW in step 9. The situation is 1
LINK(SAVE) and LINK(SAVE) E
slooks like

SAVE 2 5

NEW

Now our linked list is ready with a node 25 inserted at its proper
sition
positi
in ordered linked list.

4.7.5 Delete anode from thee starting position


of the linked lis.
Whenever we want to delete a node, we first have to check
whether the
list is empty or not. Because if list is
empty, it is underflow situation.
And when node is
a deleted, that would be inserted in the
availability list

Algorithm: DELETE_ FIRST (FIRST)


This procedure deletes a node from
the beginning of a
singly linked list
FIRST is a pointer to the first element of
linked list which and contain INFO
LINK fields.
AVAIL is a
pointer to the first node in
availability list.
TEMP is a
temporary pointer variable.
Linked List113

Stepl.[list is empty?]
ifFIRST NULL
then write ("LIST
UNDERFLOW")
return()
Step2. [initialize temporary variable]
TEMP - FIRST

Step3.[delete node]
if LINK (FIRST) NUJL*
=

then FIRST NULL


else FIRST LINK (FIRST)
Step4.[return node to the availability.list
LINK (TEMP) 4- AVAIL
AVAIL TEMP

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.

In step 4, move the deleted node to 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. 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.

the end of the línked list.


4.7.6 Delete a node from

W h e n we want to delete a node at the end,


hrst we need to reach

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

then write ("UNDERFLOW")

return)

Step2. [Initialize temporary variable]


TEMPEFIRST
Step3.[if only one node]
if(LINK(FIRST) = NULL)
LnkydCan
then FIRST NULL

Step3. Search target node]


repeat while (LTNK (TEMP) # NULL)
PRED +TEMP
TEMP LINK(TEMP)
Step4.(Delete a node]

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

When we write LINK(PRED) - INK (TEMP)


LNK(TEMP) in step 4 and LINKm
AVAIL, the situation will be:

FIRST
PRED
. TEMP

AVAIL

SO now, the last node is removed from linked list and in is


availability list.
pur

4.7.7 Search a node in the linked list.


Algorithm: SEARCH (FIRST, X)
This algorithm searches the given element in the list.

FIRST is a pointer to the first element oflinked list.


X i s the element which we want to search.

SAVE is a temporary variable. POS is the variable showing position of te


searched element.

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)

SAVE LINK (SAVE)


POS POS+1
if (INFO(SAVE) # X)
then write("Node is not in the list)
return)
else write("Node is found at")
write(POS)
Step4. Finished]
return)

Description

I n step 1, we initialize POS variable and SAVE pointer.

.In step 2, we check for the list is empty or not.

f not empty, then in step 3, we check the searched element () in the


linked list up to the end of the list, and incrementing the POS variable.

When the particular node is found, display the appropriate message.

4.7.8 Count the number of nodes in the linked list.


Algorithm: CoUNT (FTRST)
This procedure counts the number of nodes in the list

FIRST is a pointer points to the first node oflinked list which contain INFOo
and LINK fields.

SAVE is a temporary variable


count is a variable counting number of nodes.
118 Data Structures (3330704)

Step1. fempty list?)


ifFIRST NULL
then write ("LIST IS EMPTY")

Step2. [initialize]
count1
SAVE FIRST
Step3. process the list until end of the list)

repeat while LINK (SAVE) # NULL


count count + 1

step4. [finished]
return (count)

Description
I n step 1, check for empty list.

.Initialize count variable and SAVE pointer.


I n step 3, moving up to the last node and counting number ofnodes
Return the count.

Note: C program to implement singly linked is given at the end of ta


unit.)
4.8 Concept of Circular Linked List.
A list in which last node contains a ink or pointer to the first node
in
list is known as circular linked list. So, first node and last node "
adjacent.
Representation of circular linked list is given below:

Figure 4.4 Circular linked list.


Linked List 119
To detect the end of the list in
Circular List, we use one special node ca
HEAD node.(shaded as above).
,
Circular linked list can be categorized into:
.Singly circular linked list
.Doubly circular linked list

Operations on circular linked list.


. Traversal: access every node in
once a
systematic manner.
Insert a node: insert a
node in the circular linked list.
new

.Delete a node: delete a node in the circular linked list.

. Print: display the cireular linked list.

Advantages of Circular Linked List are:

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.

Concatenation and splitting operations are efficient in Circular List rather


than Singly List.
I n comparison with doubly linked list, there is saving of time to go to first
node from last node. No need to traverse the whole list backward like
doubly linked list.
Circular linkedlistcan be used to implement queues.

Disadvantage of Cireular Linked List:


fcare is not taken in processing, it is possible to get into infinite loop.
We can't traverse in reverse direction like doubly linked list.
(3330704)
120 Data Structures

4.9 Singly linked


list and ircular lin
Circular lint.
Difference between

Circular Linked list


Singly Linked List
In this list, last node has the NULLMain difference is that the la hat the last
value. Not adjacent to first node. is adjacent to first node. (iny,ofnea
node point to the first node).

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

4.10 Doubly Linked List and its basic operations.


In singly linked list, we are able to traverse in only one direction. enu

circular linked list, we can access any node from E


given node.
But in both cases, we can't move to
previous node. This is possible in
of doubly linked list.
Linkyd List 121

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.

to the predecessor node and


another point to tne
One
link 1s used to point
successor node of the node.

Thus, each node in the list contains three fields.


1. INFO

2. LPTR (left pointer points to the predecessor node).


node).
3. RPTR (right pointer points to the successor

LPTR INFO RPTRJ


NODE
in
be written
as "a ist
doubly linked list
can
of the of the node
So the definition
to the predecessor
contains two links one
which each node list or two
knowm as doubly linked
to the successor of the node, is
and one

way chains."

linked list is shown


in below figure.
Representation of doubly

linked list
Figure 4.5 Doubly

Jn above figure: node in the ist.


to the left
most
denotes
pointer points
or
Lis a
most node in the list.
denotes to the right
Ris
a pointer denotes
predecessor
node of that node.

is a left pointer node.


LPTR
denotes
s u c c e s s o r
node of that
RPTR isa right pointer
data value.
INFO field
contains
NULL i n d i c a t e s
the end oflist in
set to
most node
node and right
L e f t most
each direction.
Data Structuns (3IJO704)
122
are:
linked list
Advantages of doubly
In linked list can
traverse in one
direction
(forward)
singly we
traverse in
both forward and
list we can
in doubly linked
direction.
list
linked lis in com
Deletion operation is faster in case of doubly
mpare
singly linked list.
list.
It is easy to reverse the given linked
We can access any node
from the given node.
reduced.
Program execution time is
represented easily
tree can be
The hierarchical structure of the
doubly linked list.

list are:
Disadvantages of doubly linked
node because one exra pointer 13
needed
It requires space per
more

point to previous node.


more time compare
with singly linked
Insertion and deletion take
we have to set.
because more number of pointers

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).

It occupies more memory.


It occupies less memory.
is slower Deletion operation is faster compare
operation
Deletion with singly linked list.
compare with doubly linked list.

Hard to implement compare


to sing
|Easy to implement.
linked list.

You might also like