0% found this document useful (0 votes)
6 views9 pages

Doubly Linked List in Data Structure

A doubly linked list is a data structure where each node contains data and two pointers for bidirectional traversal, allowing efficient insertions and deletions at both ends. It has advantages such as dynamic sizing and easier navigation compared to singly linked lists, but it also has drawbacks like increased memory usage and complexity. Applications include navigation systems, music players, and implementing undo functionality in software.

Uploaded by

jananippriya18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Doubly Linked List in Data Structure

A doubly linked list is a data structure where each node contains data and two pointers for bidirectional traversal, allowing efficient insertions and deletions at both ends. It has advantages such as dynamic sizing and easier navigation compared to singly linked lists, but it also has drawbacks like increased memory usage and complexity. Applications include navigation systems, music players, and implementing undo functionality in software.

Uploaded by

jananippriya18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Doubly Linked List in Data Structure?

 A doubly linked list is a type of data structure where each element, known as a node,
holds data and two references: one pointing to the next node and one pointing to the
previous node.
 This two-way linking allows for movement in both directions through the list,
making it more versatile than a singly linked list which only allows movement in one
direction.

Structure of a Doubly Linked List

Node:

Each node in a doubly linked list includes:

 Data: The actual information held within the node, which could be numbers, strings,
or any other data type.

 Next Pointer: A reference to the next node in the list, which helps in traversing the
list forward.

 Prev Pointer: A reference to the previous node in the list, which facilitates backward
traversal.

Work Process

 Head and Tail: The list has two special nodes, the head and the tail. The head points
to the first element of the list, while the tail points to the last element. These pointers
enhance operations such as adding or removing elements at the ends of the list.

 Traversal: You can start from the head to move forward through the list or from the
tail to move backward, making it versatile for various operations that require scanning
the list in either direction.

 Insertions and Deletions: Adding or removing nodes can be done efficiently at any
point in the list. When inserting or deleting a node, pointers from adjacent nodes are
updated to maintain the list’s integrity without needing to shift elements as in
contiguous data structures like array
Doubly Linked List Operations

Operations on doubly linked lists in data structures are more versatile compared to singly
linked lists due to their two-way linking of nodes:

1. Insertion

Inserting nodes into a doubly linked list can be done in several ways, each using the ability to
access nodes from both directions:

 At the Beginning: Add a new node at the start of the list. Set the new node's next
pointer to the current head of the list, and update the prev pointer of the existing head
to point back to the new node. Update the head pointer to the new node.

 At the End: Add a new node at the end. Traverse to the last node, set the next pointer
of the last node to the new node, and set the prev pointer of the new node to the last
node. Update the tail pointer if maintained.

 After a Given Node: To insert a node after a given node, adjust the next pointer of
the new node to the next pointer of the given node, update the next pointer of the
given node to the new node, and set the prev pointer of the node that follows the new
node (if any) to the new node.

2. Deletion

Removing nodes requires adjustment of pointers from both the preceding and succeeding
nodes:

 From the Beginning: Remove the head node by updating the head to the second node
and setting the prev pointer of the new head to null.

 From the End: Remove the tail node by setting the next pointer of the second last
node to null and updating the tail to this second last node.

 A Specific Node: Disconnect the node by adjusting the next pointer of the preceding
node to point to the node after the target node, and adjust the prev pointer of the
succeeding node likewise.

3. Search

Searching involves traversing through the list either from the head or the tail, depending on
proximity and potentially the direction of traversal that may optimize the search:

 Search by Value: Start from the head (or tail) and traverse through the next (or prev)
pointers to find the node containing the desired value.

4. Traversal

Traversal can be performed in both directions, which is a significant advantage of doubly


linked lists:
 Forward Traversal: Start from the head and move through each node using the next
pointers.

 Backward Traversal: Start from the tail (if available) and move through each node
using the prev pointers.

5. Update

Updating the value of a node can be performed directly once the node is accessed, without
any specific need for traversal if the node reference is already known.

Operation Time Complexity

Traversal O(n)

Insertion at the Beginning O(1)

Insertion at the End O(1)

Insertion Into Other Nodes O(n)

Insertion Into Empty Linked List O(1)

Deletion at the Beginning O(1)

Deletion at the End O(1)

Deletion at Other Positions O(n)

Advantages of Doubly Linked Lists

 Bidirectional Navigation: Allows traversal in both forward and backward directions,


facilitating easier and more flexible data manipulation.

 Easier Insertion and Deletion: Nodes can be added or removed from both ends and
the middle of the list without needing to traverse the entire list, especially if the tail
pointer is maintained.

 Efficient Operations at Both Ends: Adding or removing elements at the beginning


and the end is efficient because both head and tail pointers provide direct access to the
endpoints of the list.

 Dynamic Size: The size of the list can increase or decrease dynamically, which is
efficient for memory usage since it allocates space only as needed.
Disadvantages of Doubly Linked Lists
 Increased Memory Usage: Each node requires extra memory for an additional
pointer (previous pointer), which can be significant in memory-constrained
environments.

 Complexity: Managing two pointers (next and prev) per node increases the
complexity of the operations, making the code more prone to errors such as memory
leaks and pointer corruption.

 Slower Individual Operations: The overhead of maintaining an extra pointer can


slightly slow down operations compared to singly linked lists, as more pointer
operations are required.

 Overhead in Memory Management: More complex memory management is


needed, especially in languages that do not handle garbage collection automatically,
due to the additional pointers that need to be correctly handled during insertions and
deletions
Uses and Applications of Doubly Linked List

Doubly linked lists find a wide range of applications in software development and system
design due to their ability to efficiently add, remove, and access elements from both ends:

 Navigation Systems: Doubly linked lists are ideal for applications where users need
to navigate both forward and backward, such as web browsers or document viewers.

 Music Players: In media playback software, doubly linked lists can manage playlists
where users might want to go to the next or previous track. This allows for seamless
navigation through the playlist.

 Undo Functionality in Applications: Many applications like text editors or graphic


design software use doubly linked lists to implement undo and redo functionalities.
Each node in the list could represent a state of the work, and navigating through the
nodes allows users to undo or redo changes in their projects.

 Gaming: In gaming, doubly linked lists can be used to manage various game states or
the inventory of items that players can cycle through both forward and backward.

 Implementing Other Data Structures: Doubly linked lists are also used to
implement more complex data structures like deque (double-ended queue) which
requires adding and removing items from both ends efficiently.

 Memory Management: In systems programming, doubly linked lists can be used to


keep track of free memory blocks or resources. This allows the system to efficiently
allocate and deallocate resources by adjusting pointers rather than moving memory
blocks.
Difference Between Singly and Doubly Linked List

Feature Singly Linked List Doubly Linked List

Only forwards. You can Both forwards and backwards. You


Direction of
traverse from the head to can traverse from the head to the tail
Traversal
the end of the list. and vice versa.

Less memory per node (one More memory per node (two pointers
Memory Usage
pointer per node). per node).

Efficient at both the beginning and


Efficient at the beginning;
the end; easier insertions and
Insertions/Deletions requires traversal from the
deletions in the middle without full
head for other positions.
traversal.

Simpler and requires less More complex due to additional


Complexity code to manage compared pointers, requiring more management
to doubly linked lists. and care in code.

Suitable when memory is a Preferred when frequent operations


Use Case concern and only forward require elements to be accessed from
traversal is needed. both ends.

Typically slower for


operations that involve Faster for operations involving the
Operations
elements at the end of the end of the list due to the tail pointer.
list.

Both head and tail pointers are used,


Head and Tail
Only head pointer is used. providing immediate access to both
Management
ends of the list.

Circular Linked List in Data Structures


A circular linked list is a variation of linked lists where the pointer of the last node instead of
pointing to NULL points to the first or the head node. This connects the first node with the
last node resulting in a circle of interconnected nodes. There is no NULL at the end. We can
traverse a circular linked list until we reach the same node where we started as there is no
beginning and no ending.

Representation of a Circular Linked List in Data Structures

Types of Circular Linked Lists in Data Structures

There are two types of circular linked Lists in Data Structures:

