Java Program to Implement Unrolled Linked List


In this problem, we will learn to implement the unrolled linked list. The unrolled linked list is a specialized version of the linked list. The normal linked list contains a single element in a single node, but the unrolled linked list contains a group of elements in each node.

Also, insertion, deletion, and traversal in the unrolled linked list work the same as the typical linked list. The linear search is faster in the array than in the linked list. So, we can add elements in the array and an array in each node of the linked list. Also, unrolled linked list improves the efficiency of the traversing and optimizes the memory uses.

Problem statement - We need to write the Java code to implement the unrolled linked list using the array.

Benefits of using the unrolled linked list over the typical linked list

  • The first benefit is that it improves the search and insertion efficiency as we can insert elements in o(1) time in the array.

  • We can use an unrolled linked list where memory efficiency and cache performance are important.

  • Fast iteration as we use an array.

  • We can save memory as we don't need a pointer for every element.

Steps

Step 1 - In this example, we defined the 'listNode' class to create nodes for the linked list.

Step 2 - Declare the variables for the list size, list, and next node. Each node will contain the list of list size.

Step 3 - Add a constructor to initialize the class variables when we create the object of the given class.

Step 4 - Define the insertElement() function to insert the element into the unrolled linked list.

Step 5 - If the list size is less than the listSize variable's value, add an element to the list using the add() method.

Step 6 - Otherwise, we need to add an element to another list. If the next is null, we need to create a new list, as all lists are full. After that, call the insertElement() function again.

Step 7 - If the next is not null, it means the next list exists and has space, so add an element.

Step 8 - Define the deleteElement() function to delete the element from the linked list.

Step 9 - If the current list contains the element, remove it from the list, and return true.

Step 10 - Otherwise, If the next is null, we iterated all lists and haven't found the element in the linked list, so return false. Else, invoke the deleteElement() method on the next list.

Step 11 - In the last, return true.

Step 12 - Create an object of the listNode class, and insert elements into that. Also, traverse the list and print the elements.

Example 1

In this example, we can insert and delete the elements from the unrolled linked list like a normal one. Also, if we delete any element from any list, and after we insert new element, it will fill the semi-empty list first rather than creating a new list. So it optimizes the space.

import java.io.*;
import java.util.*;
public class Main {
   // Nested static class
   static class listNode {
      // Declaring variables
      int listSize;
      ArrayList<Integer> list;
      listNode next = null;
      // constructor
      listNode(int listsize) {
         // Initialize the listSize
         this.listSize = listsize;
         // Creating the array list
         list = new ArrayList<>();
      }
      // Inserting elements into the list
      void insertElement(int element) {
         // If the array contains space, insert an element to the list
         if (list.size() < listSize) {
            list.add(element);
         } else { // array is full // If the new array is not started, create a new node and call the insertElement() method
            if (next == null) {
               next = new listNode(listSize);
               next.insertElement(element);
            } else {
               // Insert element to the array
               next.insertElement(element);
            }
         }
      }
      // Deleting elements from the list
      boolean deleteElement(int element) {
         // If the list has the element, remove it
         if (list.contains(element)) {
            list.remove(list.indexOf(element));
            return true;
         } else { // If the list has no element and the next is null, the element doesn't exist in the
            // linked list
            if (next == null) {
               return false;
            } else {
               // Move to the next list
               next.deleteElement(element);
            }
            return true;
         }
      }
   }
   public static void printList(listNode x) {
      while (x != null) {
         System.out.println(x.list);
         x = x.next;
      }
   }
   public static void main(String args[]) {
      listNode x = new listNode(3);
      // Inserting elements to a linked list
      for (int p = 1; p <= 16; p++) {
         x.insertElement(p * 10);
      }
      printList(x);
      // delete 10, 30, 70, and 100.
      x.deleteElement(10);
      x.deleteElement(30);
      x.deleteElement(70);
      x.deleteElement(100);
      // Print updated list
      System.out.println(" ");
      System.out.println("Updated list");
      printList(x);
   }
}

Output

[10, 20, 30]
[40, 50, 60]
[70, 80, 90]
[100, 110, 120]
[130, 140, 150]
[160]
 
Updated list
[20]
[40, 50, 60]
[80, 90]
[110, 120]
[130, 140, 150]
[160]

Time complexity - O(N) to traverse the list and delete the element.

Space complexity - O(Nodes*list_Size)

Steps for Example 2

Step 1 - Create the static class named listNode, representing the node of the linked list.

Step 2 - Define the totalElements, list, and nextNode variables inside the class.

Step 3 - Define four different nodes by creating the class object.

Step 4 - Initialize the list of each node with elements, and join the lists by the next node.

Step 5 - Define the printList() method to print all elements of unrolled linked list.

Step 6 - In the printList() function, make iterations until the start node is not null.

Step 7 - After that, traverse each element of the list, and print them.

Example 2

In the example below, we created the unrolled linked list to store the group of prime numbers.

import java.util.*;
public class Main {
   static final int maxNumbers = 5;
   // Creating the unrolled Linked list
   static class listNode {
     int totalElements;
     int[] list = new int[maxNumbers];
     listNode nextNode;
   };
   static void printList(listNode start) {
     while (start != null) {
       System.out.print("[ ");
       // print the elements of the current list
       for (int p = 0; p < start.totalElements; p++)
         System.out.print(start.list[p] + " ");
       System.out.println("] ");
       // Move to next node
       start = start.nextNode;
     }
   }
   public static void main(String[] args) {
     listNode start = null;
     listNode secondList = null;
     listNode thirdList = null;
     listNode fourthList = null;
     // Initialize nodes
     start = new listNode();
     secondList = new listNode();
     thirdList = new listNode();
     fourthList = new listNode();
     // add Values to the list
     start.totalElements = 4;
     start.list[0] = 2;
     start.list[1] = 3;
     start.list[2] = 5;
     start.list[3] = 7;
     // Link the next node to the first
     start.nextNode = secondList;
     // add elements to the second list
     secondList.totalElements = 4;
     secondList.list[0] = 11;
     secondList.list[1] = 13;
     secondList.list[2] = 17;
     secondList.list[3] = 19;
     secondList.nextNode = thirdList;
     // add elements to the third list
     thirdList.totalElements = 2;
     thirdList.list[0] = 23;
     thirdList.list[1] = 29;
     thirdList.nextNode = fourthList;
     // add elements to the fourth list
     fourthList.totalElements = 2;
     fourthList.list[0] = 31;
     fourthList.list[1] = 37;
     // assign null to the next of the fourth list
     fourthList.nextNode = null;
     printList(start);
   }
}

Output

[ 2 3 5 7 ] 
[ 11 13 17 19 ] 
[ 23 29 ] 
[ 31 37 ]

We learned to create an unrolled linked list in Java. Programmers can observe how it saves memory. Also, like the second example, we can use it to store a group of elements in each node.

Updated on: 24-Aug-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements