Data Structure and
Algorithm
Using Python
Linked List
04
TABLE OF
CONTENTS 04
Introduction 01
03
Linked list operation
Double linked list
02
Singly linked list
Introduction
4
In Chapter 2, “Arrays,” we saw that arrays had
certain disadvantages as data storage structures. In
an unordered array, searching is slow, whereas
in an ordered array, insertion is slow. In both kinds
of arrays, deletion is slow. Also, the size of an
array can’t be changed after it’s created.
Introduction
5
In this chapter we’ll look at a data storage structure that
solves some of these problems: the linked list. Linked
lists are probably the second most commonly used
general purpose storage structures after arrays.
Introduction
6
 Linked List can be defined as collection of objects
called nodes that are randomly stored in the
memory.
 A node contains two fields i.e. data stored at that
particular address and the pointer which contains the
address of the next node in the memory.
 The last node of the list contains pointer to the null.
Introduction
7
 The linked list is a versatile mechanism suitable for
use in many kinds of general-purpose databases.
 It can also replace an array as the basis for other
storage structures such as stacks and queues.
 In fact, you can use a linked list in many cases in
which you use an array, unless you need frequent
random access to individual items using an index.
Introduction
8
Introduction
9
A Singly Linked List
10
A singly linked list is a linked list in which each node contains a single
link field and allows for a complete traversal from a distinctive first node
to the last.
A Singly Linked List
11
• A Singly-linked list is a collection of nodes linked together in a
sequential way where each node of the singly linked list contains a
data field and an address field that contains the reference of the
next node.
• The nodes are connected to each other in this form where the value
of the next variable of the last node is NULL i.e. next = NULL, which
indicates the end of the linked list.
Operation of Singly linked list
12
 Traversing a linked list.
The idea here is to step through the list from beginning to
the end. Traversing a linked list is important for many
applications. For examples, we may want to print the list
or search for a specific node in the list.
a. Start with the head of the list. Access the content of the head node
if it is not null.
b. b. Then go to the next node(if exists) and access the node
information.
c. c. Continue until no more nodes (that is, you have reached the null
node)
Operation of Singly linked list
13
Operation of Singly linked list
14
Operation of Singly linked list
15
Operation of Singly linked list
16
 Prepending a new Node to the beginning of a new Linked List .
Sometimes we must prepend(or insert to beginning) a
new node to the list. Prepending a node to a list is easy
since there is no need to find the end of the list. If list is
empty, we make new node the head of the list.
Operation of Singly linked list
17
Prepending
Operation of Singly linked list
18
 Appending a new Node to the end of a Linked List.
Sometimes we must append (or insert to end) new nodes to the
list. Since we only have information about the head of the
list(unless you maintain a last pointer), we need to traverse the
list until we find the last node. Then we insert new node to the
end of the list.
Operation of Singly linked list
19
 Inserting a new Node to a Sorted Linked List.
Sometimes we must insert (to some specific location) new
nodes to a list. It is important that we traverse the list to find the
place to insert the node. In the traversal process, we must
maintain a reference to previous and next nodes of the list.
Operation of Singly linked list
20
 Deleting a Node from a Linked List .
Sometimes we must delete a specific node from the list (if
exists). It is important that we traverse the list to find the node
to delete. In the traversal process, we must maintain a
reference to previous node of the list
Doubly linked list
21
Singly linked list consists of nodes linked in a
single direction. Access and traversals begin
with the first node and progress toward the
last node, one node at a time. But what if we
want to traverse the nodes in reverse order?
Doubly linked list
22
With the singly linked list, this can be done but not
efficiently. We would have to perform multiple traversals,
with each traversal starting at the front and stopping one
node earlier than on the previous traversal. Or consider
the problem in which you are given a reference to a
specific node and need to insert a new node immediately
preceding that node.
Doubly linked list
23
In a doubly linked list, each node contains not only the
data component and a link to the next node as in the
singly linked list, but also a second link that points to the
preceding node.
Operations of Doubly linked list
24
• In this section, we will discuss how a new node is
added into an already existing doubly linked list. We
will take four cases and then see how insertion is
done in each case.
• Case 1: The new node is inserted at the beginning.
• Case 2: The new node is inserted at the end.
• Case 3: The new node is inserted after a given node.
• Case 4: The new node is inserted before a given
node.
Inserting a Node at the Beginning of a Doubly Linked List
25
Consider the doubly linked list shown in Fig. 6.39. Suppose we want to
add a new node with data 9 as the first node of the list. Then the
following changes will be done in the linked list.
Inserting a Node at the End of a Doubly Linked List
26
Consider the doubly linked list shown in Fig. 6.41. Suppose
we want to add a new node with data 9 as the last node of the list. Then
the following changes will be done in the linked list.
Inserting a Node After a Given Node in a Doubly Linked List
27
Suppose we want to add a new node with a value 9 after the node
containing 3. Before discussing the changes that will be done in the
linked List.
Inserting a Node Before a Given Node in a Doubly Linked List
28
Consider the doubly linked list shown in Fig. 6.46. Suppose we want to
add a new node with value 9 before the node containing 3.
Deleting the First Node from a Doubly Linked List
29
Consider the doubly linked list shown in Fig. 6.46. Suppose we want to add a new node with
value 9 before the node containing 3.
Any Question?
30
END

