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

Data Structures - Unit 3 Linked List

LINK(X) = LINK(NODE) iv. Set LINK field of node NODE to point to node X.

Uploaded by

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

Data Structures - Unit 3 Linked List

LINK(X) = LINK(NODE) iv. Set LINK field of node NODE to point to node X.

Uploaded by

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

Linked Lists

1
Drawbacks of sequential data structures

 Arrays are fundamental sequential data structures.


 Even stacks and queues rely on arrays for their
representation and implementation.
 However, arrays or sequential data structures in general,
suffer from the following drawbacks:
i. inefficient implementation of insertion and deletion
operations and
ii. inefficient use of storage memory.
2
Drawbacks of sequential data structures

 Let us consider an array A[1 : 20].


 This means a contiguous set of twenty memory locations
have been made available to accommodate the data
elements of A.
 As shown in Fig. 6.1(a), let us suppose the array is partially
full.
 Now, to insert a new element 108 in the position indicated, it
is not possible to do so without affecting the neighbouring
3

data elements from their positions.


Drawbacks of sequential data structures

4
Drawbacks of sequential data structures

 Methods such as making use of a temporary array (B) to


hold the data elements of A with 108 inserted at the
appropriate position or making use of B to hold the data
elements of A which follow 108, before copying B into A, call
for extensive data movement which is computationally
expensive.
 Again, attempting to delete 217 from A calls for the use of a
temporary array B to hold the elements with 217 excluded,
before copying B to A.
5
Drawbacks of sequential data structures

 With regard to the second drawback of inefficient storage


memory management, the need for allotting contiguous
memory locations for every array declaration is bound to
leave fragments of free memory space unworthy of
allotment for future requests.
 This eventually may lead to inefficient storage management.
 In fact, fragmentation of memory is a significant problem to
be reckoned with in computer science. Several methods
have been proposed to counteract this problem.
6
Drawbacks of sequential data structures
 Figure below shows a simple diagram of a storage memory
with fragmentation of free space.

 Note how fragments of free memory space, though put together,


can be a huge chunk of free space, the lack of contiguity renders
7

them unworthy of accommodating sequential data structures.


Merits of linked data structures
 A linked representation serves to counteract the drawbacks
of sequential representation by exhibiting the following merits:
i. Efficient implementation of insertion and deletion operations.
Unlike sequential data structures, there is complete absence
of data movement of neighbouring elements during the
execution of these operations.
ii. Efficient use of storage memory. The operation and
management of linked data structures are less prone to
create memory fragmentation. 8
Merits of linked data structures

 A linked representation of data structure known as a linked


list is a collection of nodes.
 Each node is a collection of fields categorized as data
items and links.
 The data item fields hold the information content or data to
be represented by the node.
 The link fields hold the addresses of the neighbouring
nodes or of those nodes which are associated with the given
node as dictated by the application.
9
Merits of linked data structures
 Figure below illustrates the general node structure of a
linked list.
 A node is represented by a rectangular box and the fields
are shown by partitions in the box.
 Link fields are shown to carry arrows to indicate the nodes
to which the given node is linked or connected.

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

reserved area or pool to the free area after use.


Merits of linked data structures
 Irrespective of the number of data item fields, a linked list is
categorized as singly linked list, doubly linked list, circularly
linked list and multiply linked list based on the number of link fields
it owns and/or its intrinsic nature.
 Thus a linked list with a single link field is known as singly linked
list and the same with a circular connectivity is known as circularly
linked list.
 On the other hand, a linked list with two links each pointing to the
predecessor and successor of a node is known as a doubly linked
list and the same with multiple links is known as multiply linked list. 13
Singly Linked Lists
Representation of a singly linked list
 A singly linked list is a linear data structure, each node of
which has one or more data item fields (DATA) but only a
single link field (LINK).
 Figure below illustrates an example of a singly linked list and
its node structure.
 Observe that the node in the list carries a single link which
points to the node representing its immediate successor in 14

the list of data elements.


