Multilevel Linked List Multilevel Linked List is a 2D data structure that comprises several linked lists and each node in a multilevel linked list has a next and child pointer. All the elements are linked using pointers.
 multilevel linked list
Representation: A multilevel linked list is represented by a pointer to the first node of the linked lists. Similar to the linked list, the first node is called the head. If the multilevel linked list is empty, then the value of head is NULL. Each node in a list consists of at least three parts: 1. Data. 2. Pointer to the next node. 3. Pointer to the child node.
Each node of a multilevel linked list is represented as:
class Node { public: int data; Node *next; Node *child; };
Below is the implementation of the multilevel linked list
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node* child;
};
Node* createList( int * arr, int n)
{
Node* head = NULL;
Node* tmp;
for ( int i = 0; i < n; i++) {
if (head == NULL) {
tmp = head = new Node();
}
else {
tmp->next = new Node();
tmp = tmp->next;
}
tmp->data = arr[i];
tmp->next = tmp->child = NULL;
}
return head;
}
void printMultiLevelList(Node* head)
{
while (head) {
if (head->child) {
printMultiLevelList(head->child);
}
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
int arr1[3] = { 1, 2, 3 };
int arr2[2] = { 5, 6 };
int arr3[1] = { 4 };
int arr4[3] = { 7, 8, 9 };
Node* head1 = createList(arr1, 3);
Node* head2 = createList(arr2, 2);
Node* head3 = createList(arr3, 1);
Node* head4 = createList(arr4, 3);
head1->child = head2;
head1->next->next->child = head3;
head2->next->child = head4;
Node* head = NULL;
head = head1;
printMultiLevelList(head);
return 0;
}
|
Java
public class GFG {
public static class Node {
public int data;
public Node next;
public Node child;
public Node( int data) {
this .data = data;
this .next = null ;
this .child = null ;
}
}
public static Node createList( int [] arr, int n) {
Node head = null ;
Node tmp = null ;
for ( int i = 0 ; i < n; i++) {
if (head == null ) {
tmp = head = new Node(arr[i]);
} else {
tmp.next = new Node(arr[i]);
tmp = tmp.next;
}
}
return head;
}
public static void printMultiLevelList(Node head) {
while (head != null ) {
if (head.child != null ) {
printMultiLevelList(head.child);
}
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args) {
int [] arr1 = { 1 , 2 , 3 };
int [] arr2 = { 5 , 6 };
int [] arr3 = { 4 };
int [] arr4 = { 7 , 8 , 9 };
Node head1 = createList(arr1, 3 );
Node head2 = createList(arr2, 2 );
Node head3 = createList(arr3, 1 );
Node head4 = createList(arr4, 3 );
head1.child = head2;
head1.next.next.child = head3;
head2.next.child = head4;
Node head = head1;
printMultiLevelList(head);
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0 ;
self . next = None ;
self .child = None ;
def createList(arr, n):
head = None ;
tmp = None ;
for i in range (n):
if (head = = None ):
tmp = head = Node();
else :
tmp. next = Node();
tmp = tmp. next ;
tmp.data = arr[i];
tmp. next = tmp.child = None ;
return head;
def printMultiLevelList(head):
while (head ! = None ):
if (head.child ! = None ):
printMultiLevelList(head.child);
print (head.data, end = " " );
head = head. next ;
if __name__ = = '__main__' :
arr1 = [ 1 , 2 , 3 ];
arr2 = [ 5 , 6 ] ;
arr3 = [ 4 ];
arr4 = [ 7 , 8 , 9 ] ;
head1 = createList(arr1, 3 );
head2 = createList(arr2, 2 );
head3 = createList(arr3, 1 );
head4 = createList(arr4, 3 );
head1.child = head2;
head1. next . next .child = head3;
head2. next .child = head4;
head = None ;
head = head1;
printMultiLevelList(head);
|
C#
using System;
public class GFG {
public class Node {
public int data;
public Node next;
public Node child;
};
public static Node createList( int []arr, int n)
{
Node head = null ;
Node tmp = null ;
for ( int i = 0; i < n; i++)
{
if (head == null ) {
tmp = head = new Node();
}
else {
tmp.next = new Node();
tmp = tmp.next;
}
tmp.data = arr[i];
tmp.next = tmp.child = null ;
}
return head;
}
public static void printMultiLevelList(Node head)
{
while (head != null ) {
if (head.child != null ) {
printMultiLevelList(head.child);
}
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String[] args)
{
int []arr1 = { 1, 2, 3 };
int []arr2 = { 5, 6 };
int []arr3 = { 4 };
int []arr4 = { 7, 8, 9 };
Node head1 = createList(arr1, 3);
Node head2 = createList(arr2, 2);
Node head3 = createList(arr3, 1);
Node head4 = createList(arr4, 3);
head1.child = head2;
head1.next.next.child = head3;
head2.next.child = head4;
Node head = null ;
head = head1;
printMultiLevelList(head);
}
}
|
Javascript
<script>
class Node {
constructor(){
this .data = 0;
this .next = null ;
this .child = null ;
}
}
function createList(arr , n)
{
var head = null ;
var tmp = null ;
for ( var i = 0; i < n; i++)
{
if (head == null ) {
tmp = head = new Node();
}
else {
tmp.next = new Node();
tmp = tmp.next;
}
tmp.data = arr[i];
tmp.next = tmp.child = null ;
}
return head;
}
function printMultiLevelList(head)
{
while (head != null ) {
if (head.child != null ) {
printMultiLevelList(head.child);
}
document.write(head.data + " " );
head = head.next;
}
}
var arr1 = [ 1, 2, 3 ];
var arr2 = [ 5, 6 ];
var arr3 = [ 4 ];
var arr4 = [ 7, 8, 9 ];
var head1 = createList(arr1, 3);
var head2 = createList(arr2, 2);
var head3 = createList(arr3, 1);
var head4 = createList(arr4, 3);
head1.child = head2;
head1.next.next.child = head3;
head2.next.child = head4;
var head = null ;
head = head1;
printMultiLevelList(head);
</script>
|
|