You are on page 1of 77

LINKED LIST

INTRODUCTION

• Firstly the size of array is fixed due to which either a


lot of memory is wasted if we have very few
elements in the array or undesriable result are
produced.
• Secondly the insertions and deletions of elements is
complex and inefficient process as large number of
elements are to be shifted to make room for new
element or delete the existing element.
• So to overcome this situation we use linked list.
LINKED LIST
A link list or one way list is a linear collection of
data elements called nodes, where the linear
order is given by means of pointers. Each node
is divided into two parts:
1.Contains the information of the element
2.Contains the address of the next node in the
list.

3
• Linked list is a liner data structure consisting of
elements called nodes where each node is
composed of two parts:
• Information part .
• Link part (called next pointer part)
• The information part contains user supplied data
and the link part is a pointer which points to the
next node in the linked list.
• The link part of the last node is set to NULL that
marks the end of the list
Representation of Link List in Memory

BED Number Patient


Next
1 Kirk
7
2
3 Daen 11
4 Maxwell 12
5 Adams 3
5
6
7 Lane 4
Start
8 Green 1
9 Samules 0
10
11 Fields 8 6

12 Nelson 9
• A linked list may not contain any node at all.Such a
list with no nodes at all is called an empty list.The
empty list is represented by setting the HEAD
pointer to NULL value.
• START or HEAD pointer is a special pointer which
points to the first node of the linked list.
• Linked list is a dynamic data structure as number of
elements are not pre-defined.Any number of
elements can be added or deleted easily.
• Linked list is also a linear data strucutre.
• Although the nodes are stored independently (i.e at
different memory locations),linearity is provided by
the links that binds them together.
• Linked lists are useful in applications where access
of elements is not at random and dynamic memory
allocation and deallocation of data elements are
frequently needed.
OPERATIONS ON LINKED LIST

• Traversing : visiting each element is the list.


• Searching : Finding the location of desired element
• Insertion : to insert new element in the list.
• Deletion : to remove an existing element from the
list
1.TRAVERSING
• Traversing involves processing each node of the
linked list exactly once from the beginning to the
end following the chain of reference..
• Let us consider a linked list LIST stored in memory
with the pointer HEAD pointing to the first node
and NULL indicating the end of the list.
• While traversing we need to process each node and
for this we use a pointer variable PTR that keeps
track of the current node being processed.
45 . 26 X
HEAD
29 .
Node-PTR NULL
INFO(PTR) LINK(PTR)
• ALGORITHM is as follows:
ALGORITHM1.1:Traversing a Linked List
Traverse(Head):Given LIST has its first node address stored in
HEAD pointer.This algorithm traverse list by applying an operation
PROCESS to each node of the LIST.we use local variable PTR that
points to the current node being processed in LIST.

1.PTRHEAD[PTR pointing to first node]


2.Repeat steps 3 and 4 while PTR !=NULL.
3.Apply PROCESS to INFO(PTR).
4.PTRLINK(PTR) [PTR points to next node].
{End of step2 loop}
5.Return.
Algorithm1.2: To show the PROCESS
• PRNT_COUNT(HEAD):This algorithm counts the number of node
in the LIST and also prints the information of each node.For this we take
a local variable COUNT that stores the total number of nodes in the list.
1.COUNT 0 [Initialize Counter]
2.PTRHEAD [PTR pointing to first node]
3.Repeat steps 4 to 6 while PTR!=NULL
4.COUNTCOUNT+1 [Increment Counter].
5.Write INFO(PTR) [Print information of current node]//PROCESS
6.PTRLINK(PTR) [PTR points to next node]
[End of step 3 loop]
7.Return.
2.SEARCHING
• It is the process of finding the location of the node
containing the desired item in the linked list.
The searching of the linked list elements can be
performed in two ways:
1.List is unsorted
2.List is sorted.
LIST be a linked list and ITEM is element to be
searched . The two algorithms can be used for
finding the location LOC of the node where ITEM
first appears in the list.
ALGORITHM 2.1:Searching in Unsorted list
SEARCH(HEAD,ITEM)-In the given LIST ,first node address is stored in
HEAD.This algorithm finds and return the location LOC of the node where ITEM
first appears in the LIST if the search is successful,else null.Here LOC is a local
variable