CH4.pptx

  • 1.
  • 2.
  • 3.
    TABLE OF CONTENTS 04 Introduction01 03 Linked list operation Double linked list 02 Singly linked list
  • 4.
    Introduction 4 In Chapter 2,“Arrays,” we saw that arrays had certain disadvantages as data storage structures. In an unordered array, searching is slow, whereas in an ordered array, insertion is slow. In both kinds of arrays, deletion is slow. Also, the size of an array can’t be changed after it’s created.
  • 5.
    Introduction 5 In this chapterwe’ll look at a data storage structure that solves some of these problems: the linked list. Linked lists are probably the second most commonly used general purpose storage structures after arrays.
  • 6.
    Introduction 6  Linked Listcan be defined as collection of objects called nodes that are randomly stored in the memory.  A node contains two fields i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory.  The last node of the list contains pointer to the null.
  • 7.
    Introduction 7  The linkedlist is a versatile mechanism suitable for use in many kinds of general-purpose databases.  It can also replace an array as the basis for other storage structures such as stacks and queues.  In fact, you can use a linked list in many cases in which you use an array, unless you need frequent random access to individual items using an index.
  • 8.
  • 9.
  • 10.
    A Singly LinkedList 10 A singly linked list is a linked list in which each node contains a single link field and allows for a complete traversal from a distinctive first node to the last.
  • 11.
    A Singly LinkedList 11 • A Singly-linked list is a collection of nodes linked together in a sequential way where each node of the singly linked list contains a data field and an address field that contains the reference of the next node. • The nodes are connected to each other in this form where the value of the next variable of the last node is NULL i.e. next = NULL, which indicates the end of the linked list.
  • 12.
    Operation of Singlylinked list 12  Traversing a linked list. The idea here is to step through the list from beginning to the end. Traversing a linked list is important for many applications. For examples, we may want to print the list or search for a specific node in the list. a. Start with the head of the list. Access the content of the head node if it is not null. b. b. Then go to the next node(if exists) and access the node information. c. c. Continue until no more nodes (that is, you have reached the null node)
  • 13.
    Operation of Singlylinked list 13
  • 14.
    Operation of Singlylinked list 14
  • 15.
    Operation of Singlylinked list 15
  • 16.
    Operation of Singlylinked list 16  Prepending a new Node to the beginning of a new Linked List . Sometimes we must prepend(or insert to beginning) a new node to the list. Prepending a node to a list is easy since there is no need to find the end of the list. If list is empty, we make new node the head of the list.
  • 17.
    Operation of Singlylinked list 17 Prepending
  • 18.
    Operation of Singlylinked list 18  Appending a new Node to the end of a Linked List. Sometimes we must append (or insert to end) new nodes to the list. Since we only have information about the head of the list(unless you maintain a last pointer), we need to traverse the list until we find the last node. Then we insert new node to the end of the list.
  • 19.
    Operation of Singlylinked list 19  Inserting a new Node to a Sorted Linked List. Sometimes we must insert (to some specific location) new nodes to a list. It is important that we traverse the list to find the place to insert the node. In the traversal process, we must maintain a reference to previous and next nodes of the list.
  • 20.
    Operation of Singlylinked list 20  Deleting a Node from a Linked List . Sometimes we must delete a specific node from the list (if exists). It is important that we traverse the list to find the node to delete. In the traversal process, we must maintain a reference to previous node of the list
  • 21.
    Doubly linked list 21 Singlylinked list consists of nodes linked in a single direction. Access and traversals begin with the first node and progress toward the last node, one node at a time. But what if we want to traverse the nodes in reverse order?
  • 22.
    Doubly linked list 22 Withthe singly linked list, this can be done but not efficiently. We would have to perform multiple traversals, with each traversal starting at the front and stopping one node earlier than on the previous traversal. Or consider the problem in which you are given a reference to a specific node and need to insert a new node immediately preceding that node.
  • 23.
    Doubly linked list 23 Ina doubly linked list, each node contains not only the data component and a link to the next node as in the singly linked list, but also a second link that points to the preceding node.
  • 24.
    Operations of Doublylinked list 24 • In this section, we will discuss how a new node is added into an already existing doubly linked list. We will take four cases and then see how insertion is done in each case. • Case 1: The new node is inserted at the beginning. • Case 2: The new node is inserted at the end. • Case 3: The new node is inserted after a given node. • Case 4: The new node is inserted before a given node.
  • 25.
    Inserting a Nodeat the Beginning of a Doubly Linked List 25 Consider the doubly linked list shown in Fig. 6.39. Suppose we want to add a new node with data 9 as the first node of the list. Then the following changes will be done in the linked list.
  • 26.
    Inserting a Nodeat the End of a Doubly Linked List 26 Consider the doubly linked list shown in Fig. 6.41. Suppose we want to add a new node with data 9 as the last node of the list. Then the following changes will be done in the linked list.
  • 27.
    Inserting a NodeAfter a Given Node in a Doubly Linked List 27 Suppose we want to add a new node with a value 9 after the node containing 3. Before discussing the changes that will be done in the linked List.
  • 28.
    Inserting a NodeBefore a Given Node in a Doubly Linked List 28 Consider the doubly linked list shown in Fig. 6.46. Suppose we want to add a new node with value 9 before the node containing 3.
  • 29.
    Deleting the FirstNode from a Doubly Linked List 29 Consider the doubly linked list shown in Fig. 6.46. Suppose we want to add a new node with value 9 before the node containing 3.
  • 30.