'Coding has over 700 languages', '67% of programming jobs arenâ€™t in the
technology industry', 'Coding is behind almost everything that is powered
by electricity'

Data structures & algorithms (Beginner to Intermediate)

Free guided path13 chapters99+ problems

Earn badges and level up

Introduction

In this article, we will study how to construct a Doubly-Linked list from a 2D matrix. We do this because, in a doubly-linked list, traversal is possible in both ways, that is, forward and backward. By reading this article, the reader will be able to clear their concepts about doubly-linked lists, which will help them have a good foundation in Data Structures and Algorithms.

To construct a doubly-linked list from a 2D matrix (see 2-Dimensional Array), we use recursion in our solution. Readers who want to brush up their concepts on recursion can do so by clicking Recursion (Also see Data Structures).

Problem Statement

You are given a 2D matrix of size n*m. Construct a doubly linked list from that 2D matrix. Consider each cell of the matrix as a node. Each node must have four pointers, namely- up, down, left, and right, connecting them.

Example

INPUT:

input[3][3] = { {1, 2, 3},

{4, 5, 6},

{7, 8, 9} }

OUTPUT:

1 <-> 2 <-> 3 <-> NULL

^ ^ ^

| | |

v v v

4 <-> 5 <-> 6 <-> NULL

^ ^ ^

| | |

v v v

7 <-> 8 <-> 9 <-> NULL

^ ^ ^

| | |

v v v

NULL NULL NULL

Recommended: Before moving on to the solution, check out a similar Problem.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Approach and Explanation

Doubly-Linked lists are special linked lists in which traversal is possible in both directions, that is, forward and backward. In our case, we have to maintain four pointers that will connect each node.

In the case of the boundary elements, we have two pointers to link, and in the case of inner elements, we have four pointers to link. So our doubly-linked list will look like this-

The double-headed arrows indicate that traversal is possible in both directions. So, to make the required doubly-linked list, we do the following steps:

Create a structure or constructor with five pointer members- data, up, down, left, and right. In the constructor pass one function argument. Set the value for data with the incoming value and initialize all the pointers to NULL.

Create a method that will create our doubly linked list. Let us name this function createDLL(). This function will take four arguments:

int input[][]: the 2D matrix inputted by the user.

int posi, int posj: two integer values that will maintain the current cell of the 2D matrix on which we are working.

DubLL *last: pointer of struct type that will point to the last added node.

Inside this function, recursively take one cell of the 2D matrix and convert it into a node of the linked list and create the links.

To create the node, we check if the values of posi or posj are pointing to the end of the matrix or not. If yes, then we return NULL; else, we create a new node and pass the value of the 2D matrix to the node of our doubly-linked list.

Once the node is created, we check if it is a boundary element or not.

If the matrix cell lies on the 0th row, i.e., its index is (0,j), it will not have any node above it. So, the value of the up pointer is set to NULL; else, it points to the last node.

If the matrix cell lies on the 0th column, i.e., its index is (i,0), it will not have any node on the left side. So, the value of the left pointer is set to NULL; else, it points to the last node.

For the right-side nodes, call the function createDLL recursively, increase the value of posj by one and pass it along with all the other values.

For the downward node, call the function createDLL recursively, increase the value of posi by one and pass it along with all the other values.

We also have a print function in the solution code that will help us see our doubly-linked list.

Recommended: Try to write the code first before moving to the solution code provided in the article.

What are the other ways to implement the construction of a doubly-linked list from a 2D matrix?

Yes, we can iteratively create a doubly-linked list using two nested for loops. One will travel all the rows, and the other will travel all the columns.

What is the difference between a singly-linked list and a doubly-linked list?

In a singly-linked list, traversal is possible in one direction only. Once a node is passed, we cannot go to it unless we start our traversal from the head node again. In the case of a doubly-linked list, traversal is possible in both directions as we maintain two address pointer fields. Thus, if we pass a node once, we can traverse it again from any node.

Conclusion

To summarize the article, we studied how to construct a doubly-linked list from a 2D matrix. We saw the problem statement, an example, our approach and explanation, and finally the solution code. We also covered the time and space complexities along with some FAQs.