0% found this document useful (0 votes)
17 views6 pages

Doubly Linked List

A Doubly Linked List (DLL) is a linked list where each node contains data, a pointer to the next node, and a pointer to the previous node, allowing for two-way traversal. The document outlines operations for inserting and deleting nodes at various positions, as well as displaying the list in both forward and reverse order. DLLs are beneficial for applications requiring efficient navigation, such as browser history, undo/redo functionality, and memory management in operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Doubly Linked List

A Doubly Linked List (DLL) is a linked list where each node contains data, a pointer to the next node, and a pointer to the previous node, allowing for two-way traversal. The document outlines operations for inserting and deleting nodes at various positions, as well as displaying the list in both forward and reverse order. DLLs are beneficial for applications requiring efficient navigation, such as browser history, undo/redo functionality, and memory management in operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Doubly Linked List (DLL)

A Doubly Linked List (DLL) is a type of linked list in which each node contains:

 Data
 A pointer to the next node (next)
 A pointer to the previous node (prev)

The purpose of a doubly linked list is to enable both-way traversal while still
allowing non-contiguous memory storage. Just like a singly linked list, we need to
have a starting pointer pointing to the first node. The last node in the list points to
NULL.

Node Structure

Pseudocode:

struct Node
{
int data; //The data point
struct Node *Prev; //Pointer to the previous node
struct Node *Next; //Pointer to the next node
};
Operations on a Doubly Linked List
1. Insert at the Beginning

 Create a new node.


 Set its next to the current head.
 Set prev of the current head to the new node.
 Update head to the new node.

Pseudocode:

Function insertAtBeginning(data):
newNode ← Create a new node with data
newNode.next ← head
if head ≠ NULL:
head.prev ← newNode
head ← newNode

2. Insert at the End

 Traverse to the last node.


 Set next of the last node to the new node.
 Set prev of the new node to the last node.

Pseudocode:

Function insertAtEnd(data):
newNode ← Create a new node with data
if head = NULL:
head ← newNode
return
temp ← head
while temp.next ≠ NULL:
temp ← temp.next
temp.next ← newNode
newNode.prev ← temp

3. Insert at a Specific Position

 Traverse to the (position - 1) node.


 Update pointers to insert the new node.

Pseudocode:

Function insertAtPosition(data, pos):


if pos = 1:
insertAtBeginning(data)
return
newNode ← Create a new node with data
temp ← head
for i ← 1 to pos-1:
if temp = NULL:
print "Position out of range"
return
temp ← temp.next
newNode.next ← temp
newNode.prev ← temp.prev
temp.prev.next ← newNode
temp.prev ← newNode

4. Delete from the Beginning

 Update head to the next node.


 Set prev of new head to NULL.

Pseudocode:

Function deleteFromBeginning():
if head = NULL:
print "List is empty"
return
head ← head.next
if head ≠ NULL:
head.prev ← NULL
5. Delete from the End

 Traverse to the second last node.


 Update its next to NULL.

Pseudocode:

Function deleteFromEnd():
if head = NULL:
print "List is empty"
return
temp ← head
while temp.next ≠ NULL:
temp ← temp.next
if temp.prev ≠ NULL:
temp.prev.next ← NULL
else:
head ← NULL

6. Delete from a Specific Position

 Traverse to the node to be deleted.


 Update next of the previous node and prev of the next node.

Pseudocode:

Function deleteAtPosition(pos):
if head = NULL:
print "List is empty"
return
temp ← head
for i ← 1 to pos:
if temp = NULL:
print "Position out of range"
return
temp ← temp.next
if temp.prev ≠ NULL:
temp.prev.next ← temp.next
if temp.next ≠ NULL:
temp.next.prev ← temp.prev

7. Display Forward

Function displayForward():
temp ← head
while temp ≠ NULL:
print temp.data
temp ← temp.next

8. Display Reverse

Function displayReverse():
temp ← head
while temp.next ≠ NULL:
temp ← temp.next
while temp ≠ NULL:
print temp.data
temp ← temp.prev

Uses of Doubly Linked List:

1. Efficient Forward and Backward Traversal


o Unlike singly linked lists, DLLs can be traversed in both directions
efficiently.
o Useful in applications like browser history, where you can move back
and forth.
2. Implementation of Undo/Redo Functionality
o Many text editors and applications use DLLs to track changes for
undo/redo operations.
3. Navigation in Music and Image Viewers
o Media players and image galleries allow users to move to the previous
or next item using a doubly linked list.
4. Used in LRU (Least Recently Used) Cache Implementation
o Operating systems and databases use DLLs for memory management
and caching.
5. Efficient Insertion and Deletion
o Unlike arrays, inserting or deleting an element in a DLL does not
require shifting elements.
o Useful in memory management systems.

You might also like