Unit 1-Linear Data Structures: Linked List Implementation
Unit 1-Linear Data Structures: Linked List Implementation
1
Introduction
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
– It can be made just as long as required.
head
– It does not waste memory space.
A B C
2
• It improves on the construction and
management of an array and Python list by
requiring smaller memory allocations and no
element shifts for insertions and deletions.
• The singly linked list is a linear structure in which
traversals start at the front and progress, one
element at a time, to the end. Other variations
include the circularly linked, the doubly linked,
and the circularly doubly linked list
3
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).
Item to be
tmp X inserted
A B C
curr
X
A B C
tmp
curr
A B C
8
Uses of Linked Lists
• Due to their dynamic size allocation and ease of
insertion/deletion, linked lists are applied in a lot of use cases.
• They’re used to implement a lot of complex data structures
like the adjacency list in graphs.
• They are used for lifecycle management in operating systems.
• A playlist in a music application is implemented using a
doubly linked list.
• Block chain, a complex data structure that is used for crypto
currencies and ledgers use a linked list at their core.
A B C
A B C
A B C
Insert
List
implementation
Delete
and the
related functions
Traverse
15
Pointer structures
• Pointer structures are lists of items that can be spread
out in memory.
• This is because each item contains one or more links
to other items in the structure.
• There are several benefits with pointer structures.
• First of all, they don't require sequential storage
space.
• Second, they can start small and grow arbitrarily as
you add more nodes to the structure
16
Singly Linked Lists
18
Introduction
• A singly linked list is a list with only one
pointer between two successive nodes.
• It can only be traversed in a single direction,
that is, you can go from the first node in the
list to the last node, but you cannot move
from the last node to the first node.
A B C
22
SLL-Insert operations
23
SLL – Deletion operations
24
SLL –Display Operations
25
SLL- Searching Operations
26
• ll.search(head, data) -> Search the given
element in the Linked List.
• ll.print_list() -> Print the linked list.
• ll.size() -> Return the length of the linked list.
• ll.insert(ele) -> Insert the given node into the
linked list.
• ll.delete(data) -> Delete the given element
from the linked list.
27
Doubly Linked Lists
28
Introduction
• A node in a doubly linked list has two
pointers: a pointer to the next node and a
pointer to the previous node:
29
• Doubly linked lists can be traversed in any
direction.
• Since there is immediate access to both next
and previous nodes, deletion operations are
much easier to perform
30
Creating a Node in DLL
31
Doubly linked list
• It is still important to create a class that
captures the data that our functions will be
operating on:
32
DLL-Insert Operation.
33
DLL- Deletion Operations
The algorithm for removing nodes from a doubly linked
list caters for basically four scenarios before deletion of
a node is completed. These are:
•When the search item is not found at all
•When the search item is found at the very beginning of the list
•When the search item is found at the tail end of the list
•When the search item is found somewhere in the middle of the
list
35
DLL –Deletion Operations
36
DLL- Display Operations
37
Circular Linked Lists
38
Introduction
• A circular list is a special case of a linked list. It is a list where
the endpoints are connected.
• That is, the last node in the list points back to the first node.
• Circular lists can be based on both singly and doubly linked
lists. In the case of a doubly linked circular list, the first node
also needs to point to the last node.
39
Implementing Circular Linked lists
• To create a circular linked list, we create two classes:
the first one for nodes and the second one for the
linked list that will use the nodes.
• For the node class, we have two members. One to
store data and the other to store the link to the next
node. The class definition will be:
40
Class: Circular Linked List
•This class will use nodes created by the
previous class to implement a circular linked list.
It will contain one head node, one count
member, and multiple methods for specific
tasks.
41
CLL – Appending Elements
• When we append an element to the circular list, we need to
make sure that the new node points back to the tail node
42
CLL- Deleting elements
43
Applications