SlideShare a Scribd company logo
LINKED LIST
Lecture 05
IT 2301 / CS 2301
Dhanushka Jayasinghe
What is Linked List ?
• A linked list is the most sought-after data structure when it comes to
handling dynamic data elements.
• A linked list is a linear data structure that includes a series of connected
nodes. Here, each containing data fields and one or two references (links)
pointing to the next and/or previous nodes.
• The last node contains null in its second field because it will point to no node.
• A linked list can grow and shrink its size, as per the requirement.
• It does not waste memory space.
Linked List Representation
Why use linked list over Array?
Array contains following limitations:
• The size of array must be known in advance before using it in the program.
• Increasing size of the array is a time taking process. It is almost impossible to expand the size
of the array at run time.
• All the elements in the array need to be contiguously stored in the memory. Inserting any
element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array. Using linked
list is useful because,
• It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored
in the memory and linked together with the help of pointers.
• Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available memory
space.
Types of Linked List
Following are the various types of linked list.
1) Singly Linked List − The nodes only point to the address of the next node in the list.
1) Doubly Linked List − The nodes point to the addresses of both previous and next nodes.
1) Circular Linked List − The last node in the list will point to the first node in the list. It can
either be singly linked or doubly linked
Singly Linked Lists
Singly linked lists contain two “buckets” in one node; one bucket holds the data and the other
bucket holds the address of the next node of the list. Traversals can be done in one direction
only as there is only a single link between two nodes of the same list.
Doubly Linked Lists
Doubly Linked Lists contain three “buckets” in one node; one bucket holds the data and the
other buckets hold the addresses of the previous and next nodes in the list. The list is traversed
twice as the nodes in the list are connected to each other from both sides.
Circular Linked Lists
⮚ In a circularly linked list, the first and final nodes are linked together. This can be
done for both singly and doubly linked lists.
⮚ Begin at any node and follow the list in either direction until return to the original
node.
⮚ No beginning or end.
⮚ Since the last node and the first node of the circular linked list are connected, the
traversal in this linked list will go on forever until it is broken.
• Singly-circularly-linked List
• Doubly-circularly-inked List
Singly Circularly Linked List
Doubly Circularly Linked List
Linked List
Node
A node consists with the data stored in the node and a pointer to the next node in the list
How to declare a linked list?
Linked list contains two parts, and both are of different types, i.e., one is the simple
variable, while another is the pointer variable. We can declare the linked list by using
the user-defined data type structure.
struct node
{
int data;
struct node
*next;
} ;
Complexity of Linked List
Linked List Vs arrays
Operation Array Linked List
Indexing O(1) O(n)
Inserting/Deleting at the
end
O(1) O(1) or O(n)
Inserting/ deleting in the
middle with iterator
O(n) O(1)
Linked List vs Arrays
◦ Elements can be inserted into linked lists indefinitely
◦ An array will eventually either fill up or need to be resized
◦ Arrays allow random access while linked list allow only sequential
access to elements
◦ Singly linked lists can traverse only in one direction
◦ Linked lists require extra storage for reference
Doubly Linked vs Singly Linked
oDoubly Linked Lists required more space per node
oDLL is easier to manipulate because they allow sequential access to the list in
both directions.
oCan be insert or delete node in a constant number of operations given only
that nodes address
oSLL requires the previous nodes address in order to correctly insert or delete
Circularly Linked vs Linearly Linked
o Circular Linked Lists are most useful for describing naturally circular
structures
o Able to traverse the list starting at any point
o Quick access to the first and the last record through a single pointer
o Complexity of iteration
Advantages of Linked list
• Dynamic data structure - The size of the linked list may vary according to the
requirements. Linked list does not have a fixed size.
• Insertion and deletion - Unlike arrays, insertion, and deletion in linked list is
easier. Array elements are stored in the consecutive location, whereas the
elements in the linked list are stored at a random location. To insert or delete an
element in an array, we have to shift the elements for creating the space.
Whereas, in linked list, instead of shifting, we just have to update the address of
the pointer of the node.
• Memory efficient - The size of a linked list can grow or shrink according to the
requirements, so memory consumption in linked list is efficient.
• Implementation - Various advanced data structures can be implemented using a
linked list like a stack, queue, graph, hash maps, etc.
Disadvantages of Linked list
• Memory usage - In linked list, node occupies more memory than array. Each
node of the linked list occupies two types of variables, i.e., one is a simple
variable, and another one is the pointer variable.
• Traversal - Traversal is not easy in the linked list. If we have to access an
element in the linked list, we cannot access it randomly, while in case of array
we can randomly access it by index. For example, if we want to access the 3rd
node, then we need to traverse all the nodes before it. So, the time required
to access a particular node is large.
• Reverse traversing - Backtracking or reverse traversing is difficult in a linked
list. In a doubly-linked list, it is easier but requires more memory to store the
back pointer.
Linked List operations
1.Traversing: To traverse all nodes one by one.
2.Insertion: To insert new nodes at specific positions.
3.Deletion: To delete nodes from specific positions.
4.Searching: To search for an element from the linked list.
Linked List operations
SINGLY LINKED LIST
Insertion - At the front of the linked
list
1. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and assign the head pointer to it.
5. If the list is not empty, add the data to a node and link to the current head. Assign the head to
the newly added node.
6. END
Insertion - After a given node
1. START
2. Create a new node and assign data to it
3. Iterate until the node at position is found
4. Point first to new first node
5. END
Insertion - At the end of the linked list
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
To be Continued..