1.LOC NULL [Assume search is unsuccesfull].


2.PTRHEAD [Initialize PTR to first node].
3.[Search for ITEM]
Repeat step 4 while PTR !=NULL and ITEM !=INFO(PTR)
4.PTRLINK(PTR) [PTR points to next node]
[end of step 3 loop].
5.If ITEM=INFO(PTR) Then [SUCCESFULL]
LOCPTR
[End of IF]
6.Return LOC.
Algorithm 2.2:Searching in Sorted List
SEARCHSORT(HEAD,ITEM)-In given LIST,the
HEAD contains address of first node.This
algorithm finds and return the location LOC of
the node where ITEM first appears in the
LIST if the search is successful or sets
LOC=NULL,if search is unsuccessful .Here
LOC is local variable.
1.PTRHEAD[Initializing PTR to first node]
2.LOCNULL[Initially assume unsuccessful search]
3.Repeat step 4 while PTR != NULL.
4.[Search for ITEM]
If ITEM > INFO(PTR) then
PTRLINK(PTR) [PTR points to next node]
ELSE if ITEM=INFO(PTR) then [ITEM found]
LOCPTR [Update LOC with target’s LOC and return]
return LOC
Else
PTRNULL
[End of IF strucutre]
[End of Step 3 loop]
5.Return LOC
• We cannot apply binary search for searching the
sorted linked list as there would be no way to refer
the middle node of the linked list directly.
• This property of linked list where we cannot access
any node randomly because of its sequential nature
is the main drawback of using linked list as a data
strucutre.
MEMORY ALLOCATION

• Unlike arrays,linked list doesnot come with pre-


supplied set of memory locations into which nodes
can be placed.Instead a free pool known as
availablity list or free storage list is maintained in
conjunction with linked list that consist of free
nodes.
• This availability list is maintained in memory as a
separate linked list of free nodes with its own head
pointer AVAIL.
OVERFLOW

• A situation where we want to insert a new element


into a linked list but the free storage list is empty.
• This situation is normally called overflow which
means that all the nodes of the free storage list are
currently in use and more memory is not available
for allocation.
• The overflow occur during the insertion operation
on linked list when AVAIL=NULL
UNDERFLOW
• Underflow occurs if we want to delete an element
from a linked list which is already empty.
• The underflow occurs during the delete operation
of a linked list when HEAD=NULL.
GARBAGE COLLECTION

Garbage Collection (GC) is a form of automatic memory


