# construct a linked list from 2d matrix in c

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

``````// C Program for
// 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) {
} else {
struct Node * right_side = NULL;
//Traverse doubly linked list from front to rear
while (head != NULL) {
while (right_side != NULL) {
printf("%3d", right_side->data);
right_side = right_side->next;
}
printf("\n");
}
}
}
struct Node * insertData(int(*matrix)[COLS],
int row,
int cols) {
struct Node *head = NULL,
*root = NULL,
*perv = NULL;
// Add first rows in linked List
for (int i = 0; i < cols; ++i) {
if (head == NULL) {
} else {
}
}
for (int i = 1; i < row; ++i) {
root = NULL;
perv = root;
for (int j = 0; j < cols; ++j) {
// Add element into next nows
if (root == NULL) {
perv = root;
} else {
}
}
}
}
int main() {
// Create 2D metrix
int matrix[ROW][COLS] = {
{1,6,9,2,-9},
{2,5,-5,7,1},
{3,4,-1,8,2}
};

struct Node * head = insertData(matrix, ROW, COLS);
return 0;
}``````

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.