Data Structures - Unit 3 Linked List
Data Structures - Unit 3 Linked List
1
Drawbacks of sequential data structures
4
Drawbacks of sequential data structures
10
Merits of linked data structures
This implies that unlike arrays, no two nodes in a linked list
need be physically contiguous.
All the nodes in a linked list data structure may in fact be
strewn across the storage memory making effective use of
what little space is available to represent a node.
However, the link fields carry on themselves the onerous
responsibility of remembering the addresses of the other
neighbouring or associated nodes, to keep track of the data
elements in the list. The link fields are referred to as pointers. 11
Merits of linked data structures
To implement linked lists the following mechanisms are
essential:
i. A mechanism to frame chunks of memory into nodes with
the desired number of data items and fields.
ii. A mechanism to determine which nodes are free and which
have been allotted for use.
iii. A mechanism to obtain nodes from the free storage area or
storage pool for use.
iv. A mechanism to return or dispose of nodes from the 12
represented by a node.
Representation of a Singly linked list
8456 respectively.
Representation of a Singly linked list
19
Representation of a Singly linked list
21
Insertion and deletion in a singly linked list
To implement insertion and deletion in a singly linked list,
one needs the two functions GETNODE(X) and RETURN(X)
respectively.
Insert operation:
Given a singly linked list START, to insert a data element
ITEM into the list to the right of node NODE, (ITEM is to be
inserted as the successor of the data element represented by
node NODE) the steps to be undertaken are given below. 22
Insertion and deletion in a singly linked list
24
Insertion and deletion in a singly linked list
Algorithm 6.1 illustrates a pseudo code procedure for
insertion in a singly linked list which is non empty.
25
Insertion and deletion in a singly linked list
However, during insert operation in a list, it is advisable to test if START
pointer is null or non-null.
If START pointer is null (START = NIL) then the singly linked list is
empty and hence the insert operation prepares to insert the data as the
first node in the list.
On the other hand, if START pointer is non-null (START != NIL), then the
singly linked list is non empty and hence the insert operation prepares
to insert the data at an appropriate position in the list as specified by
the application.
Algorithm 6.1 works on a non empty list. To handle empty lists the 26
27
Insertion and deletion in a singly linked list
Example 6.1 In the singly linked list SPACE-MISSION
illustrated in Fig. 6.5(a-b), insert the following data elements:
29
Insertion and deletion in a singly linked list
30
Insertion and deletion in a singly linked list
31
Insertion and deletion in a singly linked list
32
Insertion and deletion in a singly linked list
Delete operation:
Given a singly linked list START, the delete operation can
acquire various forms such as deletion of a node NODEY
next to that of a specific node NODEX, or more commonly
deletion of a particular element in a list and so on.
We now illustrate the deletion of a node which is the
successor of node NODEX.
33
Insertion and deletion in a singly linked list
The steps for the deletion of a node next to that of NODEX in
a singly linked START is given below.
i. Set TEMP a temporary variable to point to the right
neighbour of NODEX (i.e.) TEMP = LINK(NODEX). The node
pointed to by TEMP is to be deleted.
ii. Set LINK field of node NODEX to point to the right
neighbour of TEMP (i.e.) LINK(NODEX) = LINK(TEMP).
iii. Dispose node TEMP (i.e.) RETURN (TEMP).
Figure 6.8 illustrates the logical representation of the delete 34
operation.
Insertion and deletion in a singly linked list
35
Insertion and deletion in a singly linked list
36
Insertion and deletion in a singly linked list
37
Insertion and deletion in a singly linked list
Example 6.2
For the SPACE-MISSION list shown in Fig. 6.5(a-b) undertake
the following deletions:
(i) Delete CHANDRA
(ii) Delete PLANCK
The deletion of CHANDRA is illustrated in Fig. 6.9(a-b) and
that of PLANCK is illustrated in Fig. 6.9 (c-d).
38
Insertion and deletion in a singly linked list
39
Insertion and deletion in a singly linked list
40
Insertion and deletion in a singly linked list
41
Insertion and deletion in a singly linked list
42
Circularly Linked Lists
Representation of a Circularly Linked Lists
A normal singly linked list has its last node carrying a null
pointer.
For further improvement in processing one may replace the
null pointer in the last node with the address of the first
node in the list.
Such a list is called as a circularly linked list or a circular
linked list or simply a circular list. Figure 6.10 illustrates the 43
44
Circularly Linked Lists
Though the head node has the same structure as the other
nodes in the list, the DATA field of the node is unused and is
indicated as a shaded field in the pictorial representation. 50
Circularly Linked Lists
Example 6.3
Let CARS be a headed circularly linked list of four data
elements as shown in Fig. 6.12(a).
To insert MARUTI into the list CARS, the sequence of steps to
be undertaken are as shown in Fig. 6.12(b-d).
To delete FORD from the list CARS shown in Fig. 6.13(a) the
sequence of steps to be undertaken are shown in Fig. 6.13(b-
d).
51
Circularly Linked Lists
52
Circularly Linked Lists
53
Circularly Linked Lists
54
Circularly Linked Lists
55
Primitive operations on circularly linked lists
Some of the important primitive operations executed on a
circularly linked list are detailed below.
Here P is a circularly linked list as illustrated in figure below.
57
Primitive operations on circularly linked lists
II. Insert an element A as the right most element in the list
represented by P.
The sequence of operations to execute the insertion are the
same as that of inserting A as the left most element in the list
followed by the instruction. P=X
Figure below illustrates the insertion of A as the right most
element in list P.
58
Primitive operations on circularly linked lists
III. Set Y to the data of the left most node in the list P and delete the
node.
The sequence of operations to execute the deletion are:
PTR = LINK(P);
Y = DATA(PTR);
LINK(P) = LINK(PTR);
Call RETURN(PTR);
Here PTR is a temporary pointer variable.
Figure below illustrates the deletion of the left most node in the
59
The other operations are splitting a list into two parts and
erasing a list. 61
Other operations on circularly linked lists
62
Doubly Linked Lists
We discussed two types of linked representations viz., singly
linked list and circularly linked list, both making use of a
single link.
Also, the circularly linked list served to rectify the
drawbacks of the singly linked list.
To enhance greater flexibility of movement, the linked
representation could include two links in every node, each of
which points to the nodes on either side of the given node.
Such a linked representation is known as doubly linked list. 63
Representation of a doubly linked list
A doubly linked list is a linked linear data structure, each
node of which has one or more data fields but only two link
fields termed left link (LLINK) and right link (RLINK).
The LLINK field of a given node points to the node on its left
and its RLINK field points to the one on its right.
A doubly linked list may or may not have a head node. Again,
it may or may not be circular.
Figure 6.16 illustrates the structure of a node in a doubly
linked list and the various types of lists. 64
Representation of a doubly linked list
65
Representation of a doubly linked list
Example 6.4
Consider a list FLOWERS of four data elements LOTUS,
CHRYSANTHEMUM, LILY and TULIP stored as a circular doubly
linked list with a head node.
The logical and physical representation of FLOWERS has been
illustrated in Fig. 6.17 (a-b).
Observe how the LLINK and RLINK fields store the addresses of
the predecessors and successors of the given node respectively.
In the case of FLOWERS being an empty list, the representation is
66
67
Advantages and disadvantages of a doubly linked list
69
Advantages and disadvantages of a doubly linked list
71
Operations on doubly linked lists
Figure below shows the logical representation of list P
before and after insertion.
72
Operations on doubly linked lists
Delete operation:
Let P be a headed, circular doubly linked list.
Algorithm 6.5 illustrates the deletion of a node X from P.
The condition (X = P) that is checked ensures that the head node
P is not deleted.
73
Operations on doubly linked lists
Figure below shows the logical representation of list P
before and after the deletion of node X from the list P.
74
Operations on doubly linked lists
Example 6.5
Let PLANET be a headed circular doubly linked list with
three data elements viz. MARS, PLUTO and URANUS.
Figure 6.19 illustrates the logical and physical
representation of the list PLANET.
Figure 6.20(a) illustrates the logical and physical
representation of list PLANET after the deletion of PLUTO
and Fig. 6.20(b) the same after insertion of JUPITER.
75
Operations on doubly linked lists
76
Operations on doubly linked lists
77
Operations on doubly linked lists
78
Applications of linked lists
Addition of polynomials
The objective of this application is to perform a symbolic
addition of two polynomials as illustrated below:
Let P1 : 2x6 + x3 + 5x + 4 and
P2 : 7x6 + 8x5 -9x3 + 10x2 + 14
be two polynomials over a variable x.
The objective is to obtain the algebraic sum of P1, P2 (i.e.)
P1 + P2 as,
P1 + P 2 = 9x + 8x - 8x + 10x + 5x + 18.
6 5 3 2 79
Addition of polynomials
81
Addition of polynomials
To add the two polynomials, we presume that the singly
linked lists have their nodes arranged in the decreasing
order of the exponents of the variable x.
The objective is to create a new list of nodes representing
the sum P1 + P2.
This is achieved by adding the COEFF fields of the nodes of
like powers of variable x in lists P1, P2 and adding a new
node reflecting this operation in the resultant list P1 + P2.
We present below, the crux of the procedure: 82
Addition of polynomials
Here P1, P2 are the start pointers of the singly linked lists
representing the polynomials P1, P2.
Also PTR1 and PTR2 are two temporary pointers initially set to P1 and
P2 respectively.
83
Addition of polynomials
84
Addition of polynomials
87