Week 07 (Linked List)
Week 07 (Linked List)
STRUCTURE
Definition:
• A linear data structure.
• Used to store collection of elements.
• Unlike arrays, linked list use nodes to store elements which are not
stored in contiguous memory locations.
• A linked list is a collection of nodes where each node contains data
as well as the memory address of the next node in the list.
What is a node?
• Nodes are the building block of the linked list. After all, a linked list is a
collection of nodes.
• A node in a linked list consists of two parts:
• Data : contains actual data
• Link : link to the next element in the list
• Even though the nodes are not in a contiguous memory, the nodes are stored
linearly through links. Every node has the address of its succeeding node.
That is how each node can access its succeeding node.
A single node node
5
data pointer
Three nodes
(list of three
elements)
• The first node of the linked list is called the head node. It is the
starting point of a linked list.
• The last node is called the tail node. As there is no node after the
last node, the last node always points to the null.
• A null pointer does not point to any memory location.
Need for Linked List:
• Linked list solved the problem of contiguous memory allocation as
done by array.
• Using linked list, items can be placed anywhere in the memory.
• Each item stores the address of the next item in the list in order to
form a sequence.
• Using nodes addresses are stored and linked together.
Types of Linked List:
• Single Linked List : Navigation is forward only.
• Doubly Linked List : Forward and backward navigation is possible.
• Circular Linked List : Last element is linked to the first element.
Operations on Linked List
1. Searching/Traversing
2. Updation
3. Inserting
1. In empty list
2. In front of list
3. In the middle of list
4. At the end
4. Deleting
1. First Node
2. Middle node
3. Last node
Searching a Value
• We have learned two searching techniques:
• Sequential Search
• Binary Search
5 19 0 4 12 15 NULL
Position = 1
Head
CurrentNode
5 19 0 4 12 15 NULL
Head
Position = 2
CurrentNode
5 19 0 4 12 15 NULL
Head Position = 3
CurrentNode
5 19 0 4 12 15 NULL
Head Position = 4
CurrentNode
5 19 0 4 12 15 NULL
Head Position = 5
RETURN 5
INSERTING A NODE IN A SINGLY
LINKED LIST
• Insertion at the beginning
• Insertion after a given position
• Insertion at the end of the list.
Algorithm InsertNode (Position,
If CurrentNode == NULL Then return
Value)
NULL
If Position < 1 Then return
Create New Node NewNode
NULL
NewNode.Data = Value
CurrentNode=Head
If Position == 1
CurrentPos=1
NewNode.Next = Head
While CurrentNode !=
Head = NewNode
NULL
Else
If CurrentPos ==
NewNode.Next =
Position Then break
CurrentNode.Next
CurrentPos++
CurrentNode.Next =
CurrentNode =
NewNode
End If
CurrentNode.Next
End while
Insert A Node at the Beginning of the Linked
List:
5 19 0 4 12 15 NULL
Head
Insert A Node after 4th position in the Linked
List:
5 19 0 4 12 15 NULL
Head
Insert A Node at the End of the Linked List:
5 19 0 4 12 15 NULL
Head
DELETING A NODE IN A SINGLY LINKED
LIST
• Deleting first node
• Deleting middle node
• Deleting last node
Algorithm DeleteNode (Value) If CurrentNode = NULL Then return
PrevNode = NULL FALSE
CurrentNode = Head If PrevNode == NULL //First
Position = 1 element to be deleted
While CurrentNode != NULL Head =
If CurrentNode.Data CurrentNode.Next
== Value Then break; Else //Middle
Position++ or End Element is to be deleted
PrevNode = PreviousNode.Next =
CurrentNode CurrentNode.Next
CurrentNode = End If
CurrentNode.Next Delete CurrentNode
End while return TRUE
Delete Element 5 of the Linked List:
5 19 0 4 12 15 NULL
Head
Delete Element 12 of the Linked List:
5 19 0 4 12 15 NULL
Head
In case other than first node, there is another pointer
required since we can not go backward in single
linked list in order to update the address part after
deletion.
Delete Element 4 of the Linked List:
5 19 0 4 12 15 NULL
Head
Doubly Linked List
• It is a variation of linked list in which traversal is possible in both
forward and backward easily.
• In this variation we have to take two pointers initially as Head
and Tail. These dummy nodes are called sentinels(guards).
Structure of Node in a doubly linked list:
node
5
• Insertion and deletion of nodes are really easier. Unlike array here we
don’t have to shift elements after insertion or deletion of an element. In
linked list we just have to update the address present in next pointer of a
node.
Advantages of Linked List
• Singly Linked List: Insertion and deletion at the end takes O(n).
• Doubly Linked List: Insertion and deletion at the end takes O(1).