management. The garbage collector, or just collector, attempts
to reclaim garbage, or memory occupied by objects that are no
longer in use by the program.
• In the first step,the garbage collector perodically scans the lists
in memory and mark those locations which are still is use.
• In the second step ,the garbage collector again scans the lists in
memory,collects the unmarked memory locations which are
then freed and entered into the free storages list for future use.
3.INSERTION IN LINKED LIST
• Insertion into an unsorted linked list is generally
performed either at the beginning or at the end of
the list .In sorted linked list ,new data can be
inserted in the middle but the order has to be
maintained.
• To insert a new node into a linked list , we need to
first create space and then insert data in the INFO
part.
• The space for the new node is made available from
the free storage list or AVAIL list.
• If AVAIL=NULL(space is not available) no insertion
could be done
• If AVAIL !=NULL(Node available) then the first node
from the free storage list is removed and pointer
variable NEW is used to point to this new node.
• On removing the first node from the free storage
list,AVAIL pointer now points to next node in the free
storage list.
• Finally data is inserted into INFO(NEW)<-ITEM
3.1INSERTION AT THE BEIGNNING OF
LIST
• INSTBEG(HEAD,ITEM,AVAIL)-Given LIST first
node address is stored in HEAD. This
algorithm inserts ITEM as the first node in
the linked list. Here we use a local variable
NEW that points to new node and AVAIL
pointer pointing to the first node of free
storage list
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return
[end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.[make the new node’s link part points to first node of the list]
LINK(NEW)HEAD //as head contain address of first node,now new contain address of first node.
6.[Update HEAD pointer so that it points to new node]
HEADNEW
7.Return
3.2INSERTING NODE AFTER A GIVEN
NODE
• We first create a new node from the free storage list.
• We check the value of LOC,if LOC=NULL then new
node is inserted as the first node of list.
• If LOC !=NULL,we point the new node N to the
successor node X.The address of new node’s
successor can be found from node X’s link part.
• After this we set the link part of node X to now point
to the new node N..
• INSTLOC(HEAD,ITEM,AVAIL,LOC)-Given LIST first node
address is stored in HEAD.This algorithm inserts ITEM either
after the node whose location is LOC or inserts ITEM at the
front if LOC=NULL.Here we use a local pointer variable NEW
that points to a new node.
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return
[end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.If LOC =NULL then [Insert new node at the beginning]
LINK(NEW)HEAD
HEADNEW
Else [Inserts after node X having location LOC]
LINK(NEW)LINK(LOC) [Update link part of new node]
LINK(LOC)NEW [Update link part of node X to point to new node]
[End of If Structure]
6.Return
3.3 INSERTING A NODE AT THE END
OF LIST
• Create new node from the free storage list and store ITEM into
INFO part of this node.
• LINK part must be set to NULL.
• We use a pointer variable PTR which is first intialized to
HEAD and then we navigate through the nodes in the list till
you get a node whose link part is NULL.
• INSTEND(HEAD,ITEM,AVAIL)-Given LIST’s first
node is stored in HEAD.This algorithm inserts ITEM
at the end of the list.Here we use two local pointer
variables NEW and PTR.The NEW points to the new
node.The PTR navigates the linked list so as to find
the location of the last node after which new node is
to be inserted.
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return
[end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.LINK(NEW)NULL [Set link part of the new node to NULL]
6.[Searches for location of last node of linked list]
PTRHEAD [Intialize PTR to HEAD]
7.Repeat while LINK(PTR) !=NULL
PTRLINK(PTR)
[End of loop]
8.LINK(PTR)NEW [Set link part of PTR to point to new node]
9.Return.
3.4INSERTION IN THE SORTED LIST
INSTSORT(HEAD,ITEM,AVAIL)-Given LIST’s first node
address in contained in HEAD.This algorithm inserts
ITEM into sorted linked list based on the value of
ITEM.The local variable PTR points to the current
node being processed and pointer variable PREVPTR
points to its predecessor.The local variable NEW
points to the new node and AVAIL pointer points to
the first node in the free storage list.
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return [end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.LINK(NEW)NULL [Set link part of the new node to NULL]
6.[Is the list empty?]
If HEAD=NULL then
HEADNEW [set NEW as HEAD of new list]
Return [end of if structure]
7.[Traverse the list if non-empty]
PTRHEAD [set PTR to HEAD of the list]
8.PREVPTRNULL [set PREVPTR to NULL]
9.Repeat while PTR!=NULL and ITEM>INFO(PTR)
[Update pointers]
a)PREVPTRPTR
b)PTRLINK(PTR)
[End of step 9 loop]
10.[Does new node precede all nodes in list?]
If PREVPTR =NULL then [Insert new node at the front]
a)LINK(NEW)HEAD
b)HEADNEW
Else [Insert new node somewhere in the middle or at the end]
a)LINK(NEW)PTR
b)LINK(PREVPTR)NEW
[End of step 10]
11.Return.
DELETION

• Deletion operation logically removes a node from


the linked list by changing various link pointers.
• Once the node is deleted the space taken by the
node in the linked list must be reclaimed and added
to the free storage list.
• To delete a node we not only require a pointer
pointing to the target node but also require a
pointer pointing to its predecessor.
• Various situtaions where a node can be deleted
from a linked list are:
• Deleting a node from somewhere middle of the list
• Deleting the first node of list.
• Deleting the last node of list.
DELETING NODE FROM MIDDLE OF
LIST
• Let LOC be a pointer pointing to the target node(N)
and PREVLOC be the pointer pointing to its
predecessor.
• The node N pointed by LOC is logically removed by
simply changing the link part of its predecessor
(pointed by PREVLOC) to point to the link part of the
target node using following statement.
LINK(PREVLOC)LINK(LOC)
After removing the target node from the list ,it
must be returned to the pool of unused available
nodes using
LINK(LOC)AVAIL
AVAILLOC
• This means the link part of the deleted node
pointed by LOC is set to the current value of AVAIL
and then value of the LOC becomes the new value
of AVAIL.
DELETING THE FIRST NODE OF LIST
• Let LOC be a pointer pointing to the first node to be deleted
then in that case LOC is equal to HEAD and pointer PREVLOC
that points to its predecessor is NULL as there is no node
preceding it.
• To perform this operation all we need to do is to move the HEAD
pointer to point to next node in the list,as following statement:
HEADLINK(HEAD)………????????
• This deleted node is then added as the first node of the free
storage list.
• If the first node is the only node then its link part is a NULL
pointer ,in that case above statement becomes
• HEADNULL
DELETE THE LAST NODE
• Removing the last node of the linked list requires
that its predecessor becomes the last node of the
linked list.
• In order to delete the last node of the linked list
pointed by LOC ,the link part of this node is moved
to the predecessor’s link part.
• This makes the predecessor node the new end of
the list.
• LINK(PREVLOC)LINK(LOC)[which is null]
• DELETE(HEAD,AVAIL,LOC)-In Given LIST ,HEAD
points to first node.This algorithm deletes a node
pointed by LOC.Here we are using local variable PTR to
find the desired node to be deleted and PREVLOC to
keep track of its predecessor.The local variable
AVAIL point to the first node of the free storage
list.
1.[Empty list?]
If HEAD=NULL then
Write “UNDERFLOW” ,return
[end of IF structure]
2.PTRHEAD[set PTR to HEAD of the LIST],PREVLOCNULL
3.[Find LOC so as to find predecessor’s location i.e PREVLOC]
Repeat while PTR!=LOC and LINK(PTR)!=NULL
[Update pointers]
a)PREVLOCPTR
b)PTRLINK(PTR)
[End of step 3 loop]
4.If PTR !=LOC then
Write “Node not found”
Return
[End of if structure]
5.[Delete node pointed by LOC]
If PREVLOC=NULL then [Deleting first node]
HEADLINK(HEAD)
ELSE
LINK(PREVLOC)LINK(LOC)
[end of if structure]
6.[Return deleted node to free storage list]
a)LINK(LOC)AVAIL
b)AVAILLOC
7.Return
OTHER VARIATIONS OF LINKED LIST
 Header linked lists: Grounded and Circular
 Two-way lists: operations on two way linked lists
Header Linked Lists
• Header linked list is a linked list which always contains a special
node called the Header Node, at the beginning of the list.
• It has two types:
a) Grounded Header List
Last Node Contains the NULL Pointer
b) Circular Header List
Last Node Points Back to the Header Node

Header linked list

Grounded Header linked list Circular Header linked list


48
HEADER LINKED LIST
• A header linked list is a linked list that always
contains a special node at the front of the list.This
node is called header node.
• It doesnot contain actual data item included in the
list but usually contains useful information about
the list like total number of nodes in the list or
pointer to last node in the list or the last node
accessed.
• The header node is never deleted in the list and
always point to the first actual node in the list.
• The main advantage of using header node in the
linked list is that we can avoid special testing involved
while inserting and deleting nodes from the linked list.
• As we don’t need to check the whether the list is
empty or not because header node give us that
information.
• To perform the basic operations on a header linked
list, the algorithm that we have discussed previously
need to be re-written so as to account for presence of
header node. As for traversing ,in order to point to
first node
PTRLINK(HEAD)
CIRCULAR LIST

• The main drawback of singly linked list is that from