Representation of a Singly linked list

 Every node which is basically a chunk of memory, carries an


address.
 When a set of data elements to be used by an application
are represented using a linked list, each data element is 15

represented by a node.
Representation of a Singly linked list

 Depending on the information content of the data element,


one or more data items may be opened in the node.
 However, in a singly linked list only a single link field is
used to point to the node which represents its neigbouring
element in the list.
 The last node in the linked lists has its link field empty.
 The empty link field is also referred to as null link or in
programming language parlance - null pointer.
16
Representation of a Singly linked list

 The notations NIL, or a ground symbol ( ) or a zero (0) are


commonly used to indicate null links.
 The entire linked list is kept track of by remembering the
address of the start node.
 This is indicated by START in the figure.
 Obviously it is essential that the START pointer is carefully
handled, because it results in losing the entire list.
17
Representation of a Singly linked list
Example
 Consider a list SPACE-MISSION of four data elements as
shown in Fig. 6.5(a).
 This logical representation of the list has each node carrying
three DATA fields viz., name of the space mission, country of
origin, the current status of the mission, and a single link
pointing to the next node.
 Let us suppose the nodes which house ‘Chandra’, ‘INSAT-3A’ ,
‘Mir’ and ‘Planck’ have addresses 1001, 16002, 0026 and 18

8456 respectively.
Representation of a Singly linked list

19
Representation of a Singly linked list

 Figure 6.5(b) shows the physical representation of the linked


list.
 Note how the nodes are distributed all over the storage memory
and not physically contiguous.
 Also observe how the LINK field of each node remembers the
address of the node of its logical neighbour.
 The LINK field of the last node is NIL.
 The arrows in the logical representation represent the addresses
20

of the neighbouring nodes in its physical representation.


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

i. Call GETNODE(X) to obtain a node to accommodate ITEM.


Node has address X.
ii. Set DATA field of node X to ITEM (i.e.) DATA(X) = ITEM.
iii. Set LINK field of node X to point to the original right
neighbour of node NODE (i.e.) LINK(X) = LINK(NODE).
iv. Set LINK field of NODE to point to X (i.e.) LINK(NODE) = X.
 Figure 6.6. illustrates the logical representation of the insert
operation.
23
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

algorithm has to be appropriately modified as illustrated in Algorithm 6.2.


Insertion and deletion in a singly linked list

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:

 Let us suppose the GETNODE(X) function releases nodes


with addresses X = 646 and X = 1187 to accommodate
APPOLLO and SOYUZ 4 details respectively.
 The insertion of APPOLLO is illustrated in Fig. 6.7(a-b) and
28

the insertion of SOYUZ 4 is illustrated in Fig. 6.7(c-d).


Insertion and deletion in a singly linked list

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

 Algorithm 6.3 illustrates a pseudo-code procedure for the


deletion of a node which occurs to the right of a node
NODEX in a singly linked list START.
 However, as always, it needs to be ensured that the delete
operation is not undertaken over an empty list.
 Hence it is essential to check if START is empty.

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

representation of a circular list.


Representation of a Singly linked list
 Figure 6.10 illustrates the representation of a circular list.

44
Circularly Linked Lists

 Advantages of circularly linked lists over singly linked


lists
i. The most important advantage pertains to the accessibility of
a node.
 One can access any node from a given node due to the
circular movement permitted by the links.
 One has to merely loop through the links to reach a specific
node from a given node.
45
Circularly Linked Lists
ii. The second advantage pertains to delete operations. Recall
that for deletion of a node X in a singly linked list, the
address of the preceding node (for example node Y) is
essential, to enable, update the LINK field of Y to point to the
successor of node X.
 This necessity arises from the fact that in a singly linked list,
one cannot access a node’s predecessor due to the ‘forward’
movement of the links. In other words, LINK fields in a singly
linked list point to successors and not predecessors. 46
Circularly Linked Lists

 However, in the case of a circular list, to delete node X one


need not specify the predecessor.
 It can be easily determined by a simple ‘circular’ search
