2. Introduction
• A linked list is a data structure in which
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
2
A B C
head
3. • Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).
• Linked lists provide flexibility in allowing the
items to be rearranged efficiently.
– Insert an element.
– Delete an element.
8. Types of Linked list
• 3 types
– Singly Linked List(SLL)
– Doubly Linked List(DLL)
– Circular Linked List(CLL)
9. Insertion in a SLL
• 3cases in this
– Insertion at the beginning
– Insertion at the end
– Insertion after a particular node
10. Insertion in a SLL
• Insertion at the beginning
– Step 1:- Make the new ptr of the node point towards the
first node of the list
– Step2:- Make the head ptr point towards this new node
* If the list is empty(means this is gng to be the 1st node
then, simply make head ptr point towards new node )
14. Insertion in a SLL
• Insertion after a particular node
– Step 1:- Make the next ptr of the node to be inserted point
to the next node of the node before which you wanna
insert the node
– Step2:- Make the next ptr of the node after which the
node is to be inserted ,point to the node to be inserted
17. Deletion in a SLL
• 3cases in this
– Deletion from the beginning
– Deletion from the end
– Deletion of an intermediate node
18. Deletion in a SLL
• Deleting the first node in SLL
– Step 1:- Make the start ptr point towards the 2nd node
– Step2:- Deleting the 1st node using DELETE keyword.
20. Deletion in a SLL
• Deleting the last node in SLL
– Step 1:- Make the second last node’s next ptr point to
NULL
– Step2:- Deleting the last node using DELETE keyword.
22. Deletion in a SLL
• Deleting a particular node in SLL
– Step 1:- Make the next ptr of the node previous to the
node being deleted point to the successor node of the
node to be deleted
– Step2:- Deleting the particular node using DELETE
keyword.
23. Deletion in a SLL
• Deleting a particular node in SLL
25. Searching in a SLL
• Finding required element in the list
• 2 ways
– Linear
• Much easier in case of linked list
– Binary
• Complex as random traversal is not easy
26. Linear search in a SLL
• Each node is traversed till the data in the node
matches with the required value.
28. Doubly Linked List
• Linked data structure that consists of a set of
sequentially linked records called nodes
• Contains 3 fields
– An integer value
– Link to the next node
– Link to the previous node
38. Insertion at the end in a DLL
1st step:-
a. Traverse the
list
b. Update next
part of last
node
c. Update prev
part of temp
node
How to do this?
39. Insertion at the end in a DLL
1st step:- take a pointer tp
which initially points to 1st
node and later last node
Code for tp to
traverse each node
40. Insertion at the end in a DLL
tp is now pointing the
last node using the
code---traversal done
41. Insertion at the end in a DLL
• Step 2:- Attach a new node to end node of the list
Step:- make this point
to prev of temp node
code
72. Delete last node from a DLL
• Traverse the list till last using temp pointer
73. Delete last node from a DLL
Step:- storing this node
in a temp2
While(temp-> NEXT !=NULL)
{
temp = temp->NEXT;
}
temp2 = temp->prev;
temp2->NEXT = NULL;
Step:-Now this should
point to NULL
74. Delete last node from a DLL
While(temp-> NEXT !=NULL)
{
temp = temp->NEXT;
}
temp2 = temp->prev;
temp2->NEXT = NULL;
free(temp);
temp=NULL;
78. Circular Linked List
• Variation of LL in which last element points to
1st element
• Both SLL and DLL can be converted to CLL.
79. Singly Linked List as Circular
• In SLL , the next pointer of last node points to
1st node
80. Doubly Linked List as Circular
• In DLL , the nest pointer of last node points to
1st node and previous pointer of 1st node
points to last node making the circular in both
direction
81. Advantages of CLL
• 1.Any node can be a starting point.We can
traverse the whole list by starting from any
point
• 2.
82. Disadvantages of CLL
• 1. inserting at start of list would require doing
a search for last node ----expensive
• 2. Finding end of the list and loop control is
harder (no NULL’s to mark the beginning and end)
85. Circular Linked List—Insertion at beginning
Tail pointer is the
pointer to the last
node of the linked list
Why this tail pointer is
needed?
Next slide
86. Circular Linked List—Insertion at beginning
• Consider a CLL of 3 nodes
head
A ptr is must for this node
• it’s NEXT can point to new node
•No need to traverse the whole list to
find last node
89. Circular Linked List– Insertion at end
Step:- update
this ptr and let
it point to the
first node
So NEXT ptr of new node takes
the current address which tail
ptr’s NEXT has (to point the first
node)
90. Circular Linked List– Insertion at end
Step:- this ptr needs
to point to the new
node
Tail ptr needs to point
to new node now
Code
103. CLL – Deletion of last node
For this, we take a temp ptr
and make it point to 1st node
initially.
This code is to make
temp reach the 2nd
node so that we can
update the NEXT ptr of
this node point to
1000
104. CLL – Deletion of last node
The loop has made
the temp ptr reach
here
108. CLL – Deletion of intermediate node
Temp reaches here
due to this loop in the
code We need
something to point
to this position
So, taking another
variable
109. CLL – Deletion of intermediate node
Code for setting position of
temp2
113. GTU questions
• Explain creation,insertion and deletion of a doubly linked list
with example.[07]
• Write and explain algorithm for deletion in a singly linked list
[07]
• Write and explain algorithm for insertion in doubly linked
list[07]
• Write an algorithm to insert a node in a circular linked list at
the first position[07]
• Write user defined ‘C’ function to insert node at a specific
location in singly linked list[03]
• Write user defined ‘C’ function to delete node from end in
circular linked list[04]
• Design an algorithm to merge two linked list[07]