You are on page 1of 18

LINKED LIST

LECTURE:5-8
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.
26 X
45 .
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.

You might also like