through the list before deletion of node X.
iii. The third advantage is the relative efficiency in the
implementation of list based operations such as
concatenation of two lists, erasing a whole list, splitting a
list into parts and so on.
47
Circularly Linked Lists
 Disadvantages of circularly linked lists
 The only disadvantage of circularly linked lists is that during
processing one has to make sure that one does not get into an
infinite loop owing to the circular nature of pointers in the
list.
 This is liable to occur owing to the absence of a node which
will help point out the end of the list and thereby terminate
processing.
 A solution to this problem is to designate a special node to 48

act as the head of the list.


Circularly Linked Lists
 This node, known as list head or head node has its
advantages other than pointing to the beginning of a list.
 The list can never be empty and represented by a ‘hanging’
pointer (START = NIL) as was the case with empty singly
linked lists.
 The condition for an empty circular list becomes (LINK(HEAD)
= HEAD), where HEAD points to the head node of the list.
 Such a circular list is known as a headed circularly linked list
or simply circularly linked list with head node. 49
Circularly Linked Lists
 Figure 6.11 illustrates the representation of a headed
circularly linked list.

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

i. Insert an element A as the left most element in the list


represented by P. 56
Primitive operations on circularly linked lists
 The sequence of operations to execute the insertion is:
Call GETNODE (X);
DATA (X) = A;
LINK (X) = LINK(P);
LINK (P) = X;
 Figure below illustrates the insertion of A as the left most element
in the circular list P.

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

list P, setting Y to its data.


Primitive operations on circularly linked lists

 Observe that the primitive operations (i) and (iii) when


combined, results in the circularly linked list working as a
stack and operations (ii) and (iii) when combined, results in
the circularly linked list working as a queue. 60
Other operations on circularly linked lists
 The concatenation of two circularly linked lists L1, L2 as
illustrated in Fig. 6.15 has the following sequence of
instructions.

 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

as shown in Fig. 6.17 (c-d)


Representation of a doubly linked list

67
Advantages and disadvantages of a doubly linked list

 Doubly linked lists have the following advantages:


i. The availability of two links LLINK and RLINK permit forward
and backward movement during the processing of the list.
ii. The deletion of a node X from the list calls only for the value
X to be known.
 Contrast how in the case of a singly linked or circularly
linked list, the delete operation necessarily needs to know
the predecessor of the node to be deleted.
68
Advantages and disadvantages of a doubly linked list

 While a singly linked list expects the predecessor of the


node to be deleted, to be explicitly known, a circularly
linked list is endowed with the capability to move round the
list to find the predecessor node.
 However, in the latter case, if the list is too long it may render
the delete operation inefficient.

69
Advantages and disadvantages of a doubly linked list

 The only disadvantage of the doubly linked list is its


memory requirement.
 That each node needs two links could be considered
expensive storage-wise, when compared to singly linked
lists or circular lists.
 Nevertheless, the efficiency of operations due to the
availability of two links more than compensate for the extra
space requirement.
70
Operations on doubly linked lists
 Insert Operation:
 Let P be a headed circular doubly linked list which is non
empty.
 Algorithm 6.4 illustrates the insertion of a node X to the right of
node Y.

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

 To perform this symbolic manipulation of the polynomials,


we make use of a singly linked list to represent each
polynomial.
 The node structure and the singly linked list
representation for the two polynomials are given in Fig.
6.26.
 Here each node in the singly linked list represents a term of
the polynomial.
80
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

 If any one of the lists during the course of addition of terms


has exhausted its nodes earlier than the other list, then the
nodes of the other list are simply appended to list P1 + P2 in
the order of their occurrence in their original list.
 In case of polynomials of two variables x, y or three
variables x, y, z the node structures are as shown in Figure
below.
85
Addition of polynomials

 Here COEFFICIENT refers to the coefficient of the term in


the polynomial represented by the node.
 EXPONENT X, EXPONENT Y and EXPONENT Z are the
86

exponents of the variables x, y and z respectively.


ADT for Linked Lists

87

You might also like