Open In App

Unrolled Linked List | Set 1 (Introduction)

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Like array and linked list, the unrolled Linked List is also a linear data structure and is a variant of a linked list. 

Why do we need unrolled linked list?

One of the biggest advantages of linked lists over arrays is that inserting an element at any location takes only O(1). However, the catch here is that to search an element in a linked list takes O(n). So to solve the problem of searching i.e reducing the time for searching the element the concept of unrolled linked lists was put forward. The unrolled linked list covers the advantages of both array and linked list as it reduces the memory overhead in comparison to simple linked lists by storing multiple elements at each node and it also has the advantage of fast insertion and deletion as that of a linked list.

unrolledlinkedlist

Advantages:

  • Because of the Cache behavior, linear search is much faster in unrolled linked lists.
  • In comparison to the ordinary linked list, it requires less storage space for pointers/references.
  • It performs operations like insertion, deletion, and traversal more quickly than ordinary linked lists (because search is faster).

Disadvantages:

  • The overhead per node is comparatively high than singly-linked lists. Refer to an example node in the below code

Example: Let say we are having 8 elements so sqrt(8)=2.82 which rounds off to 3. So each block will store 3 elements. Hence, to store 8 elements 3 blocks will be created out of which the first two blocks will store 3 elements and the last block would store 2 elements.

How searching becomes better in unrolled linked lists?

So taking the above example if we want to search for the 7th element in the list we traverse the list of blocks to the one that contains the 7th element. It takes only O(sqrt(n)) since we found it through not going more than sqrt(n) blocks. 

Simple Implementation:

The below program creates a simple unrolled linked list with 3 nodes containing a variable number of elements in each. It also traverses the created list.

C++




// C++ program to implement unrolled linked list 
// and traversing it. 
#include <bits/stdc++.h>
using namespace std;
#define maxElements 4 
  
// Unrolled Linked List Node 
class Node 
    public:
    int numElements; 
    int array[maxElements]; 
    Node *next; 
}; 
  
/* Function to traverse an unrolled linked list 
and print all the elements*/
void printUnrolledList(Node *n) 
    while (n != NULL) 
    
        // Print elements in current node 
        for (int i=0; i<n->numElements; i++) 
            cout<<n->array[i]<<" "
  
        // Move to next node 
        n = n->next; 
    
  
// Program to create an unrolled linked list 
// with 3 Nodes 
int main() 
    Node* head = NULL; 
    Node* second = NULL; 
    Node* third = NULL; 
  
    // allocate 3 Nodes 
    head = new Node();
    second = new Node();
    third = new Node();
  
    // Let us put some values in second node (Number 
    // of values must be less than or equal to 
    // maxElement) 
    head->numElements = 3; 
    head->array[0] = 1; 
    head->array[1] = 2; 
    head->array[2] = 3; 
  
    // Link first Node with the second Node 
    head->next = second; 
  
    // Let us put some values in second node (Number 
    // of values must be less than or equal to 
    // maxElement) 
    second->numElements = 3; 
    second->array[0] = 4; 
    second->array[1] = 5; 
    second->array[2] = 6; 
  
    // Link second Node with the third Node 
    second->next = third; 
  
    // Let us put some values in third node (Number 
    // of values must be less than or equal to 
    // maxElement) 
    third->numElements = 3; 
    third->array[0] = 7; 
    third->array[1] = 8; 
    third->array[2] = 9; 
    third->next = NULL; 
  
    printUnrolledList(head); 
  
    return 0; 
  
// This is code is contributed by rathbhupendra


C




// C program to implement unrolled linked list
// and traversing it.
#include<stdio.h>
#include<stdlib.h>
#define maxElements 4
  
// Unrolled Linked List Node
struct Node
{
    int numElements;
    int array[maxElements];
    struct Node *next;
};
  
/* Function to traverse an unrolled linked list
   and print all the elements*/
void printUnrolledList(struct Node *n)
{
    while (n != NULL)
    {
        // Print elements in current node
        for (int i=0; i<n->numElements; i++)
            printf("%d ", n->array[i]);
  
        // Move to next node 
        n = n->next;
    }
}
  
// Program to create an unrolled linked list
// with 3 Nodes
int main()
{
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;
  
    // allocate 3 Nodes
    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
  
    // Let us put some values in second node (Number
    // of values must be less than or equal to
    // maxElement)
    head->numElements = 3;
    head->array[0] = 1;
    head->array[1] = 2;
    head->array[2] = 3;
  
    // Link first Node with the second Node
    head->next = second;
  
    // Let us put some values in second node (Number
    // of values must be less than or equal to
    // maxElement)
    second->numElements = 3;
    second->array[0] = 4;
    second->array[1] = 5;
    second->array[2] = 6;
  
    // Link second Node with the third Node
    second->next = third;
  
    // Let us put some values in third node (Number
    // of values must be less than or equal to
    // maxElement)
    third->numElements = 3;
    third->array[0] = 7;
    third->array[1] = 8;
    third->array[2] = 9;
    third->next = NULL;
  
    printUnrolledList(head);
  
    return 0;
}


