Vous êtes sur la page 1sur 37

LINKED LISTS

01

Objectives

 Understand the basic concepts and architecture of linked lists


data structures
 Comprehend he algorithms for insertion, deletion, traversal
and sorting operations
 Implement the algorithms using dynamic allocation of storage
space
 Describe relative advantages and disadvantages of linked
list data structures
02

Objectives

 Understand the concepts and structure of circular linked list,


doubly linked lists, circular doubly linked list and multi lists

 Learn about the basic operations associated with different


forms of the linked lists.
03

Introduction to Linked List


 A linked list is a collection of data elements called nodes.

 Each node consists of two parts called information field


and link field

 First element in the list in referred to as head node.


04

Introduction to Linked List


 A list may store complex data types
 First figure shows a lined list of three letter words
 Second example shows linked list of records
05

Storage Structure for Linked Lists


 Storage structure describes physical arrangement of nodes
 Suppose A and B are stored in locations 1000 and 2000 of
memory
 Link pointer of A would contain storage address of B , which
is 2000
06

Storage Structure for Linked Lists


 Another storage structure for a linked list consisting of
records is also shown

 Link pointer identifies physical address of a successor node


 Storage space is made available out of a special memory
block called heap
07

Operations on Linked Lists

 Inserting of new data elements


 Deleting the existing data
 Printing the contents of a list
 Sorting the list in the alphabetic or numeric order
 Searching for a given data value in the list
08

Inserting Nodes Into a Linked List


A new node can be inserted in the
 At the beginning
 In the middle
 At the end
09

Inserting Nodes At Front of List


To insert a node at the front , the following procedure is
followed:
 Front is identified by head pointer
 Its link pointer is set so that it points to the new node
 New node is linked to the previous front node
 Figure shows an example of list consisting of four nodes
10

Inserting Nodes At Front of List


 Figure shows the code fragment for the method InsFront,
which inserts a new node at the front
11

Inserting Nodes At End of List


 We use pointer which is set to the first node
 Then the list is traversed until the last node is reached
 A new node is created.
 Link pointer of tail node of original list is reset to point to new
node
12

Inserting Nodes At End of List


 Figure shows the steps
13

Inserting Nodes Into The Middle of List


 New node can be inserted which either precedes or
succeeds a given node
 To insert a middle node following steps are taken:

 A temporary pointer is used to traverse the list


 A new node is created
 Linked pointer is rest to point to the new node
14

Inserting Nodes Into The Middle of List


Procedure is depicted in Figure
15

Deleting Nodes From A Linked List


 Deletion can be performed to remove
 Node at the front
 Node in the middle
 Tail node
16

Deleting A Front Node


 We create a pointer which identifies the first node
 Head pointer is then reset to point to the next node
 Using the pointer we remove the first node
 Figure illustrates the procedure
17

Deleting A Tail Node


18

Deleting A Middle Node


19

Traversing a Linked List


 In traversing each node is visited exactly once.
 Traversing is performed when we need to access information
contained in each of he node, for example, to print the list or
to determine size of the list.
20

Traversing a Linked List


 Figure shows a sample code for traversal of a list. It returns
the number of nodes in the list.
21

Sorting Linked List


 In sorting a list, we compare the contents of nodes and swap
them if necessary.
 Remember, only the contents of nodes are swapped. The link
pointers of corresponding nodes remain unchanged.
22

Sorting Linked List


23

Variations of Linked List

 Circular linked list

 Doubly linked lists

 Circular doubly linked lists


 List containing multiple linked lists, commonly referred to as
multi list
24

Circular Linked List

 In a regular linked list, we cannot go back to any of the


preceding nodes
 A circular linked list overcomes this limitation
 In a circular linked list, the pointer in the tail node points to
the front node
 The list has no front or tail node. To make entry into the
circular linked list, a reference node is used which is
identified by an external pointer.
25

Circular Linked List


26

Doubly Linked List


 In the case of a doubly linked list, we can add or remove
elements from both sides.
 Unlike a regular list, we can traverse in forward and
backward directions. In other words we can perform bi-
directional traversals
 Insertion and deletion can be performed by adjusting
left and right link pointers
27

Inserting A Node Into A Doubly Linked List


28

Deleting A Node Into A Doubly Linked List


29

Circular Doubly Linked List


 It is a variant of the doubly linked list
 In this case next link pointer in the tail node contains
pointer to the first element.
 Previous link pointer in the first node points to the tail
node
30

Multi List
 We can combine a set of related list structures into an
integrated list called multi list
 In multi lists, nodes of all the integrated list can be
accessed using link pointers
 Previous link pointer in the first node points to the tail
node
 For example, a list that contains student record and
another list that contains subjects can be combined into
single multi list
31

Multi List
 We can, for example, access all students that are
registered in a given subject.
 Conversely, we can access the subjects in which a
particular student is registered,
32

Multi List
33

Advantages of Linked List

 Size of storage space can be adjusted dynamically at run


time
 Insertion and deletion operations are comparatively fast

 List traversal is extremely fast


34

Disadvantages of Linked List

 There is an overhead of pointers which occupy additional


storage space
 Elements of linked list cannot be accessed directly
35

Summary
 A linked list is a collection of data elements called
nodes.
 Each node consists of two parts called information field
and link field
 A value in the information field, called key, uniquely
identifies each node of the list
 Key contains the storage address of the successor
node.
 First element in the list in referred to as head node.
36

Summary
 Operations on Linked Lists includes Inserting, deleting,
printing, traversing and searching
 Variations of Linked list are Circular linked list, Doubly
linked lists, Circular doubly linked lists and Multi List

Vous aimerez peut-être aussi