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++
#include <bits/stdc++.h>
using namespace std;
#define maxElements 4
class Node
{
public :
int numElements;
int array[maxElements];
Node *next;
};
void printUnrolledList(Node *n)
{
while (n != NULL)
{
for ( int i=0; i<n->numElements; i++)
cout<<n->array[i]<< " " ;
n = n->next;
}
}
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
head = new Node();
second = new Node();
third = new Node();
head->numElements = 3;
head->array[0] = 1;
head->array[1] = 2;
head->array[2] = 3;
head->next = second;
second->numElements = 3;
second->array[0] = 4;
second->array[1] = 5;
second->array[2] = 6;
second->next = third;
third->numElements = 3;
third->array[0] = 7;
third->array[1] = 8;
third->array[2] = 9;
third->next = NULL;
printUnrolledList(head);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
#define maxElements 4
struct Node
{
int numElements;
int array[maxElements];
struct Node *next;
};
void printUnrolledList( struct Node *n)
{
while (n != NULL)
{
for ( int i=0; i<n->numElements; i++)
printf ( "%d " , n->array[i]);
n = n->next;
}
}
int main()
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = ( struct Node*) malloc ( sizeof ( struct Node));
second = ( struct Node*) malloc ( sizeof ( struct Node));
third = ( struct Node*) malloc ( sizeof ( struct Node));
head->numElements = 3;
head->array[0] = 1;
head->array[1] = 2;
head->array[2] = 3;
head->next = second;
second->numElements = 3;
second->array[0] = 4;
second->array[1] = 5;
second->array[2] = 6;
second->next = third;
third->numElements = 3;
third->array[0] = 7;
third->array[1] = 8;
third->array[2] = 9;
third->next = NULL;
printUnrolledList(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int maxElements = 4 ;
static class Node
{
int numElements;
int []array = new int [maxElements];
Node next;
};
static void printUnrolledList(Node n)
{
while (n != null )
{
for ( int i = 0 ; i < n.numElements; i++)
System.out.print(n.array[i] + " " );
n = n.next;
}
}
public static void main(String[] args)
{
Node head = null ;
Node second = null ;
Node third = null ;
head = new Node();
second = new Node();
third = new Node();
head.numElements = 3 ;
head.array[ 0 ] = 1 ;
head.array[ 1 ] = 2 ;
head.array[ 2 ] = 3 ;
head.next = second;
second.numElements = 3 ;
second.array[ 0 ] = 4 ;
second.array[ 1 ] = 5 ;
second.array[ 2 ] = 6 ;
second.next = third;
third.numElements = 3 ;
third.array[ 0 ] = 7 ;
third.array[ 1 ] = 8 ;
third.array[ 2 ] = 9 ;
third.next = null ;
printUnrolledList(head);
}
}
|
Python3
maxElements = 4
class Node:
def __init__( self ):
self .numElements = 0
self .array = [ 0 for i in range (maxElements)]
self . next = None
def printUnrolledList(n):
while (n ! = None ):
for i in range (n.numElements):
print (n.array[i], end = ' ' )
n = n. next
if __name__ = = '__main__' :
head = None
second = None
third = None
head = Node()
second = Node()
third = Node()
head.numElements = 3
head.array[ 0 ] = 1
head.array[ 1 ] = 2
head.array[ 2 ] = 3
head. next = second
second.numElements = 3
second.array[ 0 ] = 4
second.array[ 1 ] = 5
second.array[ 2 ] = 6
second. next = third
third.numElements = 3
third.array[ 0 ] = 7
third.array[ 1 ] = 8
third.array[ 2 ] = 9
third. next = None
printUnrolledList(head)
|
C#
using System;
class GFG{
static readonly int maxElements = 4;
class Node
{
public int numElements;
public int []array = new int [maxElements];
public Node next;
};
static void printUnrolledList(Node n)
{
while (n != null )
{
for ( int i = 0; i < n.numElements; i++)
Console.Write(n.array[i] + " " );
n = n.next;
}
}
public static void Main(String[] args)
{
Node head = null ;
Node second = null ;
Node third = null ;
head = new Node();
second = new Node();
third = new Node();
head.numElements = 3;
head.array[0] = 1;
head.array[1] = 2;
head.array[2] = 3;
head.next = second;
second.numElements = 3;
second.array[0] = 4;
second.array[1] = 5;
second.array[2] = 6;
second.next = third;
third.numElements = 3;
third.array[0] = 7;
third.array[1] = 8;
third.array[2] = 9;
third.next = null ;
printUnrolledList(head);
}
}
|
Javascript
<script>
const maxElements = 4;
class Node {
constructor() {
this .numElements = 0;
this .array = new Array(maxElements);
this .next = null ;
}
}
function printUnrolledList(n) {
while (n != null ) {
for ( var i = 0; i < n.numElements; i++)
document.write(n.array[i] + " " );
n = n.next;
}
}
var head = null ;
var second = null ;
var third = null ;
head = new Node();
second = new Node();
third = new Node();
head.numElements = 3;
head.array[0] = 1;
head.array[1] = 2;
head.array[2] = 3;
head.next = second;
second.numElements = 3;
second.array[0] = 4;
second.array[1] = 5;
second.array[2] = 6;
second.next = third;
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
Please Login to comment...