Construct a linked list from 2D matrix

Construct linked list from of 2D matrix

Here given code implementation process.

//C Program
//Construct a linked list from 2D matrix
#include <stdio.h>
#include <stdlib.h> //for malloc function

#define ROW 3
#define COLS 5
//create structure
struct Node{
  int data; 
  struct Node*next;
  struct Node*down;
};



//insert Node element of end of linked list
struct Node* insert(int value){

  //Create a dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL){
    printf("Memory overflow\n");
    exit(0);
  }else{
    //set data value
    node->data=value;
    node->next=NULL;
    node->down=NULL;
  }

  return node;
}
//display element of Node
void display(struct Node*head){
  
  if(head==NULL){
    printf("Empty linked list");
  }
  else{
    struct Node*right_side=NULL;
    //Traverse doubly linked list from front to rear
    while(head!=NULL)
    {
      
      right_side=head;

      while(right_side!=NULL)
      {
        printf("%3d",right_side->data );
        right_side=right_side->next;
      }

      printf("\n");
      head=head->down;
    }
  }

}

struct Node* insertData(int (*matrix)[COLS],int row,int cols)
{
  struct Node*head=NULL,*levelHead=NULL,*root=NULL ,*perv=NULL;
  //Add first rows in linked List
  for (int i = 0; i < cols; ++i)
  {
    if(head==NULL)
    {
      head=insert(matrix[0][i]);
      levelHead=head;
    }
    else{
      levelHead->next=insert(matrix[0][i]);
      levelHead=levelHead->next;
    }
  }
  
  levelHead=head;

  for (int i = 1; i < row; ++i)
  {
    
    root=NULL;
    perv=root;
    for (int j = 0; j < cols; ++j)
    {
      //Add element into next nows
      levelHead->down=insert(matrix[i][j]);

      if(root==NULL)
      {
        root=levelHead->down;
        perv=root;
      }
      else{

        perv->next=levelHead->down;
        perv=levelHead->down;
      }

      levelHead=levelHead->next;
    }
    levelHead=root;
    
  }
  
  return head;
}
int main(){
 

  //Create 2D metrix
  int matrix[ROW][COLS]={
    {1,6,9,2,-9},
    {2,5,-5,7,1},
    {3,4,-1,8,2}
  };
  //set node pointer value
  struct Node*head=insertData(matrix,ROW,COLS);

  display(head);

 
  return 0;
}

Output

  1  6  9  2 -9
  2  5 -5  7  1
  3  4 -1  8  2
/*
 C++ Program
 Construct a linked list from 2D matrix
*/

#include<iostream>

using namespace std;