More Related Content

PPTX
Link list
PPTX
Link list
PPTX
Linked List
PPTX
Linked list
PPTX
linked list in Data Structure, Simple and Easy Tutorial
PPTX
Linked lists linked lists vs Arrays.pptx
PPTX
Data Structures-UNIT Four_Linked_List.pptx
PPTX
Datastucture-Unit 4-Linked List Presentation.pptx
Link list
Link list
Linked List
Linked list
linked list in Data Structure, Simple and Easy Tutorial
Linked lists linked lists vs Arrays.pptx
Data Structures-UNIT Four_Linked_List.pptx
Datastucture-Unit 4-Linked List Presentation.pptx

Similar to Data Structures and Algorithms - Lec 05.pptx (20)

DOCX
Introduction to linked lists
PPT
ANOITO2341988888888888888888888885555.ppt
PPTX
Linked list in Data Structure and Algorithm
PPTX
Linked_List_Presentation_1.pptxjsjkskxjjdjekdlkdjmdkdk
PPT
Lecture 2b lists
PPTX
Linear Data Structures - List, Stack and Queue
PPT
Linked List
PPT
Link list using array in Data structure amd algorithms
ODP
Linked List
DOCX
Linked List
PPTX
CH4.pptx
PPTX
Linked List Representation of a Linked List.pptx
PPTX
DSA-Linked-List-.. learning process.pptx
PPTX
LINKED LIST.pptx
PPTX
link list.pptx complete notes detailed ans
PDF
Static arrays are structures whose size is fixed at compile time and.pdf
PDF
Linked list (introduction) 1
PDF
DS Module 03.pdf
PPTX
Data Structures Introduction & Linear DS
Introduction to linked lists
ANOITO2341988888888888888888888885555.ppt
Linked list in Data Structure and Algorithm
Linked_List_Presentation_1.pptxjsjkskxjjdjekdlkdjmdkdk
Lecture 2b lists
Linear Data Structures - List, Stack and Queue
Linked List
Link list using array in Data structure amd algorithms
Linked List
Linked List
CH4.pptx
Linked List Representation of a Linked List.pptx
DSA-Linked-List-.. learning process.pptx
LINKED LIST.pptx
link list.pptx complete notes detailed ans
Static arrays are structures whose size is fixed at compile time and.pdf
Linked list (introduction) 1
DS Module 03.pdf
Data Structures Introduction & Linear DS
Ad

Recently uploaded (20)

PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
573137875-Attendance-Management-System-original
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CH1 Production IntroductoryConcepts.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Internet of Things (IOT) - A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
R24 SURVEYING LAB MANUAL for civil enggi
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
573137875-Attendance-Management-System-original
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Construction Project Organization Group 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CH1 Production IntroductoryConcepts.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Model Code of Practice - Construction Work - 21102022 .pdf
Ad

