SlideShare a Scribd company logo
1 of 113
Linked List
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
• 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.
Array vs Linked list
Creating a node
To be called from main as
Types of Linked list
• 3 types
– Singly Linked List(SLL)
– Doubly Linked List(DLL)
– Circular Linked List(CLL)
Insertion in a SLL
• 3cases in this
– Insertion at the beginning
– Insertion at the end
– Insertion after a particular node
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 )
Insertion in a SLL
• Insertion at the beginning
Insertion in a SLL
• Insertion at the end
– Simply need to make the next ptr of the last node point to
the new node instead of NULL
Insertion in a SLL
• Insertion at the end
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
Insertion in a SLL
• Insertion after a particular node
Deletion in a SLL
Deletion in a SLL
• 3cases in this
– Deletion from the beginning
– Deletion from the end
– Deletion of an intermediate node
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.
Deletion in a SLL
• Deleting the first node in SLL
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.
Deletion in a SLL
• Deleting the last node in SLL
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.
Deletion in a SLL
• Deleting a particular node in SLL
Searching in a SLL
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
Linear search in a SLL
• Each node is traversed till the data in the node
matches with the required value.
Doubly Linked List
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
Structure of a DLL
Traversing in a DLL
Searching in a DLL
Insertion in a DLL
Doubly Linked List--Insertion
• Insertion at the beginning
Node to
be added
Existing linked list
Doubly Linked List--Insertion
• Insertion at the beginning
1st step:-
Make it point
to head
Doubly Linked List--Insertion
• Insertion at the beginning
2nd step:- make prev
point to temp
Doubly Linked List—Insertion at
beginning
Insertion at the beginning in a DLL
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?
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
Insertion at the end in a DLL
tp is now pointing the
last node using the
code---traversal done
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
Insertion at the end in a DLL
Insertion at the end in a DLL
Insertion at the end in a DLL
Insertion at the end in a DLL
Insertion after a node in a DLL
Insertion after a node in a DLL
4000
newP
There must be a
pointer pointing to this
new node
We need a
ptr pointing
to this 2nd
node
Insertion after a node in a DLL
temp will point to the 2nd node as per this code
Insertion after a node in a DLL
Now we have pointers
pointing position node
and after node of the
desired location to insert
Insertion after a node in a DLL
Make this ptr
point to prev of
new node
code
Insertion after a node in a DLL
Now prev part of this
node needs to point next
of new node
code
Insertion after a node in a DLL
Prev of this node
points to next of temp
and next of this node
points to prev of
temp2
code
Insertion after a node in a DLL
Insertion after a node in a DLL
Deletion in a DLL
Deletion from beginning in a DLL
Deletion from beginning in a DLL
Step1:-Let a temp node
point the head node
Deletion from beginning in a DLL
Step2:- let head
point to next node
now
code
Deletion from beginning in a DLL
Step:-Remove the temp
node
Deletion from beginning in a DLL
Step:- Assign NULL
to temp and prev of
head node
Deletion from beginning in a DLL
• Method 2
Deletion from beginning in a DLL
Deletion from particular position in a DLL
Deletion from a particular position in a DLL
Initially temp is pointing
to head and moving
towards the position to
be deleted
Deletion from a particular position in a DLL
Step:- take 1 more
ptr to point to
node previous to
temp
Deletion from a particular position in a DLL
Step:- next of temp2
should now point to next
of temp
Deletion from a particular position in a DLL
Step:- update this so that it
points to next of temp2
Deletion from a particular position in a DLL
Step:- free memory of
temp pointer
Deletion from a particular position in
a DLL
Deletion from a particular position in a DLL
Delete last node from a DLL
Delete last node from a DLL
• Traverse the list till last using temp pointer
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
Delete last node from a DLL
While(temp-> NEXT !=NULL)
{
temp = temp->NEXT;
}
temp2 = temp->prev;
temp2->NEXT = NULL;
free(temp);
temp=NULL;
Delete last node from a DLL
Delete last node from a DLL
Circular Linked List
Circular Linked List
• Variation of LL in which last element points to
1st element
• Both SLL and DLL can be converted to CLL.
Singly Linked List as Circular
• In SLL , the next pointer of last node points to
1st node
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
Advantages of CLL
• 1.Any node can be a starting point.We can
traverse the whole list by starting from any
point
• 2.
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)
Circular Linked List--Operations
• Insertion
• Deletion
• Display
Circular Linked List--Insertion
• Insertion
– At the beginning
– At the end
– At a particular position
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
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
Circular Linked List—Insertion at beginning
Step:- Update
this ptr
Let it point to
NEXT ptr of
tail
Circular Linked List—Insertion at beginning
Step:- Update the
NEXT of tail ptr
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)
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
Circular Linked List– Insertion at end
Final result
Circular Linked List– Insertion between nodes
Initial State
Circular Linked List– Insertion between nodes
Step:- a ptr is needed
to point this node as
we need to reach
here
Circular Linked List– Insertion between nodes
Step:- this ptr
needs to be
updated
Circular Linked List– Insertion between nodes
Step:- NEXT of
this node will
point to new
node now
code
Circular Linked List– Insertion between nodes
• If pos =3(similar to inserting at the end,with slight
changes)
Circular Linked List--Deletion
• Deletion
– At the beginning
– At the end
– At a particular position
CLL – Deletion of first node
CLL – Deletion of first node
To delete, we are
making a temp ptr
point to first node
CLL – Deletion of first node
code
CLL – Deletion of first node
Step:- let’s free this
node now
CLL – Deletion of last node
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
CLL – Deletion of last node
The loop has made
the temp ptr reach
here
CLL – Deletion of last node
CLL – Deletion of intermediate node
CLL – Deletion of intermediate node
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
CLL – Deletion of intermediate node
Code for setting position of
temp2
CLL – Deletion of intermediate node
CLL – Deletion of intermediate node
free(temp2)
CLL – Deletion of intermediate node
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]