#define COLS 5
class Node {
  public:
    int data;
  Node *next;
  Node *down;
  Node(int value) {
    this->data = value;
    this->next = NULL;
    this->down = NULL;
  }
};
class LinkedList {
  public:
    Node *head;
  Node *tail;
  LinkedList() {
    this->head = NULL;
    this->tail = NULL;
  }
  void display() {
    if (this->head == NULL) {
      cout << "Empty linked list";
    } else {
      Node *front = this->head;
      Node *right_side = NULL;
      while (front != NULL) {
        right_side = front;
        while (right_side != NULL) {
          cout << right_side->data << "  ";
          right_side = right_side->next;
        }
        cout << "\n";
        front = front->down;
      }
    }
  }
  void insertData(int matrix[][COLS], int row, int cols) {
    Node *levelHead = NULL, *root = NULL, *perv = NULL;
    int i = 0;
    int j = 0;
    while (i < cols) {
      if (this->head == NULL) {
        this->head = new Node(matrix[0][i]);
        levelHead = this->head;
      } else {
        levelHead->next = new Node(matrix[0][i]);
        levelHead = levelHead->next;
      }
      i++;
    }
    levelHead = this->head;
    i = 1;
    while (i < row) {
      root = NULL;
      perv = root;
      j = 0;
      while (j < cols) {
        levelHead->down = new Node(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;
      i++;
    }
  }
};
int main() {
  LinkedList obj ;
  int matrix[][COLS] = {
    {
      1,
      6,
      9,
      2,
      -9
    },
    {
      2,
      5,
      -5,
      7,
      1
    },
    {
      3,
      4,
      1,
      8,
      2
    }
  };
  int row = sizeof(matrix) / sizeof(matrix[0]);

  obj.insertData(matrix, row, COLS);

  obj.display();

  return 0;
}

Output

  1  6  9  2 -9
  2  5 -5  7  1
  3  4 -1  8  2
/*
  Java Program
  Construct a linked list from 2D matrix
*/
class Node {

  public int data;

  public Node next;

  public Node down;

  public Node(int value) {
    //Setup initial values of linked list node
    this.data = value;
    this.next = null;
    this.down = null;
  }
}
class LinkedList {

  public Node head;
  public Node tail;

  public LinkedList() {
    head = null;
    tail = null;
  }
  public void display() {

    if (head == null) {

      System.out.print("Empty linked list");
    } else {
      Node front = head;
      Node right_side = null;

      while (front != null) {

        right_side = front;

        while (right_side != null) {

          System.out.print(right_side.data+"  ");
          right_side = right_side.next;
        }

        System.out.print("\n");
        front = front.down;
      }
    }
  }
 public void insertData(int[][] matrix, int row, int cols) {
    Node levelHead = null, root = null, perv = null;
    int i=0;
    int j=0;
    while (i < cols) {

      if (head == null) {
        head = new Node(matrix[0][i]);
        levelHead = head;
      } else {
        levelHead.next = new Node(matrix[0][i]);
        levelHead = levelHead.next;
      }
      i++;
    }
    levelHead = head;
    i=1;
    while ( i < row) {
      root = null;
      perv = root;
      j=0;
      while(j < cols) {
        levelHead.down = new Node(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;

      i++;
    }
  }




  public static void main(String[] args) {

    LinkedList obj = new LinkedList();
    //Create 2D metrix
    int[][] matrix = {
      {
        1, 6, 9, 2,  -9
      },
      {
        2, 5, -5, 7, 1
      },
      {
        3,  4, 1, 8, 2
      }
    };
    int row = matrix.length;

    int cols = matrix[0].length;

    obj.insertData(matrix, row, cols);
    
    obj.display();


  }
}

Output

  1  6  9  2 -9
  2  5 -5  7  1
  3  4 -1  8  2
internal node presentation of 2D linked list
/*
  C# Program
  Construct a linked list from 2D matrix
*/
using System;
public class Node {

	public int data;

	public Node next;

	public Node down;

	public Node(int value) {
		//Setup initial values of linked list node
		this.data = value;
		this.next = null;
		this.down = null;
	}
}
class LinkedList {

	public Node head;
	public Node tail;

	public LinkedList() {
		head = null;
		tail = null;
	}
	public void display() {

		if (head == null) {

			Console.Write("Empty linked list");
		} else {
			Node front = head;
			Node right_side = null;

			while (front != null) {

				right_side = front;

				while (right_side != null) {

					Console.Write(right_side.data+"  ");
					right_side = right_side.next;
				}

				Console.Write("\n");
				front = front.down;
			}
		}
	}
	public void insertData(int[,] matrix, int row, int cols) {
		Node levelHead = null, root = null, perv = null;
		int i=0;
		int j=0;
		while (i < cols) {

			if (head == null) {
				head = new Node(matrix[0,i]);
				levelHead = head;
			} else {
				levelHead.next = new Node(matrix[0,i]);
				levelHead = levelHead.next;
			}
			i++;
		}
		levelHead = head;
		i=1;
		while ( i < row) {
			root = null;
			perv = root;
			j=0;
			while(j < cols) {
				levelHead.down = new Node(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;

			i++;
		}
	}




	public static void Main(String[] args) {

		LinkedList obj = new LinkedList();
		//Create 2D metrix
		int[,] matrix = {
			{
				1, 6, 9, 2,  -9
			},
			{
				2, 5, -5, 7, 1
			},
			{
				3,  4, 1, 8, 2
			}
		};
		int row = matrix.GetLength(0);

		int cols = matrix.GetLength(1);

		obj.insertData(matrix, row, cols);

		obj.display();


	}
}

Output

  1  6  9  2 -9
  2  5 -5  7  1
  3  4 -1  8  2
# Python 3 Program
# Construct a linked list from 2D matrix

class Node :

  def __init__(self, value) :
    self.data = value
    self.next = None
    self.down = None
  

class LinkedList :

  def __init__(self) :
    self.head = None
    self.tail = None
  
  def display(self) :
    if (self.head == None) :
      print("Empty linked list")
    else :
      front = self.head
      right_side = None
      while (front != None) :
        right_side = front
        while (right_side != None) :
          print(right_side.data ,end="  ")
          right_side = right_side.next
        
        print(end="\n")
        front = front.down
      
    
  
  def insertData(self, matrix, row, cols) :
    levelHead = None
    root = None
    perv = None
    i = 0
    j = 0
    while (i < cols) :
      if (self.head == None) :
        self.head = Node(matrix[0][i])
        levelHead = self.head
      else :
        levelHead.next = Node(matrix[0][i])
        levelHead = levelHead.next
      
      i += 1
    
    levelHead = self.head
    i = 1
    while (i < row) :
      root = None
      perv = root
      j = 0
      while (j < cols) :
        levelHead.down = Node(matrix[i][j])
        if (root == None) :
          root = levelHead.down
          perv = root
        else :
          perv.next = levelHead.down
          perv = levelHead.down
        
        levelHead = levelHead.next
        j += 1
      
      levelHead = root
      i += 1
    
  

def main() :
  obj = LinkedList()
  matrix = [
    [1, 6, 9, 2, -9],
    [2, 5, -5, 7, 1],
    [3, 4, 1, 8, 2]
  ]
  row = len(matrix)
  cols = len(matrix[0])
  obj.insertData(matrix, row, cols)
  obj.display()

if __name__ == "__main__":
  main()

Output

1  6  9  2  -9  
2  5  -5  7  1  
3  4  1  8  2  
# Ruby Program
# Construct a linked list from 2D matrix

class Node 
	attr_reader :data, :next, :down
	attr_accessor :data, :next, :down
	def initialize(value) 
		self.data = value
		self.next = nil
		self.down = nil
	end
end

class LinkedList 
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		@head = nil
		@tail = nil
	end
	def display() 
		if (@head == nil) 
			print("Empty linked list")
		else 
			front = @head
			right_side = nil
			while (front != nil) 
				right_side = front
				while (right_side != nil) 
					print(right_side.data ,"  ")
					right_side = right_side.next
				end
				print("\n")
				front = front.down
			end
		end
	end
	def insertData(matrix, row, cols) 
		levelHead = nil
		root = nil
		perv = nil
		i = 0
		j = 0
		while (i < cols) 
			if (@head == nil) 
				@head = Node.new(matrix[0][i])
				levelHead = @head
			else 
				levelHead.next = Node.new(matrix[0][i])
				levelHead = levelHead.next
			end
			i += 1
		end
		levelHead = @head
		i = 1
		while (i < row) 
			root = nil
			perv = root
			j = 0
			while (j < cols) 
				levelHead.down = Node.new(matrix[i][j])
				if (root == nil) 
					root = levelHead.down
					perv = root
				else 
					perv.next = levelHead.down
					perv = levelHead.down
				end
				levelHead = levelHead.next
				j += 1
			end
			levelHead = root
			i += 1
		end
	end
end
def main() 
	obj = LinkedList.new()
	matrix = [
		[1, 6, 9, 2, -9],
		[2, 5, -5, 7, 1],
		[3, 4, 1, 8, 2]
	]
	row = matrix.length
	cols = matrix[0].length
	obj.insertData(matrix, row, cols)
	obj.display()
end


main()

Output

1  6  9  2  -9  
2  5  -5  7  1  
3  4  1  8  2  
<?php

/*
 Php Program
 Construct a linked list from 2D matrix
*/

class Node {
  public $data;
  public $next;
  public $down;

  function __construct($value) {
    $this->data = $value;
    $this->next = null;
    $this->down = null;
  }
}
class LinkedList {
  public $head;
  public $tail;

  function __construct() {
    $this->head = null;
    $this->tail = null;
  }
  public  function display() {
    if ($this->head == null) {
      echo("Empty linked list");
    } else {
      $front = $this->head;
      $right_side = null;
      while ($front != null) {
        $right_side = $front;
        while ($right_side != null) {
          echo($right_side->data ."  ");
          $right_side = $right_side->next;
        }
        echo("\n");
        $front = $front->down;
      }
    }
  }
  public  function insertData($matrix, $row, $cols) {
    $levelHead = null;
    $root = null;
    $perv = null;
    $i = 0;
    $j = 0;
    while ($i < $cols) {
      if ($this->head == null) {
        $this->head = new Node($matrix[0][$i]);
        $levelHead = $this->head;
      } else {
        $levelHead->next = new Node($matrix[0][$i]);
        $levelHead = $levelHead->next;
      }
      $i++;
    }
    $levelHead = $this->head;
    $i = 1;
    while ($i < $row) {
      $root = null;
      $perv = $root;
      $j = 0;
      while ($j < $cols) {
        $levelHead->down = new Node($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;
      $i++;
    }
  }
}

function main() {
  $obj = new LinkedList();
  $matrix = array(array(1, 6, 9, 2, -9), array(2, 5, -5, 7, 1), array(3, 4, 1, 8, 2)
  );
  $row = count($matrix);
  $cols = count($matrix[0]);
  $obj->insertData($matrix, $row, $cols);
  $obj->display();
}
main();

Output

1  6  9  2  -9  
2  5  -5  7  1  
3  4  1  8  2  
/*
 Node Js Program
 Construct a linked list from 2D matrix
*/

class Node {
	
	constructor(value) {
		this.data = value;
		this.next = null;
		this.down = null;
	}
}
class LinkedList {

	constructor() {
		this.head = null;
		this.tail = null;
	}
	display() {
		if (this.head == null) {
			process.stdout.write("Empty linked list");
		} else {
			var front = this.head;
			var right_side = null;
			while (front != null) {
				right_side = front;
				while (right_side != null) {
					process.stdout.write(right_side.data + "  ");
					right_side = right_side.next;
				}
				process.stdout.write("\n");
				front = front.down;
			}
		}
	}
	insertData(matrix, row, cols) {
		var levelHead = null;
		var root = null;
		var perv = null;
		var i = 0;
		var j = 0;
		while (i < cols) {
			if (this.head == null) {
				this.head = new Node(matrix[0][i]);
				levelHead = this.head;
			} else {
				levelHead.next = new Node(matrix[0][i]);
				levelHead = levelHead.next;
			}
			i++;
		}
		levelHead = this.head;
		i = 1;
		while (i < row) {
			root = null;
			perv = root;
			j = 0;
			while (j < cols) {
				levelHead.down = new Node(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;
			i++;
		}
	}
}

function main() {
	var obj = new LinkedList();
	var matrix = [
		[1, 6, 9, 2, -9],
		[2, 5, -5, 7, 1],
		[3, 4, 1, 8, 2]
	];
	var row = matrix.length;
	var cols = matrix[0].length;
	obj.insertData(matrix, row, cols);
	obj.display();
}

main();

Output

1  6  9  2  -9  
2  5  -5  7  1  
3  4  1  8  2  
/*
 Swift 4 Program
 Construct a linked list from 2D matrix
*/

class Node {
  var data: Int;
  var next: Node? ;
  var down: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.next = nil;
    self.down = nil;
  }
}
class LinkedList {
  var head: Node? ;
  var tail: Node? ;
  init() {
    self.head = nil;
    self.tail = nil;
  }
  func display() {
    if (self.head == nil) {
      print("Empty linked list");
    } else {
      var front: Node? = self.head;
      var right_side: Node? = nil;
      while (front != nil) {
        right_side = front;
        while (right_side != nil) {
          print(right_side!.data ,terminator:"  ");
          right_side = right_side!.next;
        }
        print(terminator:"\n");
        front = front!.down;
      }
    }
  }
  func insertData(_ matrix: [[Int]] , _ row : Int, _ cols: Int) {
    var levelHead: Node? = nil;
    var root: Node?  = nil;
    var perv: Node?  = nil;
    var i: Int = 0;
    var j: Int = 0;
    while (i < cols) {
      if (self.head == nil) {
        self.head = Node(matrix[0][i]);
        levelHead = self.head;
      } else {
        levelHead!.next = Node(matrix[0][i]);
        levelHead = levelHead!.next;
      }
      i += 1;
    }
    levelHead = self.head;
    i = 1;
    while (i < row) {
      root = nil;
      perv = root;
      j = 0;
      while (j < cols) {
        levelHead!.down = Node(matrix[i][j]);
        if (root == nil) {
          root = levelHead!.down;
          perv = root;
        } else {
          perv!.next = levelHead!.down;
          perv = levelHead!.down;
        }
        levelHead = levelHead!.next;
        j += 1;
      }
      levelHead = root;
      i += 1;
    }
  }
}
func main() {
  let obj: LinkedList = LinkedList();
  let matrix: [[Int]] = [
    [1, 6, 9, 2, -9],
    [2, 5, -5, 7, 1],
    [3, 4, 1, 8, 2]
  ];
  let row: Int = matrix.count;
  let cols: Int = matrix[0] .count;
  obj.insertData(matrix, row, cols);
  obj.display();
}
main();

Output

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

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







© 2021, kalkicode.com, All rights reserved