Java




// Java program to implement unrolled
// linked list and traversing it. 
import java.util.*;
  
class GFG{
      
static final int maxElements = 4;
  
// Unrolled Linked List Node 
static class Node 
    int numElements; 
    int []array = new int[maxElements]; 
    Node next; 
}; 
  
// Function to traverse an unrolled 
// linked list  and print all the elements
static void printUnrolledList(Node n) 
    while (n != null
    
        
        // Print elements in current node 
        for(int i = 0; i < n.numElements; i++) 
            System.out.print(n.array[i] + " "); 
  
        // Move to next node 
        n = n.next; 
    
  
// Program to create an unrolled linked list 
// with 3 Nodes 
public static void main(String[] args) 
    Node head = null
    Node second = null
    Node third = null
  
    // Allocate 3 Nodes 
    head = new Node();
    second = new Node();
    third = new Node();
  
    // Let us put some values in second 
    // node (Number of values must be 
    // less than or equal to maxElement) 
    head.numElements = 3
    head.array[0] = 1
    head.array[1] = 2
    head.array[2] = 3
  
    // Link first Node with the 
    // second Node 
    head.next = second; 
  
    // Let us put some values in 
    // second node (Number of values
    // must be less than or equal to 
    // maxElement) 
    second.numElements = 3
    second.array[0] = 4
    second.array[1] = 5
    second.array[2] = 6
  
    // Link second Node with the third Node 
    second.next = third; 
  
    // Let us put some values in third 
    // node (Number of values must be
    // less than or equal to maxElement) 
    third.numElements = 3
    third.array[0] = 7
    third.array[1] = 8
    third.array[2] = 9
    third.next = null
  
    printUnrolledList(head); 
  
// This code is contributed by amal kumar choubey  


Python3




# Python3 program to implement unrolled
# linked list and traversing it. 
maxElements = 4 
  
# Unrolled Linked List Node 
class Node:
      
    def __init__(self):
          
        self.numElements = 0
        self.array = [0 for i in range(maxElements)] 
        self.next = None
  
# Function to traverse an unrolled linked list 
# and print all the elements
def printUnrolledList(n):
  
    while (n != None):
  
        # Print elements in current node 
        for i in range(n.numElements):
            print(n.array[i], end = ' ')
  
        # Move to next node 
        n = n.next
  
# Driver Code
if __name__=='__main__':
      
    head = None
    second = None 
    third = None 
  
    # Allocate 3 Nodes 
    head = Node()
    second = Node()
    third = Node()
  
    # Let us put some values in second
    # node (Number of values must be 
    # less than or equal to 
    # maxElement) 
    head.numElements = 3
    head.array[0] = 1
    head.array[1] = 2 
    head.array[2] = 3 
  
    # Link first Node with the second Node 
    head.next = second
  
    # Let us put some values in second node
    # (Number of values must be less than
    # or equal to maxElement) 
    second.numElements = 3
    second.array[0] = 4
    second.array[1] = 5 
    second.array[2] = 6 
  
    # Link second Node with the third Node 
    second.next = third 
  
    # Let us put some values in third node
    # (Number of values must be less than 
    # or equal to maxElement) 
    third.numElements = 3
    third.array[0] = 7
    third.array[1] = 8 
    third.array[2] = 9 
    third.next = None 
  
    printUnrolledList(head)
      
# This code is contributed by rutvik_56


C#




// C# program to implement unrolled
// linked list and traversing it. 
using System;
class GFG{
      
static readonly int maxElements = 4;
  
// Unrolled Linked List Node 
class Node 
  public int numElements; 
  public int []array = new int[maxElements]; 
  public Node next; 
}; 
  
// Function to traverse an unrolled 
// linked list  and print all the elements
static void printUnrolledList(Node n) 
  while (n != null
  
    // Print elements in current node 
    for(int i = 0; i < n.numElements; i++) 
      Console.Write(n.array[i] + " "); 
  
    // Move to next node 
    n = n.next; 
  
  
// Program to create an unrolled linked list 
// with 3 Nodes 
public static void Main(String[] args) 
  Node head = null
  Node second = null
  Node third = null
  
  // Allocate 3 Nodes 
  head = new Node();
  second = new Node();
  third = new Node();
  
  // Let us put some values in second 
  // node (Number of values must be 
  // less than or equal to maxElement) 
  head.numElements = 3; 
  head.array[0] = 1; 
  head.array[1] = 2; 
  head.array[2] = 3; 
  
  // Link first Node with the 
  // second Node 
  head.next = second; 
  
  // Let us put some values in 
  // second node (Number of values
  // must be less than or equal to 
  // maxElement) 
  second.numElements = 3; 
  second.array[0] = 4; 
  second.array[1] = 5; 
  second.array[2] = 6; 
  
  // Link second Node with the third Node 
  second.next = third; 
  
  // Let us put some values in third 
  // node (Number of values must be
  // less than or equal to maxElement) 
  third.numElements = 3; 
  third.array[0] = 7; 
  third.array[1] = 8; 
  third.array[2] = 9; 
  third.next = null
  
  printUnrolledList(head); 
  
// This code is contributed by Rajput-Ji


Javascript




<script>
  
      // JavaScript program to implement unrolled
      // linked list and traversing it.
      const maxElements = 4;
  
      // Unrolled Linked List Node
      class Node {
        constructor() {
          this.numElements = 0;
          this.array = new Array(maxElements);
          this.next = null;
        }
      }
  
      // Function to traverse an unrolled
      // linked list and print all the elements
      function printUnrolledList(n) {
        while (n != null) {
          // Print elements in current node
          for (var i = 0; i < n.numElements; i++)
            document.write(n.array[i] + " ");
  
          // Move to next node
          n = n.next;
        }
      }
  
      // Program to create an unrolled linked list
      // with 3 Nodes
  
      var head = null;
      var second = null;
      var third = null;
  
      // Allocate 3 Nodes
      head = new Node();
      second = new Node();
      third = new Node();
  
      // Let us put some values in second
      // node (Number of values must be
      // less than or equal to maxElement)
      head.numElements = 3;
      head.array[0] = 1;
      head.array[1] = 2;
      head.array[2] = 3;
  
      // Link first Node with the
      // second Node
      head.next = second;
  
      // Let us put some values in
      // second node (Number of values
      // must be less than or equal to
      // maxElement)
      second.numElements = 3;
      second.array[0] = 4;
      second.array[1] = 5;
      second.array[2] = 6;
  
      // Link second Node with the third Node
      second.next = third;
  
      // Let us put some values in third
      // node (Number of values must be
      // less than or equal to maxElement)
      third.numElements = 3;
      third.array[0] = 7;
      third.array[1] = 8;
      third.array[2] = 9;
      third.next = null;
  
      printUnrolledList(head);
        
</script>


Output

1 2 3 4 5 6 7 8 9 

Complexity Analysis:

  • Time Complexity: O(n).
  • Space Complexity: O(n).

In this article, we have introduced an unrolled list and advantages of it. We have also shown how to traverse the list. In the next article, we will be discussing the insertion, deletion, and values of maxElements/numElements in detail.

Insertion in Unrolled Linked List

 



Previous Article
Next Article

Similar Reads

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 containing an array. It is also a data structure and is a
11 min read
XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert a new node at the beginning.A function to travers
10 min read
XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists. We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, whi
15+ min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
Convert Singly Linked List to XOR Linked List
Prerequisite: XOR Linked List – A Memory Efficient Doubly Linked List | Set 1XOR Linked List – A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a singly linked list, the task is to convert the gi
9 min read
Create new linked list from two given linked list with greater element at each node
Given two linked list of the same size, the task is to create a new linked list using those linked lists. The condition is that the greater node among both linked list will be added to the new linked list.Examples: Input: list1 = 5-&gt;2-&gt;3-&gt;8 list2 = 1-&gt;7-&gt;4-&gt;5 Output: New list = 5-&gt;7-&gt;4-&gt;8 Input: list1 = 2-&gt;8-&gt;9-&gt;
8 min read
Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
Given a Linked List of even number of nodes, the task is to generate a new Linked List such that it contains the maximum difference of squares of node values in decreasing order by including each node in a single pair. Examples: Input: 1 -&gt; 6 -&gt; 4 -&gt; 3 -&gt; 5 -&gt;2Output: 35 -&gt; 21 -&gt; 7Explanation:The difference between squares of 6
11 min read
XOR linked list- Remove first node of the linked list
Given an XOR linked list, the task is to remove the first node of the XOR linked list. Examples: Input: XLL = 4 &lt; – &gt; 7 &lt; – &gt; 9 &lt; – &gt; 7 Output: 7 &lt; – &gt; 9 &lt; – &gt; 7 Explanation: Removing the first node of the XOR linked list modifies XLL to 7 &lt; – &gt; 9 &lt; – &gt; 7 Input: XLL = NULL Output: List Is Empty Approach: Th
11 min read
Remove all occurrences of one Linked list in another Linked list
Given two linked lists head1 and head2, the task is to remove all occurrences of head2 in head1 and return the head1. Examples: Input: head1 = 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 3 -&gt; 4, head2 = 3 -&gt; 4Output: 2 -&gt; 5Explanation: After removing all occurrences of 3 -&gt; 4 in head1 output is 2 -&gt; 5. Input: head1 = 3 -&gt; 6 -&gt; 9 -&gt; 8 -
9 min read
Insert a linked list into another linked list
Given two linked lists, list1 and list2 of sizes m and n respectively. The task is to remove list1's nodes from the ath node to the bth node and insert the list2 in their place.Examples: Input: list1: 10-&gt;11-&gt;12-&gt;13-&gt;14-&gt;15, list2: 100-&gt;101-&gt;102-&gt;103, a = 3, b = 4Output: 10-&gt;11-&gt;12-&gt;100-&gt;101-&gt;102-&gt;103-&gt;1
13 min read
Article Tags :
Practice Tags :