Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Searching in Splay Tree
Next article icon

Unrolled Linked List | Set 1 (Introduction)

Last Updated : 11 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
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

 



Next Article
Searching in Splay Tree

H

Harsh Agarwal
Improve
Article Tags :
  • DSA
  • Linked List
Practice Tags :
  • Linked List

Similar Reads

  • Advanced Data Structures
    Advanced Data Structures refer to complex and specialized arrangements of data that enable efficient storage, retrieval, and manipulation of information in computer science and programming. These structures go beyond basic data types like arrays and lists, offering sophisticated ways to organize and
    3 min read
  • Generic Linked List in C
    A generic linked list is a type of linked list that allows the storage of different types of data in a single linked list structure, providing more versatility and reusability in various applications. Unlike C++ and Java, C doesn't directly support generics. However, we can write generic code using
    5 min read
  • Memory efficient doubly linked list
    We need to implement a doubly linked list with the use of a single pointer in each node. For that we are given a stream of data of size n for the linked list, your task is to make the function insert() and getList(). The insert() function pushes (or inserts at the beginning) the given data in the li
    9 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 lin
    15+ 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
    10 min read
  • Skip List - Efficient Search, Insert and Delete in Linked List
    A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis. In a skip list, elements are organized in layers, with
    6 min read
  • Self Organizing List | Set 1 (Introduction)
    The worst case search time for a sorted linked list is O(n). With a Balanced Binary Search Tree, we can skip almost half of the nodes after one comparison with root. For a sorted array, we have random access and we can apply Binary Search on arrays. One idea to make search faster for Linked Lists is
    3 min read
  • 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. 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
    10 min read
  • Splay Tree

    • Searching in Splay Tree
      Splay Tree- Splay tree is a binary search tree. In a splay tree, M consecutive operations can be performed in O (M log N) time. A single operation may require O(N) time but average time to perform M operations will need O (M Log N) time. When a node is accessed, it is moved to the top through a set
      15+ min read

    • Insertion in Splay Tree
      It is recommended to refer following post as prerequisite of this post.Splay Tree | Set 1 (Search)As discussed in the previous post, Splay tree is a self-balancing data structure where the last accessed key is always at root. The insert operation is similar to Binary Search Tree insert with addition
      15+ min read

    Trie

    • Trie Data Structure Tutorial
      The trie data structure, also known as a prefix tree, is a tree-like data structure used for efficient retrieval of key-value pairs. It is commonly used for implementing dictionaries and autocomplete features, making it a fundamental component in many search algorithms. In this article, we will expl
      15+ min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences