Skip to main content

construct a linked list from 2d matrix in java

Java program for construct a linked list from 2d matrix. Here problem description and other solutions.

// Java program for
// Construct a linked list from 2D matrix
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode down;
    public LinkNode(int data)
    {
        this.data = data;
        this.next = null;
        this.down = null;
    }
}
public class DoublyLinkedList
{
    public LinkNode head;
    public DoublyLinkedList()
    {
        // Set inital value
        this.head = null;
    }
    public void display()
    {
        if (this.head == null)
        {
            System.out.print("Empty linked list");
        }
        else
        {
            LinkNode front = this.head;
            LinkNode right = null;
            while (front != null)
            {
                right = front;
                while (right != null)
                {
                    System.out.print(right.data + "  ");
                    right = right.next;
                }
                System.out.print("\n");
                // Visit to down node
                front = front.down;
            }
        }
    }
    public void insertData(int[][] matrix, int rows, int cols)
    {
        // Some auxiliary variables
        LinkNode levelHead = null, root = null, perv = null;
        int i = 0;
        int j = 0;
        // Add first row elements into result list 
        while (i < cols)
        {
            if (this.head == null)
            {
                // Add first node
                this.head = new LinkNode(matrix[0][i]);
                levelHead = this.head;
            }
            else
            {
                // next head
                levelHead.next = new LinkNode(matrix[0][i]);
                levelHead = levelHead.next;
            }
            i++;
        }
        // Get first element    
        levelHead = this.head;
        i = 1;
        // Add all the bottom element of each column
        while (i < rows)
        {
            root = null;
            perv = root;
            while (j < cols)
            {
                levelHead.down = new LinkNode(matrix[i][j]);
                if (root == null)
                {
                    root = levelHead.down;
                    perv = root;
                }
                else
                {
                    perv.next = levelHead.down;
                    perv = levelHead.down;
                }
                levelHead = levelHead.next;
                j++;
            }
            levelHead = root;
            // reset col
            j = 0;
            // change row
            i++;
        }
    }
    public static void main(String[] args)
    {
        DoublyLinkedList dll = new DoublyLinkedList();
        // Create 2D metrix
        int[][] matrix = {
            {
                1 , 6 , 9 , 2 , -9
            },
            {
                2 , 5 , -5 , 7 , 1
            },
            {
                3 , 4 , 1 , 8 , 2
            }
        };
        int rows = matrix.length;
        int cols = matrix[0].length;
        dll.insertData(matrix, rows, cols);
        dll.display();
    }
}
Construct linked list from 2d matrix in java

Output

1  6  9  2  -9
2  5  -5  7  1
3  4  1  8  2




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment