Unrolled Linked List | Set 1 (Introduction)

Like array and linked list, the unrolled Linked List is also a linear data structure and is a variant of a linked list. Unlike a simple linked list, it stores multiple elements at each node. That is, instead of storing a single element at a node, unrolled linked lists store an array of elements at a node. 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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 am 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;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 am 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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 am 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 am 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

chevron_right


Output:

1 2 3 4 5 6 7 8 9


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
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up