Introduction to Multi Linked List
Last Updated :
19 Dec, 2022
A multi-linked list is a special type of list that contains two or more logical key sequences. Before checking details about multi-linked list, see what is a linked list. A linked list is a data structure that is free from any size restriction until the heap memory is not full. We have seen different types of linked lists, such as Singly Linked List, Circular Linked List, and Doubly Linked List. Here we will see about multi-linked list.
In a multi-linked list, each node can have N number of pointers to other nodes. A multi-linked list is generally used to organize multiple orders of one set of elements.
Properties of Multi-Linked List:
The properties of a multi-linked list are mentioned below.
- It is an integrated list of related structures.
- All the nodes are integrated using links of pointers.
- Linked nodes are connected with related data.
- Nodes contain pointers from one structure to the other.
Structure of Multi-linked list:
The structure of a multi-linked list depends on the structure of a node. A single node generally contains two things:
- A list of pointers
- All the relevant data.
Shown below is the structure of a node that contains only one data and a list of pointers.
C++
typedef struct node {
int data;
vector<struct node*> pointers;
} Node;
// This code is contributed by akashish__
C
typedef struct node {
int data;
vector<struct node*> pointers;
} Node;
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args) {}
public static class Node {
int data;
Node[] children;
};
}
// This code is contributed by ishankhandelwal
Python
# Python code
class Node:
def _init_(self):
self.data = 0
self.pointers = []
# This code is contributed by ishankhandelwals.
C#
// C# code to create the structure of a node
using System;
class GFG
{
public static void Main() { }
public class Node
{
int data;
Node[] children;
};
}
// This code is contributed by Saurabh jaiswal
JavaScript
// JS code
class node{
constructor(){
this.data;
this.pointers=[];
}
}
// This code is contributed by ishankhandelwals.
Use cases of Multi-Linked Lists:
Some use cases of a multi-linked list are:
- Multiple orders of one set of elements
- Representation of a sparse matrix
- List of List
Multiple orders of one set of elements:
- A multi-linked list is a more general linked list with multiple links from nodes.
- For example, suppose the task is to maintain a list in multiple orders, age and name here, we can define a Node that has two references, an age pointer and a name pointer.
- Then it is possible to maintain one list, where if we follow the name pointer we can traverse the list in alphabetical order
- And if we try to traverse the age pointer, we can traverse the list by age also.
- This type of node organization may be useful for maintaining a customer list in a bank where the same list can be traversed in any order (name, age, or any other criteria) based on the need. For example, suppose my elements include the name of a person and his/her age. e.g.
(ANIMESH, 19), (SUMIT, 17), (HARDIK, 22), (ISHA, 18)
Multiple orders of set
Inserting into this structure is very much like inserting the same node into two separate lists. In multi-linked lists it is quite common to have back-pointers, i.e. inverses of each of the forward links; in the above example, this would mean that each node had 4pointers.
Representation of Sparse Matrix:
Multi Linked Lists are used to store sparse matrices. A sparse matrix is such a matrix that has few non-zero values. If we use a normal array to store such a matrix, it will end up wasting lots of space.
Spare Matrix
The sparse matrix can be represented by using a linked list for every row and column.
- A node in a multi-linked list has four parts:
- The first part stores the data.
- The second stores the pointer to the next row.
- Third for the pointer to the next column and
- Fourth for storing the coordinate number of the cell in the matrix.
Representation of sparse matrix
List of List:
A multi-linked list can be used to represent a list of lists. For example, we can create a linked list where each node is itself a list and have pointers to other nodes.
See the structure below:
- It is a 2-dimensional data structure.
- Here each node has three fields:
- The first field stores the data.
- The second field stores a pointer to the child node.
- The third field stores the pointer to the next node.
List of List (multi-level linked list)
Advantages of Multi-Linked List:
The advantages of a multi-linked list are:
- Sets of same data can be processed into multiple sequences.
- Data are not duplicated anywhere.
- Data of one kind exist only once in the list.
Comparison of Multi-Linked List with Doubly Linked List:
Let's first see the structure of a node of Doubly Linked List:
C++
#include <iostream>
using namespace std;
typedef struct node {
int data;
struct node* prev;
struct node* next;
} Node;
int main() {
return 0;
}
// This code is contributed by akashish__
C
typedef struct node {
int data;
struct node* prev;
struct node* next;
} Node;
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args) {}
public static class Node {
int data;
Node prev;
Node next;
};
}
// This code is contributed by ishankhandelwal
Python3
# Python code
class Node:
def _init_(self, val = 0, next = None):
self.val = val
self.next = next
# This code is contributed by lokesh.
C#
using System;
public class GFG{
static public void Main (){}
public class Node {
int data;
Node prev;
Node next;
};
}
// This code is contributed by aadityapburujwale
JavaScript
// JS code for above approach
class Node{
constructor(){
this.data;
this.prev=null;
this.next=null;
}
}
// This code is contributed by ishankhandelwals.
Comparing Doubly linked list and Multi-linked list:
- Unlike doubly nodes in a multilinked list may or may not have an inverse for each pointer.
- A doubly Linked list has exactly two pointers, whether multi-linked list can have multiple pointers
- In a doubly linked list, pointers are exactly opposite to each other, but in a multi-linked list, it is not so.
- Doubly linked list is a special case of multi-linked list.
Similar Reads
Introduction to Circular Linked List
A circular linked list is a data structure where the last node connects back to the first, forming a loop. This structure allows for continuous traversal without any interruptions. Circular linked lists are especially helpful for tasks like scheduling and managing playlists, allowing for smooth navi
15+ min read
Introduction to Doubly Linked Lists in Java
Doubly linked list is a data structure that has reference to both the previous and next nodes in the list. It provides simplicity to traverse, insert and delete the nodes in both directions in a list. In a doubly linked list, each node contains three data members: data: The data stored in the nodene
11 min read
Introduction to Circular Doubly Linked List
A circular doubly linked list is defined as a circular linked list in which each node has two links connecting it to the previous node and the next node. Circular doubly linked listCharacteristics of Circular Doubly Linked List :A circular doubly linked list has the following properties: Circular: A
4 min read
Linked List of Linked List
Linked list of linkeÂd list, also known as nested linkeÂd lists, is a type of Linked List where each main node stores another full linked list. This structure beats old, plain linked lists. It gives way more flexibility to store and manage compleÂx data. In this article we will learn about the bas
9 min read
Insertion in Linked List
Insertion in a linked list involves adding a new node at a specified position in the list. There are several types of insertion based on the position where the new node is to be added:At the front of the linked list Before a given node.After a given node.At a specific position.At the end of the link
4 min read
Concatenation of two Linked lists
Given two linked lists. The task is to concatenate the second list to the end of the first list.Examples:Input: list1: 10 -> 15 -> 4 -> 20, list2: 8 -> 4 -> 2 -> 10Output: 10 -> 15 -> 4 -> 20 -> 8 -> 4 -> 2 -> 10Input: list1: 1 -> 2 -> 3, list2: 4 -> 5
6 min read
Insertion in Unrolled Linked List
An unrolled linked list is a linked list of small arrays, all of the same size where each is so small that the insertion or deletion is fast and quick, but large enough to fill the cache line. An iterator pointing into the list consists of both a pointer to a node and an index into that node contain
11 min read
Queue - Linked List Implementation
In this article, the Linked List implementation of the queue data structure is discussed and implemented. Print '-1' if the queue is empty.Approach: To solve the problem follow the below idea:we maintain two pointers, front and rear. The front points to the first item of the queue and rear points to
8 min read
Find Middle of the Linked List
Given a singly linked list, the task is to find the middle of the linked list. If the number of nodes are even, then there would be two middle nodes, so return the second middle node.Example:Input: linked list: 1->2->3->4->5Output: 3 Explanation: There are 5 nodes in the linked list and
14 min read
Multilevel Linked List
Multilevel Linked ListMultilevel 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
8 min read