Data Structures and Algorithms - Lec 05.pptx

  • 1. LINKED LIST Lecture 05 IT 2301 / CS 2301 Dhanushka Jayasinghe
  • 2. What is Linked List ? • A linked list is the most sought-after data structure when it comes to handling dynamic data elements. • A linked list is a linear data structure that includes a series of connected nodes. Here, each containing data fields and one or two references (links) pointing to the next and/or previous nodes. • The last node contains null in its second field because it will point to no node. • A linked list can grow and shrink its size, as per the requirement. • It does not waste memory space.
  • 4. Why use linked list over Array? Array contains following limitations: • The size of array must be known in advance before using it in the program. • Increasing size of the array is a time taking process. It is almost impossible to expand the size of the array at run time. • All the elements in the array need to be contiguously stored in the memory. Inserting any element in the array needs shifting of all its predecessors. Linked list is the data structure which can overcome all the limitations of an array. Using linked list is useful because, • It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored in the memory and linked together with the help of pointers. • Sizing is no longer a problem since we do not need to define its size at the time of declaration. List grows as per the program's demand and limited to the available memory space.
  • 5. Types of Linked List Following are the various types of linked list. 1) Singly Linked List − The nodes only point to the address of the next node in the list. 1) Doubly Linked List − The nodes point to the addresses of both previous and next nodes. 1) Circular Linked List − The last node in the list will point to the first node in the list. It can either be singly linked or doubly linked
  • 6. Singly Linked Lists Singly linked lists contain two “buckets” in one node; one bucket holds the data and the other bucket holds the address of the next node of the list. Traversals can be done in one direction only as there is only a single link between two nodes of the same list.
  • 7. Doubly Linked Lists Doubly Linked Lists contain three “buckets” in one node; one bucket holds the data and the other buckets hold the addresses of the previous and next nodes in the list. The list is traversed twice as the nodes in the list are connected to each other from both sides.
  • 8. Circular Linked Lists ⮚ In a circularly linked list, the first and final nodes are linked together. This can be done for both singly and doubly linked lists. ⮚ Begin at any node and follow the list in either direction until return to the original node. ⮚ No beginning or end. ⮚ Since the last node and the first node of the circular linked list are connected, the traversal in this linked list will go on forever until it is broken. • Singly-circularly-linked List • Doubly-circularly-inked List
  • 11. Linked List Node A node consists with the data stored in the node and a pointer to the next node in the list
  • 12. How to declare a linked list? Linked list contains two parts, and both are of different types, i.e., one is the simple variable, while another is the pointer variable. We can declare the linked list by using the user-defined data type structure. struct node { int data; struct node *next; } ;
  • 13. Complexity of Linked List Linked List Vs arrays Operation Array Linked List Indexing O(1) O(n) Inserting/Deleting at the end O(1) O(1) or O(n) Inserting/ deleting in the middle with iterator O(n) O(1)
  • 14. Linked List vs Arrays ◦ Elements can be inserted into linked lists indefinitely ◦ An array will eventually either fill up or need to be resized ◦ Arrays allow random access while linked list allow only sequential access to elements ◦ Singly linked lists can traverse only in one direction ◦ Linked lists require extra storage for reference
  • 15. Doubly Linked vs Singly Linked oDoubly Linked Lists required more space per node oDLL is easier to manipulate because they allow sequential access to the list in both directions. oCan be insert or delete node in a constant number of operations given only that nodes address oSLL requires the previous nodes address in order to correctly insert or delete
  • 16. Circularly Linked vs Linearly Linked o Circular Linked Lists are most useful for describing naturally circular structures o Able to traverse the list starting at any point o Quick access to the first and the last record through a single pointer o Complexity of iteration
  • 17. Advantages of Linked list • Dynamic data structure - The size of the linked list may vary according to the requirements. Linked list does not have a fixed size. • Insertion and deletion - Unlike arrays, insertion, and deletion in linked list is easier. Array elements are stored in the consecutive location, whereas the elements in the linked list are stored at a random location. To insert or delete an element in an array, we have to shift the elements for creating the space. Whereas, in linked list, instead of shifting, we just have to update the address of the pointer of the node. • Memory efficient - The size of a linked list can grow or shrink according to the requirements, so memory consumption in linked list is efficient. • Implementation - Various advanced data structures can be implemented using a linked list like a stack, queue, graph, hash maps, etc.
  • 18. Disadvantages of Linked list • Memory usage - In linked list, node occupies more memory than array. Each node of the linked list occupies two types of variables, i.e., one is a simple variable, and another one is the pointer variable. • Traversal - Traversal is not easy in the linked list. If we have to access an element in the linked list, we cannot access it randomly, while in case of array we can randomly access it by index. For example, if we want to access the 3rd node, then we need to traverse all the nodes before it. So, the time required to access a particular node is large. • Reverse traversing - Backtracking or reverse traversing is difficult in a linked list. In a doubly-linked list, it is easier but requires more memory to store the back pointer.
  • 19. Linked List operations 1.Traversing: To traverse all nodes one by one. 2.Insertion: To insert new nodes at specific positions. 3.Deletion: To delete nodes from specific positions. 4.Searching: To search for an element from the linked list.
  • 21. Insertion - At the front of the linked list 1. START 2. Create a node to store the data 3. Check if the list is empty 4. If the list is empty, add the data to the node and assign the head pointer to it. 5. If the list is not empty, add the data to a node and link to the current head. Assign the head to the newly added node. 6. END
  • 22. Insertion - After a given node 1. START 2. Create a new node and assign data to it 3. Iterate until the node at position is found 4. Point first to new first node 5. END
  • 23. Insertion - At the end of the linked list 1. START 2. Create a new node and assign the data 3. Find the last node 4. Point the last node to new node 5. END