Insertion in Unrolled Linked List
Last Updated :
31 Jan, 2023
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 another variant of Linked List. It is related to B-Tree. It can store an array of elements at a node unlike a normal linked list which stores single element at a node. It is combination of arrays and linked list fusion-ed into one. It increases cache performance and decreases the memory overhead associated with storing reference for metadata. Other major advantages and disadvantages are already mentioned in the previous article.
Prerequisite : Introduction to Unrolled Linked List
Below is the insertion and display operation of Unrolled Linked List.
Input : 72 76 80 94 90 70
capacity = 3
Output : Unrolled Linked List :
72 76
80 94
90 70
Explanation : The working is well shown in the
algorithm below. The nodes get broken at the
mentioned capacity i.e., 3 here, when 3rd element
is entered, the flow moves to another newly created
node. Every node contains an array of size
(int)[(capacity / 2) + 1]. Here it is 2.
Input : 49 47 62 51 77 17 71 71 35 76 36 54
capacity = 5
Output :
Unrolled Linked List :
49 47 62
51 77 17
71 71 35
76 36 54
Explanation : The working is well shown in the
algorithm below. The nodes get broken at the
mentioned capacity i.e., 5 here, when 5th element
is entered, the flow moves to another newly
created node. Every node contains an array of
size (int)[(capacity / 2) + 1]. Here it is 3.
Algorithm :
Insert (ElementToBeInserted)
if start_pos == NULL
Insert the first element into the first node
start_pos.numElement ++
end_pos = start_pos
If end_pos.numElements + 1 < node_size
end_pos.numElements.push(newElement)
end_pos.numElements ++
else
create a new Node new_node
move final half of end_pos.data into new_node.data
new_node.data.push(newElement)
end_pos.numElements = end_pos.data.size / 2 + 1
end_pos.next = new_node
end_pos = new_node
Implementation: Following is the Java implementation of the insertion and display operation. In the below code, the capacity is 5 and random numbers are input.
C++
// C++ program to show the insertion operation of Unrolled Linked List
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// class for each node
class UnrollNode {
public:
UnrollNode* next;
int num_elements;
int* array;
// Constructor
UnrollNode(int n)
{
next = nullptr;
num_elements = 0;
array = new int[n];
}
};
// Operation of Unrolled Function
class UnrollLinkList {
private:
UnrollNode* start_pos;
UnrollNode* end_pos;
int size_node;
int nNode;
public:
// Parameterized Constructor
UnrollLinkList(int capacity)
{
start_pos = nullptr;
end_pos = nullptr;
nNode = 0;
size_node = capacity + 1;
}
// Insertion operation
void Insert(int num)
{
nNode++;
// Check if the list starts from NULL
if (start_pos == nullptr) {
start_pos = new UnrollNode(size_node);
start_pos->array[0] = num;
start_pos->num_elements++;
end_pos = start_pos;
return;
}
// Attaching the elements into nodes
if (end_pos->num_elements + 1 < size_node) {
end_pos->array[end_pos->num_elements] = num;
end_pos->num_elements++;
}
// Creation of new Node
else {
UnrollNode* node_pointer = new UnrollNode(size_node);
int j = 0;
for (int i = end_pos->num_elements / 2 + 1;
i < end_pos->num_elements; i++)
node_pointer->array[j++] = end_pos->array[i];
node_pointer->array[j++] = num;
node_pointer->num_elements = j;
end_pos->num_elements = end_pos->num_elements / 2 + 1;
end_pos->next = node_pointer;
end_pos = node_pointer;
}
}
// Display the Linked List
void display()
{
cout << "\nUnrolled Linked List = " << endl;
UnrollNode* pointer = start_pos;
while (pointer != nullptr) {
for (int i = 0; i < pointer->num_elements; i++)
cout << pointer->array[i] << " ";
cout << endl;
pointer = pointer->next;
}
cout << endl;
}
};
// Main Class
int main()
{
srand(time(0));
UnrollLinkList ull(5);
// Perform Insertion Operation
for (int i = 0; i < 12; i++) {
// Generate random integers in range 0 to 99
int rand_int1 = rand() % 100;
cout << "Entered Element is " << rand_int1 << endl;
ull.Insert(rand_int1);
ull.display();
}
return 0;
}
// This code is contributed by Vikram_Shirsat
Python3
# Python program to show the insertion operation of Unrolled Linked List
import random
# class for each node
class UnrollNode:
def __init__(self, n):
self.next = None
self.num_elements = 0
self.array = [0] * n
# Operation of Unrolled Function
class UnrollLinkList:
def __init__(self, capacity):
self.start_pos = None
self.end_pos = None
self.nNode = 0
self.size_node = capacity + 1
# Insertion operation
def Insert(self, num):
self.nNode += 1
# Check if the list starts from NULL
if self.start_pos is None:
self.start_pos = UnrollNode(self.size_node)
self.start_pos.array[0] = num
self.start_pos.num_elements += 1
self.end_pos = self.start_pos
return
# Attaching the elements into nodes
if self.end_pos.num_elements + 1 < self.size_node:
self.end_pos.array[self.end_pos.num_elements] = num
self.end_pos.num_elements += 1
# Creation of new Node
else:
node_pointer = UnrollNode(self.size_node)
j = 0
for i in range(self.end_pos.num_elements // 2 + 1, self.end_pos.num_elements):
node_pointer.array[j] = self.end_pos.array[i]
j += 1
node_pointer.array[j] = num
node_pointer.num_elements = j + 1
self.end_pos.num_elements = self.end_pos.num_elements // 2 + 1
self.end_pos.next = node_pointer
self.end_pos = node_pointer
# Display the Linked List
def display(self):
print("\nUnrolled Linked List = ")
pointer = self.start_pos
while pointer is not None:
for i in range(pointer.num_elements):
print(pointer.array[i], end=" ")
print()
pointer = pointer.next
print()
# Main function
if __name__ == "__main__":
ull = UnrollLinkList(5)
# Perform Insertion Operation
for i in range(12):
# Generate random integers in range 0 to 99
rand_int1 = random.randint(0, 99)
print("Entered Element is ", rand_int1)
ull.Insert(rand_int1)
ull.display()
# This code is contributed by Vikram_Shirsat
C#
/* C# program to show the insertion operation
* of Unrolled Linked List */
using System;
// class for each node
public class UnrollNode {
public UnrollNode next;
public int num_elements;
public int[] array;
// Constructor
public UnrollNode(int n)
{
next = null;
num_elements = 0;
array = new int[n];
}
}
// Operation of Unrolled Function
public class UnrollLinkList {
private UnrollNode start_pos;
private UnrollNode end_pos;
int size_node;
int nNode;
// Parameterized Constructor
public UnrollLinkList(int capacity)
{
start_pos = null;
end_pos = null;
nNode = 0;
size_node = capacity + 1;
}
// Insertion operation
public void Insert(int num)
{
nNode++;
// Check if the list starts from NULL
if (start_pos == null) {
start_pos = new UnrollNode(size_node);
start_pos.array[0] = num;
start_pos.num_elements++;
end_pos = start_pos;
return;
}
// Attaching the elements into nodes
if (end_pos.num_elements + 1 < size_node) {
end_pos.array[end_pos.num_elements] = num;
end_pos.num_elements++;
}
// Creation of new Node
else {
UnrollNode node_pointer = new UnrollNode(size_node);
int j = 0;
for (int i = end_pos.num_elements / 2 + 1;
i < end_pos.num_elements; i++)
node_pointer.array[j++] = end_pos.array[i];
node_pointer.array[j++] = num;
node_pointer.num_elements = j;
end_pos.num_elements = end_pos.num_elements / 2 + 1;
end_pos.next = node_pointer;
end_pos = node_pointer;
}
}
// Display the Linked List
public void display()
{
Console.Write("\nUnrolled Linked List = ");
Console.WriteLine();
UnrollNode pointer = start_pos;
while (pointer != null) {
for (int i = 0; i < pointer.num_elements; i++)
Console.Write(pointer.array[i] + " ");
Console.WriteLine();
pointer = pointer.next;
}
Console.WriteLine();
}
}
/* Main Class */
public class UnrolledLinkedList_Check {
// Driver code
public static void Main(String[] args)
{
// create instance of Random class
Random rand = new Random();
UnrollLinkList ull = new UnrollLinkList(5);
// Perform Insertion Operation
for (int i = 0; i < 12; i++) {
// Generate random integers in range 0 to 99
int rand_int1 = rand.Next(100);
Console.WriteLine("Entered Element is " + rand_int1);
ull.Insert(rand_int1);
ull.display();
}
}
}
// This code has been contributed by 29AjayKumar
Java
/* Java program to show the insertion operation
* of Unrolled Linked List */
import java.util.Scanner;
import java.util.Random;
// class for each node
class UnrollNode {
UnrollNode next;
int num_elements;
int array[];
// Constructor
public UnrollNode(int n)
{
next = null;
num_elements = 0;
array = new int[n];
}
}
// Operation of Unrolled Function
class UnrollLinkList {
private UnrollNode start_pos;
private UnrollNode end_pos;
int size_node;
int nNode;
// Parameterized Constructor
UnrollLinkList(int capacity)
{
start_pos = null;
end_pos = null;
nNode = 0;
size_node = capacity + 1;
}
// Insertion operation
void Insert(int num)
{
nNode++;
// Check if the list starts from NULL
if (start_pos == null) {
start_pos = new UnrollNode(size_node);
start_pos.array[0] = num;
start_pos.num_elements++;
end_pos = start_pos;
return;
}
// Attaching the elements into nodes
if (end_pos.num_elements + 1 < size_node) {
end_pos.array[end_pos.num_elements] = num;
end_pos.num_elements++;
}
// Creation of new Node
else {
UnrollNode node_pointer = new UnrollNode(size_node);
int j = 0;
for (int i = end_pos.num_elements / 2 + 1;
i < end_pos.num_elements; i++)
node_pointer.array[j++] = end_pos.array[i];
node_pointer.array[j++] = num;
node_pointer.num_elements = j;
end_pos.num_elements = end_pos.num_elements / 2 + 1;
end_pos.next = node_pointer;
end_pos = node_pointer;
}
}
// Display the Linked List
void display()
{
System.out.print("\nUnrolled Linked List = ");
System.out.println();
UnrollNode pointer = start_pos;
while (pointer != null) {
for (int i = 0; i < pointer.num_elements; i++)
System.out.print(pointer.array[i] + " ");
System.out.println();
pointer = pointer.next;
}
System.out.println();
}
}
/* Main Class */
class UnrolledLinkedList_Check {
// Driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// create instance of Random class
Random rand = new Random();
UnrollLinkList ull = new UnrollLinkList(5);
// Perform Insertion Operation
for (int i = 0; i < 12; i++) {
// Generate random integers in range 0 to 99
int rand_int1 = rand.nextInt(100);
System.out.println("Entered Element is " + rand_int1);
ull.Insert(rand_int1);
ull.display();
}
}
}
JavaScript
<script>
/* Javascript program to show the insertion operation
* of Unrolled Linked List */
// class for each node
class UnrollNode
{
// Constructor
constructor(n)
{
this.next = null;
this.num_elements = 0;
this.array = new Array(n);
for(let i = 0; i < n; i++)
{
this.array[i] = 0;
}
}
}
// Operation of Unrolled Function
class UnrollLinkList
{
// Parameterized Constructor
constructor(capacity)
{
this.start_pos = null;
this.end_pos = null;
this.nNode = 0;
this.size_node = capacity + 1;
}
// Insertion operation
Insert(num)
{
this.nNode++;
// Check if the list starts from NULL
if (this.start_pos == null) {
this.start_pos = new UnrollNode(this.size_node);
this.start_pos.array[0] = num;
this.start_pos.num_elements++;
this.end_pos = this.start_pos;
return;
}
// Attaching the elements into nodes
if (this.end_pos.num_elements + 1 < this.size_node) {
this.end_pos.array[this.end_pos.num_elements] = num;
this.end_pos.num_elements++;
}
// Creation of new Node
else {
let node_pointer = new UnrollNode(this.size_node);
let j = 0;
for (let i = Math.floor(this.end_pos.num_elements / 2 )+ 1;
i < this.end_pos.num_elements; i++)
node_pointer.array[j++] = this.end_pos.array[i];
node_pointer.array[j++] = num;
node_pointer.num_elements = j;
this.end_pos.num_elements = Math.floor(this.end_pos.num_elements / 2) + 1;
this.end_pos.next = node_pointer;
this.end_pos = node_pointer;
}
}
// Display the Linked List
display()
{
document.write("<br>Unrolled Linked List = ");
document.write("<br>");
let pointer = this.start_pos;
while (pointer != null) {
for (let i = 0; i < pointer.num_elements; i++)
document.write(pointer.array[i] + " ");
document.write("<br>");
pointer = pointer.next;
}
document.write("<br>");
}
}
// Driver code
let ull = new UnrollLinkList(5);
// Perform Insertion Operation
for (let i = 0; i < 12; i++)
{
// Generate random integers in range 0 to 99
let rand_int1 = Math.floor(Math.random()*(100));
document.write("Entered Element is " + rand_int1+"<br>");
ull.Insert(rand_int1);
ull.display();
}
// This code is contributed by rag2127
</script>
OutputEntered Element is 67
Unrolled Linked List =
67
Entered Element is 69
Unrolled Linked List =
67 69
Entered Element is 50
Unrolled Linked List =
67 69 50
Entered Element is 60
Unrolled Linked List =
67 69 50 60
Entered Element is 18
Unrolled Linked List =
67 69 50 60 18
Entered Element is 15
Unrolled Linked List =
67 69 50
60 18 15
Entered Element is 41
Unrolled Linked List =
67 69 50
60 18 15 41
Entered Element is 79
Unrolled Linked List =
67 69 50
60 18 15 41 79
Entered Element is 12
Unrolled Linked List =
67 69 50
60 18 15
41 79 12
Entered Element is 95
Unrolled Linked List =
67 69 50
60 18 15
41 79 12 95
Entered Element is 37
Unrolled Linked List =
67 69 50
60 18 15
41 79 12 95 37
Entered Element is 13
Unrolled Linked List =
67 69 50
60 18 15
41 79 12
95 37 13
Time complexity : O(n)
Also, few real-world applications :
- It is used in B-Tree and T-Tree
- Used in Hashed Array Tree
- Used in Skip List
- Used in CDR Coding
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