More Related Content

Similar to Linked List, basics , types , operations

circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
MeghaKulkarni27
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 

Similar to Linked List, basics , types , operations (20)

ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
Reverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonReverse a Linked List In Place in Python
Reverse a Linked List In Place in Python
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
linked list
linked list linked list
linked list
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Team 10
Team 10Team 10
Team 10
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
Linked list
Linked listLinked list
Linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked List in Data Structure
Linked List in Data StructureLinked List in Data Structure
Linked List in Data Structure
 

Recently uploaded

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 

Recently uploaded (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdf
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 

Linked List, basics , types , operations

  • 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.
  • 6.
  • 7. To be called from main as
  • 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 )
  • 11. Insertion in a SLL • Insertion at the beginning
  • 12. Insertion in a SLL • Insertion at the end – Simply need to make the next ptr of the last node point to the new node instead of NULL
  • 13. Insertion in a SLL • Insertion at the end
  • 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
  • 15. Insertion in a SLL • Insertion after a particular node
  • 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.
  • 19. Deletion in a SLL • Deleting the first node in SLL
  • 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.
  • 21. Deletion in a SLL • Deleting the last node in SLL
  • 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
  • 33. Doubly Linked List--Insertion • Insertion at the beginning Node to be added Existing linked list
  • 34. Doubly Linked List--Insertion • Insertion at the beginning 1st step:- Make it point to head
  • 35. Doubly Linked List--Insertion • Insertion at the beginning 2nd step:- make prev point to temp
  • 37. Insertion at the beginning in a DLL
  • 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
  • 42. Insertion at the end in a DLL
  • 43. Insertion at the end in a DLL
  • 44. Insertion at the end in a DLL
  • 45. Insertion at the end in a DLL
  • 46. Insertion after a node in a DLL
  • 47. Insertion after a node in a DLL 4000 newP There must be a pointer pointing to this new node We need a ptr pointing to this 2nd node
  • 48. Insertion after a node in a DLL temp will point to the 2nd node as per this code
  • 49. Insertion after a node in a DLL Now we have pointers pointing position node and after node of the desired location to insert
  • 50. Insertion after a node in a DLL Make this ptr point to prev of new node code
  • 51. Insertion after a node in a DLL Now prev part of this node needs to point next of new node code
  • 52. Insertion after a node in a DLL Prev of this node points to next of temp and next of this node points to prev of temp2 code
  • 53. Insertion after a node in a DLL
  • 54. Insertion after a node in a DLL
  • 57. Deletion from beginning in a DLL Step1:-Let a temp node point the head node
  • 58. Deletion from beginning in a DLL Step2:- let head point to next node now code
  • 59. Deletion from beginning in a DLL Step:-Remove the temp node
  • 60. Deletion from beginning in a DLL Step:- Assign NULL to temp and prev of head node
  • 61. Deletion from beginning in a DLL • Method 2
  • 63. Deletion from particular position in a DLL
  • 64. Deletion from a particular position in a DLL Initially temp is pointing to head and moving towards the position to be deleted
  • 65. Deletion from a particular position in a DLL Step:- take 1 more ptr to point to node previous to temp
  • 66. Deletion from a particular position in a DLL Step:- next of temp2 should now point to next of temp
  • 67. Deletion from a particular position in a DLL Step:- update this so that it points to next of temp2
  • 68. Deletion from a particular position in a DLL Step:- free memory of temp pointer
  • 69. Deletion from a particular position in a DLL
  • 70. Deletion from a particular position in a DLL
  • 71. Delete last node from a DLL
  • 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;
  • 75. Delete last node from a DLL
  • 76. Delete last node from a DLL
  • 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)
  • 83. Circular Linked List--Operations • Insertion • Deletion • Display
  • 84. Circular Linked List--Insertion • Insertion – At the beginning – At the end – At a particular position
  • 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
  • 87. Circular Linked List—Insertion at beginning Step:- Update this ptr Let it point to NEXT ptr of tail
  • 88. Circular Linked List—Insertion at beginning Step:- Update the NEXT of tail ptr
  • 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
  • 91. Circular Linked List– Insertion at end Final result
  • 92. Circular Linked List– Insertion between nodes Initial State
  • 93. Circular Linked List– Insertion between nodes Step:- a ptr is needed to point this node as we need to reach here
  • 94. Circular Linked List– Insertion between nodes Step:- this ptr needs to be updated
  • 95. Circular Linked List– Insertion between nodes Step:- NEXT of this node will point to new node now code
  • 96. Circular Linked List– Insertion between nodes • If pos =3(similar to inserting at the end,with slight changes)
  • 97. Circular Linked List--Deletion • Deletion – At the beginning – At the end – At a particular position
  • 98. CLL – Deletion of first node
  • 99. CLL – Deletion of first node To delete, we are making a temp ptr point to first node
  • 100. CLL – Deletion of first node code
  • 101. CLL – Deletion of first node Step:- let’s free this node now
  • 102. CLL – Deletion of last node
  • 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
  • 105. CLL – Deletion of last node
  • 106. CLL – Deletion of intermediate node
  • 107. CLL – Deletion of intermediate node
  • 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
  • 110. CLL – Deletion of intermediate node
  • 111. CLL – Deletion of intermediate node free(temp2)
  • 112. CLL – Deletion of intermediate node
  • 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]