0% found this document useful (0 votes)
43 views17 pages

6-4-Doubly Linked List Tutorial

A doubly linked list is a complex data structure that allows efficient traversal in both directions due to each node containing pointers to both the previous and next nodes. It supports various operations such as insertion and deletion at the beginning, end, or specific positions, as well as traversal and length finding. The document provides detailed explanations and implementations of these operations in multiple programming languages.

Uploaded by

shubhrajkumar707
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)
43 views17 pages

6-4-Doubly Linked List Tutorial

A doubly linked list is a complex data structure that allows efficient traversal in both directions due to each node containing pointers to both the previous and next nodes. It supports various operations such as insertion and deletion at the beginning, end, or specific positions, as well as traversal and length finding. The document provides detailed explanations and implementations of these operations in multiple programming languages.

Uploaded by

shubhrajkumar707
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/ 17

Doubly Linked List Tutorial

Last Updated : 12 Aug, 2024

A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages.
The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This
is because each node in the list contains a pointer to the previous node and a pointer to the next node. This allows
for quick and easy insertion and deletion of nodes from the list, as well as efficient traversal of the list in both
directions.

What is a Doubly Linked List?


A doubly linked list is a data structure that consists of a set of nodes, each of which contains a value and two
pointers, one pointing to the previous node in the list and one pointing to the next node in the list. This allows
for efficient traversal of the list in both directions, making it suitable for applications where
frequent insertions and deletions are required.
Doubly Linked List

Representation of Doubly Linked List in Data Structure


In a data structure, a doubly linked list is represented using nodes that have three fields:

1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)

Node Structure of Doubly Linked List

Node Definition
Here is how a node in a Doubly Linked List is typically represented:

Try it on GfG
Practice
C++ C Java Python C# JavaScript

