0% found this document useful (0 votes)
78 views

DS - Doubly Linked List

A doubly linked list is a type of linked list where each node contains a pointer to both the next and previous nodes. This allows traversal in both directions, unlike a singly linked list which only supports forward traversal. Each node in a doubly linked list contains data, a pointer to the next node, and a pointer to the previous node. Operations like insertion and deletion require updating both the previous and next node pointers. Advantages include bidirectional traversal and ability to insert/delete from both ends. Disadvantages include use of more memory per node and more pointer adjustments during operations compared to a singly linked list.

Uploaded by

Asif Sarfraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

DS - Doubly Linked List

A doubly linked list is a type of linked list where each node contains a pointer to both the next and previous nodes. This allows traversal in both directions, unlike a singly linked list which only supports forward traversal. Each node in a doubly linked list contains data, a pointer to the next node, and a pointer to the previous node. Operations like insertion and deletion require updating both the previous and next node pointers. Advantages include bidirectional traversal and ability to insert/delete from both ends. Disadvantages include use of more memory per node and more pointer adjustments during operations compared to a singly linked list.

Uploaded by

Asif Sarfraz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structures & Algorithms

Doubly Linked List

Computer Science Department


Introduction
• The singly linked list contains only one pointer
field i.e. every node holds an address of next
node.

• The singly linked list is uni-directional i.e. we can


only move from one node to its successor.

• This limitation can be overcome by Doubly


linked list.

Computer Science Department


Doubly Linked List
• In Doubly linked list, each node has two pointers.
• One pointer to its successor (NULL if there is
none) and one pointer to its predecessor (NULL
if there is none).
• These pointers enable bi-directional traversing.

*Previous Data *Next

Computer Science Department


A Singly Linked List
Head

A Doubly Linked List

Head

Computer Science Department


Comparison of Linked Lists
• Linked list
struct Node {
int data;
Node* next;
};
• Doubly linked list
struct Node {
Node *previous;
int data;
Node *next;
};

Computer Science Department


Insertion
• In insertion process, element can be inserted in three
different places
– At the beginning of the list
– At the end of the list
– At the specified position.

• To insert a node in doubly linked list, you must update


pointers in both predecessor and successor nodes.

Computer Science Department


Insertion

Computer Science Department


Doubly Linked List
void insert(){
struct DoublyList{
DoublyList * newNode = new
int data;
DoublyList;
DoublyList * prev;
if (last == NULL){
DoublyList * next;
newNode->prev =
};
newNode->next = NULL;
DoublyList * first = NULL;
first = last = newNode;
DoublyList * last = NULL;
}
void main(){
else{
//switch statement
newNode->next = NULL;
insert(10);
newNode->prev = last;
insert(20);
last->next = newNode;
delete();
last = newNode;
traverse();
}
} Computer Science Department
}
Doubly Linked List
void traverse(){ newNode->data = x;
DoublyList * temp = front; while(temp!=NULL){
while(temp!=NULL){ if(temp->data > x){
cout<<temp->data; newNode->next = temp;
temp = temp->next; newNode->prev = temp->prev;
} temp->prev->next = newNode;
} temp->prev = newNode;
void insertAT(int x){ }
// assuming that list is in temp = temp->next;
ascending order }
DoublyList * temp = front;
DoublyList * newNode = new }
DoublyList;

Computer Science Department


Deletion
• In deletion process, element can be deleted from three
different places
– From the beginning of the list
– From the end of the list
– From the specified position in the list.

• When the node is deleted, the memory allocated to that


node is released and the previous and next nodes of that
node are linked

Computer Science Department


Deletion

Computer Science Department


Doubly Linked List (Delete)
void delete ( int x ){
DoublyList * temp = front;

DoublyList * toDelete = NULL;


while( temp != NULL ){

if( temp->data == x ){
temp->prev->next = temp-next;
temp->next->prev = temp->prev;
toDelete = temp;
delete toDelete;
}
temp = temp->next;
}
}
Computer Science Department
Advantages of Doubly Linked List
1. The doubly linked list is bi-directional, i.e. it can be
traversed in both backward and forward direction.

2. The operations such as insertion, deletion and


searching can be done from both ends.

3. Previous and Next records of any element can be


searched quickly

Computer Science Department


Disadvantages

1. It consume more memory space (one extra pointer with


each node).
2. More pointer adjustments during insertion and deletion
of element.

Computer Science Department

You might also like