1. Circular Singly Linked List: Here, each node has only one pointer i.e. next pointer.
The next pointer of the last node of the list points to the first node of the list creating a
circular structure. We can transverse only in one direction in a circular manner in this.

2. Doubly Circular Linked List: We saw there are two pointers, prev and next in
the doubly linked lists in data structures. In this type also each node has two
pointers, prevpoints to the previous node, and the next points to the next node. Here,
in addition to the last node's next pointer storing the address of the first node, even the
first node's prev pointer will store the address of the last node thus forming a circular
structure.

Standard Operations on Circular Linked Lists in Data Structures


1. Insertion

Adding a node to a circular linked list is similar to adding a node to a linked list. The only
extra work is to connect the last node with the first node. In the above given circular linked
list, Insertion can be done in three ways:

1. Insertion at the beginning: Let us understand this with an illustration. Suppose we


want to insert a node with the value 6 at the beginning of the given circular linked list.
The following three steps to accomplish this operation:
1. Create a new node
 allocate memory for new Node
 assign the data to the new Node.

2. store the address of the current first node in the new Node (i.e. pointing the
new Node to the current first node)
3. point the last node to new Node (i.e making new Node as head)

Example of Insertion at the beginning of a Circular Linked List

Insertion at a specific position/in between the nodes: Suppose we want to insert a node
with value 6 after the head node with value 1 in the given circular linked list. The following
three steps to accomplish this operation:

1. travel to the node given i.e. node 1


2. point the next of new Node to the node next to node 1
3. store the address of the new Node at the next of node 1.
Insertion at the end: Suppose we want to insert a node with value 6 after the last
node with value 3 in the given circular linked list. The following three steps to
accomplish this operation:

1. store the address of the head node to the next of new Node (making new Node
the last node)
2. point the current last node to new Node
3. make new Node as the last node

2. Deletion

In the above given circular linked list, deletion can also be done in three ways:

1. If it's the only node: Follow the given two steps procedure
1. free the memory occupied by the node
2. store NULL in the last
2. If it's the last node: Follow the given four steps procedure
1. find the node before the last node (let it be temp)
2. store the address of the node next to the last node in temp
3. free the memory of the last
4. make temp the last node

3. If it's any other node: Follow the given four steps procedure
1. travel to the node to be deleted (here we are deleting node two)
2. Let the node before node two be temp
3. store the address of the node next to two in temp
4. free the memory of two

3. Traversal

We can access each element of the circular linked list starting from
the head node until we reach back to it.
Complexity Analysis of Circular Linked List Operations

Operations Time Complexity Space Complexity

Insertion O(1) or O(N) O(1)

Deletion O(1) O(1)


Traversal O(N) O(1)
Applications of Circular Linked List

1. This is used in multiplayer games to give each participant a chance to play.


2. In an operating system, a circular linked list can be used to organize many running
applications.
3. In resource allocation difficulties, circular linked lists might be useful.
4. Circular linked lists are frequently used to build circular buffers.
5. They can also be used in simulation and gaming.

Advantages of Circular Linked Lists

1. Any node can be a starting point. We can traverse the whole list by starting from any
point. We just need to stop when the first visited node is visited again.
2. Useful for the implementation of a queue. Unlike this implementation, we don’t need
to maintain two-pointers for the front and rear if we use a circular linked list. We can
maintain a pointer to the last inserted node and the front can always be obtained as
next of last.
3. Circular Doubly Linked Lists are used for the implementation of advanced data
structures like the Fibonacci Heap.
4. Implementing a circular linked list can be relatively easy compared to other more
complex data structures like trees or graphs.

Disadvantages of Circular Linked Lists

1. Compared to singly linked lists, circular lists are more complex.


2. Reversing a circular list is more complicated than reversing a singly or doubly linked
list.
3. The code can go into an infinite loop if it is not handled carefully.
4. It is harder to find the end of the list and control the loop.
5. Although circular linked lists can be efficient in certain applications, their
performance can be slower than other data structures in certain cases, such as when
the list needs to be sorted or searched.
6. Circular linked lists don’t provide direct access to individual nodes.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy