2D matrix using linked list in C++
IntroductionIn 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 DSA. Show To construct a doubly-linked list from a 2D matrix, we use recursion in our solution. Readers who want to brush up their concepts on recursion can do so by clickingRecursion. Problem StatementYou 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. ExampleINPUT: 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 Approach and ExplanationDoubly-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- (source:Creately) 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:
Recommended: Try to write the code first before moving to the solution code provided in the article. C++ implementation#include usingnamespacestd; #define ROW3 #define COL3 structDubLL { intdata; DubLL *up; DubLL *down; DubLL *left; DubLL *right; DubLL(intval) :data(val) ,up(NULL),down(NULL),left(NULL),right(NULL){} }; DubLL*createDLL(intinput[][COL],intstart,intend,DubLL*last){ if(start >=ROW ||end >=COL){ returnNULL; } DubLL *curr =newDubLL(input[start][end]); if(end ==0){ curr->left =NULL; }else{ curr->left =last; } if(start ==0){ curr->up =NULL; }else{ curr->up =last; } curr->right =createDLL(input,start,end+1,curr); curr->down =createDLL(input,start+1,end,curr); returncurr; } voidprintLL(DubLL*head){ DubLL *rightpt, *downpt =head; while(downpt){ rightpt =downpt; while (rightpt) { cout<< (rightpt->data)<<" <-> "; rightpt =rightpt->right; } cout<<"NULL"; cout<
ComplexitiesTime ComplexityIn the given implementation, we traverse the whole 2D matrix to create our Doubly Linked List. Thus, the time complexity is: T(n) = O(n*m) where n is the number of rows and m is the number of columns. Space ComplexityIn the given implementation, no extra space is used. Thus, Space complexity = O(1) Frequently Asked Questions
Key TakeawaysTo summarize the article, we studied how to construct a doubly-linked list from a 2D matrix. We saw the problem statement, an example along, our approach and explanation, and finally the solution code. We also covered the time and space complexities along with some FAQs. Confident about Doubly-Linked List construction? Practice this questionConvert A Given Binary Tree To Doubly Linked List on ourCodeStudio. Want to ace the coding rounds of big tech companies? Try ourAttempt Unlimited Online Mock Test Series to start your preparation. Happy Coding! |