Unrolled Linked List | Set 1 (Introduction)
Last Updated :
11 Sep, 2023
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.


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>
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
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem