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

Week 07 (Linked List)

A linked list is a linear data structure where each element is a node that stores data and a pointer to the next node. It allows for efficient insertion and removal of nodes but random access is inefficient. Common operations on linked lists include traversing, searching, inserting, and deleting nodes. Variations include doubly linked lists, which allow traversal in both directions, and circular linked lists, where the last node links to the first.

Uploaded by

umarzain2005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Week 07 (Linked List)

A linked list is a linear data structure where each element is a node that stores data and a pointer to the next node. It allows for efficient insertion and removal of nodes but random access is inefficient. Common operations on linked lists include traversing, searching, inserting, and deleting nodes. Variations include doubly linked lists, which allow traversal in both directions, and circular linked lists, where the last node links to the first.

Uploaded by

umarzain2005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

LINKED LIST DATA

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

• In linked-lists, we can only do sequential search.


• It is because, linked-list is not a random access data
structure.
Algorithm SearchNode (Head, KEY)
CurrentNode=Head
Position=1
While CurrentNode != NULL
If CurrentNode.Data == KEY
return Position
End If
Position++
CurrentNode = CurrentNode.Next
End while
return NULL
KEY = 12
CurrentNode

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

previous data next


Operations on Doubly 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
Circular Linked List
• It is a variation of linked list in which the last node of the list points the first
node rather than containing a null reference.
• In this way elements are connected in a loop.
• Both singly and doubly linked list can be made into a circular linked list.
• The next pointer of the last node points to the first node of the list(Circular
single linked list).
• The previous pointer of the first node will point to the last node of the
list(Circular doubly linked list).
Operations on Circular 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
Advantages of Linked List
• Linked list is a dynamic data structure so it can grow and shrink at
runtime by allocating and deallocating memory. So there is no need to give
initial size of linked list.

• 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

• As size of linked list can increase or decrease at run time so there is no


memory wastage. In case of array there is lot of memory wastage, like if
we declare an array of size 10 and store only 6 elements in it then space of
4 elements are wasted. There is no such problem in linked list as memory
is allocated only when required.
Disadvantages of Linked List
• More memory is required to store elements in linked list as compared to
array. Because in linked list each node contains a pointer and it requires
extra memory for itself.

• Elements or nodes traversal is difficult in linked list. We can not randomly


access any element as we do in array by index. For example if we want to
access a node at position n then we have to traverse all the nodes before it.
So, time required to access a node is large.
Disadvantages of Linked List
• In linked list reverse traversing is really difficult. In case
of doubly linked list its easier but extra memory is required for
back pointer hence wastage of memory.
Runtime analysis of linked list operations:
Access Search Insertion Deletion

Array O(1) O(n) O(n) O(n)

Singly Linked List O(n) O(n) O(n) O(n)

Doubly Linked List O(n) O(n) O(1) O(1)

• Singly Linked List: Insertion and deletion at the end takes O(n).
• Doubly Linked List: Insertion and deletion at the end takes O(1).

You might also like