a given node,we can access all the nodes that
follow it but not the one preceding it.
• To overcome this we make slight modification in
the single linked list by replacing the null pointer of
the last node of list with the address of its first
node.
• This is called the Circular Linked List
• The operations performed on a circular linked list are
similar to those of a single linked list except that we
need to remember that the last node does not contain
null pointer but points to the first node.
• Eg: for deleting the first node of a circular linked list we
need to change the link part of the last node and for
that we need to traverse the list to reach the last node.
• This problem can be solved using the TAIL pointer. That
points to the last node of the list.
• Now we can directly access the last node and first node
• Another alternative is to include the HEADER node
and such a linked list is called Circular Header
Linked List.
TRAVERSING A CIRCULAR LINKED LIST
• TRAV_CIR(HEAD)-Given LIST be a circular header
linked list in memory and address of its node (header
node) stored in HEAD pointer.This algorithm
traverses the list by applying an operation PROCESS
to each node of the list.We use a local variable PTR
that points to the current node being processed in the
list
1.PTRLINK(HEAD) [PTR points to first node]
2.Repeat step 3 and 4 while PTR!=HEAD
3.APPLY PROCESS to INFO(PTR)
4.PTRLINK(PTR) [Now PTR points to next node]
[end of step 2 loop]
5.Return
SEARCHING IN CIRCULAR LINKED LIST
• SEARCH_CIR(HEAD,ITEM)-Given a non-empty
unsorted header linked list LIST in memory and
address of its first node(header node) is stored in
HEAD pointer.This algorithm finds and return the
location LOC of the node where ITEM first appearsb
in the list if search is successful or sets LOC=NULL
,if search is unsuccessful .Here LOC is a local variable.
1.LOCNULL [Assume search unsuccesful]
2.PTRLINK(HEAD) [Intialize PTR to first node]
3.[search for ITEM]
Repeat step 4 while PTR!=HEAD and ITEM!=INFP(PTR)
4.PTRLINK(PTR)
[End of step 3 loop]
5.If ITEM=INFO(PTR) then [successful]
LOCPTR
[End of if structure]
6.Return LOC
DELETION IN CIRCULAR LINKED LIST
• DEL_ITEM_CIR(HEAD,ITEM,AVAIL)-Given
LIST be a circular header linked list and HEAD
pointer points to its header node.This algortihm
deletes the node of the circular linked list that coints
the given ITEM of the information.Here we are using
local variable PTR,PREVLOC,LOC and AVAIL.
1.[Traverse the list,step 1,2,3]
PTRLINK(HEAD) [Set PTR to first node]
PREVLOCHEAD [Set PREVLOC to header node]
Repeat while PTR!=HEAD and ITEM!=INFO(PTR)
[update pointers]
a)PREVLOCPTR
B)PTRLINK(PTR)
[End of step 3 loop]
4.if ITEM=INFO(PTR)
LOCPTR [update LOC,now points to target node]
ELSE
Write “Item not found”
Retun
[end of if structure]
5.[Delete node]
LINK(PREVLOC)LINK(LOC)
6.[Return node to free storage list]
LINK(LOC)AVAIL
7.AVAILLOC
8.Return
TWO WAY LIST/DOUBLY LINKED LIST
• A two-way list is a linear collection of data elements,
called nodes, where each node N is divided into three
parts:
– Information field
– Forward Link which points to the next node(FLINK)
– Backward Link which points to the previous node(PLINK)
• The starting address or the address of first node is
stored in START / FIRST pointer .
• Another pointer can be used to traverse list from end.
This pointer is called END or LAST.
Two-way lists(cont…)
• Every node (except the last node) contains the
address of the next node, and every node (except the
first node) contains the address of the previous node.
• A two-way list (doubly linked list) can be traversed in
either direction.
Representations of
Two-way lists
X 4 2 10 X
Start Last

BACK Pointer
FORE Pointer

INFO Field
A two way list is a linear collection of data elements, called nodes, where
each node N is divided into three parts:

1. An information field INFO which contains the data of N.


2. A pointer field FORW which contains the location of the next node in the list.
3. A pointer field BACK which contains the location of the preceding node in
the list.

64
TYPES DOUBLE LINKED LIST

A) Doubly Header Linked List-This variation of doubly