1
struct Node {

3
// To store the Value or data.

4
int data;

5
6
// Pointer to point the Previous Element

7
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node in the list, and a pointer
to the previous node in the list. By linking these nodes together through the next and prev pointers, we can
traverse the list in both directions (forward and backward), which is a key feature of a Doubly Linked List.

Operations on Doubly Linked List


Traversal in Doubly Linked List
Searching in Doubly Linked List
Finding Length of Doubly Linked List
Insertion in Doubly Linked List:
Insertion at the beginning of Doubly Linked List
Insertion at the end of the Doubly Linked List
Insertion at a specific position in Doubly Linked List
Deletion in Doubly Linked List:
Deletion of a node at the beginning of Doubly Linked List
Deletion of a node at the end of Doubly Linked List
Deletion of a node at a specific position in Doubly Linked List

Let's go through each of the operations mentioned above, one by one.

Traversal in Doubly Linked List


To Traverse the doubly list, we can use the following steps:

a. Forward Traversal:

Initialize a pointer to the head of the linked list.


While the pointer is not null:
Visit the data at the current node.
Move the pointer to the next node.

b. Backward Traversal:

Initialize a pointer to the tail of the linked list.


While the pointer is not null:
Visit the data at the current node.
Move the pointer to the previous node.

Below are the implementation of the above approach:

C++ C Java Python C# JavaScript


1
#include <iostream>

2
using namespace std;

4
// Define the Node structure

5
struct Node {

6
int data;

7
Node* next;

8
Node* prev;

10
// Constructor to initialize Node with data

11
Node(int data) : data(data), next(nullptr),

12
prev(nullptr) {}

13
};

14

15
// Function to traverse the doubly linked list

16
// in forward direction

17
void forwardTraversal(Node* head) {

18

19
// Start traversal from the head of the list

20
Node* curr = head;
21

22
// Continue until current node is not null

23
// (end of list)

24
while (curr != nullptr) {

25

26
// Output data of the current node

27
cout << curr->data << " ";

28

29
// Move to the next node

30
curr = curr >next;

Output

Forward Traversal:
1 2 3
Backward Traversal:
3 2 1

Finding Length of Doubly Linked List


To find the length of doubly list, we can use the following steps:

Start at the head of the list.


Traverse through the list, counting each node visited.
Return the total count of nodes as the length of the list.

Below are the implementation of the above approach:

C++ C Java Python C# JavaScript

1
#include <iostream>

2
3
using namespace std;

5
// Node structure for doubly linked list

6
struct Node {

7
int data;

8
Node * prev;

9
Node * next;

10

11
Node(int val) {

12
data = val;

13
prev = next = nullptr;

14
}

15
};

16

Output

Length of the doubly linked list: 3

Insertion at the Beginning in Doubly Linked List


Insertion at the Beginning in Doubly Linked List

To insert a new node at the beginning of the doubly list, we can use the following steps:

Create a new node, say new_node with the given data and set its previous pointer to null, new_node->prev
= NULL.
Set the next pointer of new_node to current head, new_node->next = head.
If the linked list is not empty, update the previous pointer of the current head to new_node, head->prev =
new_node.
Return new_node as the head of the updated linked list.
Below are the implementation of the above approach:

C++ C Java Python C# JavaScript

1
// C++ Program to insert a new node at the

2
// beginning of doubly linked list

4
#include <iostream>

5
using namespace std;

7
// Node structure for the doubly linked list

8
struct Node {

9
int data;

10
Node* prev;

11
Node* next;

12

13
Node(int d) {

14
data = d;

15
prev = next = NULL;

16
}

17
};

18
19
// Insert a node at the beginning

20
Node* insertBegin(Node* head, int data) {

21

22
// Create a new node

23
Node* new_node = new Node(data);

24

25
// Make next of it as head

26
new_node->next = head;

27

Output

Original Linked List: 2 3 4


After inserting Node 1 at the front: 1 2 3 4

Insertion at the End of Doubly Linked List


Insertion at the End in the Doubly Linked List

To insert a new node at the end of the doubly linked list, we can use the following steps:

Allocate memory for a new node and assign the provided value to its data field.
Initialize the next pointer of the new node to nullptr.
If the list is empty:
Set the previous pointer of the new node to nullptr.
Update the head pointer to point to the new node.
If the list is not empty:
Traverse the list starting from the head to reach the last node.
Set the next pointer of the last node to point to the new node.
Set the previous pointer of the new node to point to the last node.

Below are the implementation of the above approach:

C++ C Java Python C# JavaScript


1
// C++ Program to insert a node at the end of

2
//doubly linked list

4
#include <bits/stdc++.h>

5
using namespace std;

7
struct Node {

8
int data;

9
Node *next, *prev;

10

11
Node(int new_data) {

12
data = new_data;

13
next = prev = nullptr;

14
}

15
};

16

17
// Function to insert a new node at the end of

18
//doubly linked list

19
Node *insertEnd(Node *head, int new_data) {

20
21
// Create a new node

22
Node *new_node = new Node(new_data);

23

24
// If the linked list is empty, set the new

25
//node as the head of linked list

26
if (head == NULL) {

27
head = new_node;

28
}

29
else {

30
Node *curr = head;

Output

Original Linked List: 1 2 3


Inserting Node with data 4 at the end: 1 2 3 4

Insertion at a Specific Position in Doubly Linked List


To insert a node at a specific Position in doubly linked list, we can use the following steps:
Insertion at a Specific Position in Doubly Linked List

To insert a new node at a specific position,

If position = 1, create a new node and make it the head of the linked list and return it.
Otherwise, traverse the list to reach the node at position – 1, say curr.
If the position is valid, create a new node with given data, say new_node.
Update the next pointer of new node to the next of current node and prev pointer of new node to current
node, new_node->next = curr->next and new_node->prev = curr.
Similarly, update next pointer of current node to the new node, curr->next = new_node.
If the new node is not the last node, update prev pointer of new node’s next to the new node, new_node->next-
>prev = new_node.

Below is the implementation of the above approach:

C++ C Java Python C# JavaScript


1
// C++ Program to insert a node at a given position

3
#include <bits/stdc++.h>

4
using namespace std;

6
struct Node {

7
int data;

8
Node *next, *prev;

10
Node(int new_data) {

11
data = new_data;

12
next = prev = nullptr;

13
}

14
};

15

16
// Function to insert a new node at a given position

17
Node *insertAtPosition(Node *head, int pos, int new_data) {

18

19
// Create a new node

20
Node *new_node = new Node(new_data);
21

22
// Insertion at the beginning

23
if (pos == 1) {

24
new_node->next = head;

25

26
// If the linked list is not empty, set the prev

27
//of head to new node

28
if (head != NULL)

29
head->prev = new_node;

30

31
// Set the new node as the head of linked list

32
head = new_node;

33
return head;

34
}

35

36
Node *curr = head;

37
// Traverse the list to find the node before the

38
// insertion point

39
for (int i = 1; i < pos - 1 && curr != NULL; ++i) {

Output
Original Linked List: 1 2 4
Inserting Node with data 3 at position 3: 1 2 3 4

Deletion at the Beginning of Doubly Linked List


Deletion at the Beginning of Doubly Linked List

To delete a node at the beginning in doubly linked list, we can use the following steps:

Check if the list is empty, there is nothing to delete. Return.


Store the head pointer in a variable, say temp.
Update the head of linked list to the node next to the current head, head = head->next.
If the new head is not NULL, update the previous pointer of new head to NULL, head->prev = NULL.

Below is the implementation of the above approach:

C++ C Java Python C# JavaScript

1
// C++ Program to delete a node from the

2
// beginning of Doubly Linked List

4
#include <bits/stdc++.h>

5
using namespace std;

7
struct Node{

8
int data;

9
Node *prev;

10
Node *next;

11
Node(int d) {

12
data = d;

13
prev = next = nullptr;
14
}

15
};

16

17
// Deletes the first node (head) of the list

18
// and returns the second node as new head

19
Node *delHead(Node *head) {

20

21
// If empty, return

22
if (head == nullptr)

23
return nullptr;

24

25
// Store in temp for deletion later

26
Node *temp = head;

Output

Original Linked List: 1 2 3


After Deletion at the beginning: 2 3

Deletion at the End of Doubly Linked List


Deletion at the End in Doubly Linked List

To delete a node at the end in doubly linked list, we can use the following steps:

Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
If the list is not empty, then move to the last node of the doubly linked list, say curr.
Update the second-to-last node's next pointer to NULL, curr->prev->next = NULL.
Free the memory allocated for the node that was deleted.

Below is the implementation of the above approach:

C++ C Java Python C# JavaScript


1
// C++ Program to delete a node from the end of

2
//Doubly Linked List

4
#include <bits/stdc++.h>

5
using namespace std;

7
struct Node {

8
int data;

9
Node *prev;

10
Node *next;

11
Node(int d) {

12
data = d;

13
prev = NULL;

14
next = NULL;

15
}

16
};

17

18
// Function to delete the last node of the doubly

19
// linked list

20
Node *delLast(Node *head) {
21

22
// Corner cases

23
if (head == NULL)

24
return NULL;

25
if (head->next == NULL) {

26
delete head;

27
return NULL;

28
}

29

Output

Original Linked List: 1 2 3


After Deletion at the end: 1 2

Deletion at a Specific Position in Doubly Linked List


Deletion at a Specific Position in Doubly Linked List

To delete a node at a specific position in doubly linked list, we can use the following steps:

Traverse to the node at the specified position, say curr.


If the position is valid, adjust the pointers to skip the node to be deleted.
If curr is not the head of the linked list, update the next pointer of the node before curr to point to the
node after curr, curr->prev->next = curr-next.
If curr is not the last node of the linked list, update the previous pointer of the node after curr to the
node before curr, curr->next->prev = curr->prev.
Free the memory allocated for the deleted node.

Below is the implementation of the above approach:

C++ C Java Python C# JavaScript

1
// C++ Program to delete node at a specific position
2
// in Doubly Linked List

4
#include <iostream>

6
using namespace std;

8
struct Node {

9
int data;

10
Node * prev;

11
Node * next;

12
Node(int d) {

13
data = d;

14
prev = next = NULL;

15
}

16
};

17

18
// Function to delete a node at a specific position

19
// in the doubly linked list

20
Node * delPos(Node * head, int pos) {

21
22
// If the list is empty

23
if (!head)

24
return head;

25

26
Node * curr = head;

27

28
// Traverse to the node at the given position

29
for (int i = 1; curr && i < pos; ++i) {

30
curr = curr -> next;

31
}

32

You might also like