Doubly Linked List Tutorial

Continuing with our tutorials series on data structure, let’s discuss Doubly Linked List in this article. Earlier in this series of article we discussed Singly Linked List, it’s definition and various operations which can be performed on it along with code snippets. Let’s now dig into Doubly Linked List and understand what all operations are supported by it.

[linkad]

Doubly Linked List

Definition:

A doubly linked list, in computer science, is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes’ previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list.

Noticeable differences between singly and doubly linked lists :

Although doubly linked lists require more space per node as compared to singly linked list, and their elementary operations are more expensive; but they are often easier to manipulate because they allow sequential access to the list in both directions.

In doubly linked list insertion or deletion of any node, whose address is given, can be carried out in constant number of operations and on the other hand in singly linked list the same operation would require the address of the predecessor’s address which of course is not a problem with doubly linked list as we can move in both directions.

The below figure show how a doubly linked list looks like :

[linkad]

Let’s now directly jump over to various operations which can be performed over doubly linked list:

Add a node in a list at beginning or at end or in between.

Delete a node from list at specific location.

Reverse a list.

Count nodes present in the list.

Print the list to see all the nodes present in the list.

First let’s have a look at the node definition :

struct node{
int data;
struct node *prev;
struct node *next;
};

It’s evident from the above node definition that a node in a doubly linked list have 2 links (nextand prev) and one data value (data).

Add a node in a list. Insertion of a node in a linked list can be done at three places, viz. at start, in between at a specified location or at end.

Inserting a node at the start of list :Algorithm :

Update the nextpointer of the new node to the head node and make prevpointer of the new node as NULL

Now update head node’s prevpointer to point to new node and make new node as head node.

Inserting a node in between of list :Algorithm :

Traverse the list to the position where the new node is to be inserted.Let’s call this node as Position Node (we have to insert new node just next to it).

Make the nextpointer of new pointer to point to next node of position node. Also make the prevpoint of new node to point to position node.

Now point position node’s nextpointer to new node and prevnode of next node of position node to point to new node.

Inserting a node at the end of the list :Algorithm :

Traverse the list to end. Let’s call the current last node of list as Last node.

Make nextpointer of New node to point to NULL and prevpointer of new node to point to Last node.

Update next pointer of Last node to point to new Node.

Thus we see how easily we can add a node in a list. Let us now write code for all the three cases.Please note here that we are passing double pointer in the function as we may require to change the head pointer.

This is one of the favorite question which interviewer is bound to ask while interviewing a candidate on data structures. There are many ways to accomplish this but we will explain the way we implemented in our example.

Algorithm :

First swap prevand nextpointer of all nodes.

Point head to the tail node as tail node is now our new head node.

This method is pretty straight forward and fast also. Below is the code snippet of reversing a Doubly Linked List.

Counting nodes in list/ Printing content of list. This one is the easiest operation on Doubly Linked List. All what you have to do is to traverse the list and while traversing you need to keep on increment counter (while counting nodes in list)/ you need to print the data of each node (while printing the nodes). The code snippets are as below :

About the Author

Pritam+ - Co-Founder of IdleBrains, is software Engineer by profession with expertise in .NET technologies and data structures. An avid reader and writer, loves to keep himself well versed with new technologies. When not working can be found on Badminton court or chatting with friends. Among other hobbies, loves to listen old hindi numbers of Kishore Kumar and Mukesh.

good mornign sir. I have seen your interesting tutorial about the double linked list. Functions are ok but i don’t know how to run this program ,pratically into the main i try to do something but it doesn’t works .Your link “Please find the complete code snippet here” doesn’t work , no page opening when i click on it . Could you please tell me what to do ? Thanks a lot Regards Walter