linked list contains a header node at the beginning
and HEAD pointer points to header node.
B)Double Linked Circular Linked List-The FLINK of last
node contain the address of first node and PLINK of
first node contain the address of last node.
C)Doubly Circular Header Linked List:It is extension
of doubly circular linked list with a header node.
OPERATIONS ON DOUBLY LINKED LIST
1.TRAVERSING:
• Doubly linked list can be traversed both ways and
the traversing algorithm is same as the traversing
the linked list,we start using HEAD pointer and
follow the chain of link using the pointers FLINK in
succession
• Similary in backward traversal we traverse in the
backward direction using TAIL pointer and follow
the chain of PLINK pointers in succession
2.SEARCHING:
• It involves the searching in of the LOC ,and usually
we search using the forward progression only same
as the way when searching in the single linked list.
• But if our list is sorted and we are searching for the
ITEM which may be at the end of the list then we
may use the TAIL pointer for ease of access.
3.DELETION:
Deletion is easy in doubly linked list as we have
information about the previous and next node.
The possible situation where nodes can be deleted
from the doubly linked list are:
If the list contains only a single node then deletion
results in empty list with both HEAF and TAIL
pointers set to NULL
• If the node to be deleted is the first node then we
need to change the HEAD pointer.
• If the node to be deleted is the last node then we
need to change the TAIL pointer.
• If the node
DELETION IN DOUBLY LINKED LIST

• DOUBLE_DEL(HEAD,TAIL,LOC): Given a
doubly linked list with the address of the leftmost
and rightmost nodes given by pointers HEAD and
TAIL resp. This algo deletes the node whose address
is contained in pointer variable LOC . The forward and
backward links of node are denoted by FLINK and
PLINK respectively.
1.[empty list?]
IF TAIL=NULL, then
Write ”Underflow”.
return
[End of if structure]
2.[Delete Node]
IF HEAD=TAIL, then [Only one node in the list]
HEADTAILNULL
Else If LOC=HEAD then [Leftmost node being deleted]
a)HEAD FLINK(HEAD)
b) PLINK(HEAD)NULL
Else If LOC=TAIL then[Rightmost node being deleted]
a) TAILPLINK(TAIL)
b) FLINK(TAIL)NULL
Else[Node being deleted appear somewhere in the middle of the list]
a) FLINK(PLINK(LOC))FLINK(LOC)
b) PLINK(FLINK(LOC))PLINK(LOC)
[end of if structure]
3. [Return deleted node to free storage list]
a) FLINK(LOC)-AVAIL
b) AVAIL LOC
4. Return
INSERTION IN LINKED LIST
Insertion IN Doubly Linked List
• DOUBL_INS(HEAD, TAIL,LOC, ITEM): Given doubly linked list whose left-most and
right-most node addresses are given by pointer variable HEAD and TAIL resp. This algo
insert a node to the left of specified node with address given by pointer variable LOC . Here
NEW is local pointer variable that points to new node to be inserted. The info to be entered
in the new node is contained in ITEM. The forward and backward links of node are denoted
by FLINK and PLINK resp.
1. IF AVAIL=NULL, then [Avail list is empty]
Write” Overflow”
Return
[end of if structure]
2. NEWAVAIL [create new node]
3. AVAILFLINK(AVAIL) [Update AVAIL]
4. INFO(NEW)ITEM [copy item into info part of new node]
5.[inserting new node in empty list?]
IF HEAD=NULL, then
a) PLINK(NEW)FLINK(NEW)NULL
b) HEADTAILNEW
return
[end of if structure]
6.[Leftmost insertion]
IF LOC=HEAD , then
a)PLINK(NEW)NULL
b) FLINK(NEW)LOC
c) PLINK(LOC)NEW
d) HEADNEW
return
[end of id structure]
7.[Insertion somewhere in the middle of list]
a)PLINK(NEW)PLINK(LOC)
b) FLINK(NEW)LOC
c) PLINK(LOC)NEW
d) FLINK((PLINK(NEW))NEW
8. Return
THANK YOU

You might also like