Java Program to Implement Unrolled Linked List

This is a Java Program to implement an Unrolled Linked List. An unrolled linked list is a variation on the linked list which stores multiple elements in each node. It can dramatically increase cache performance, while decreasing the memory overhead associated with storing list metadata such as references. It is related to the B-tree.

Here is the source code of the Java program to implement an Unrolled Linked List. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *  Java Program to Implement Unrolled Linked List
  3.  */
  4.  
  5. import java.util.Scanner;
  6.  
  7. /*  Class ULLNode  */
  8. class ULLNode    
  9. {
  10.     ULLNode next;
  11.     int numElements;
  12.     int array[];
  13.  
  14.     /* Constructor */
  15.     public ULLNode(int n)
  16.     {
  17.         next = null;
  18.         numElements = 0;
  19.         array = new int[n];        
  20.     }
  21. }
  22.  
  23. /* Class UnrolledLinkedList */
  24. class UnrolledLinkedList
  25. {
  26.     private ULLNode start;
  27.     private ULLNode end;
  28.     private int sizeNode;
  29.     private int nNode;
  30.  
  31.     /* Constructor */
  32.     public UnrolledLinkedList(int capacity)
  33.     {
  34.         start = null;
  35.         end = null;
  36.         nNode = 0;
  37.         sizeNode = capacity + 1;
  38.     }
  39.     /*  Function to check if list is empty  */
  40.     public boolean isEmpty()
  41.     {
  42.         return start == null;
  43.     }
  44.     /*  Function to get size of list  */
  45.     public int getSize()
  46.     {
  47.         return nNode;
  48.     }  
  49.     /* Function to clear list */
  50.     public void makeEmpty()
  51.     {
  52.         start = null;
  53.         end = null;
  54.         nNode = 0;
  55.     }
  56.     /* Function to insert element */
  57.     public void insert(int x)
  58.     {
  59.         nNode++;
  60.         if (start == null)
  61.         {
  62.             start = new ULLNode(sizeNode);
  63.             start.array[0] = x;
  64.             start.numElements++;
  65.             end = start;
  66.             return;
  67.         }
  68.         if (end.numElements + 1 < sizeNode)
  69.         {
  70.             end.array[end.numElements] = x;
  71.             end.numElements++;                        
  72.         }
  73.         else
  74.         {
  75.             ULLNode nptr = new ULLNode(sizeNode);
  76.             int j = 0;
  77.             for (int i = end.numElements / 2 + 1; i < end.numElements; i++)
  78.                 nptr.array[j++] = end.array[i];
  79.             nptr.array[j++] = x;
  80.             nptr.numElements = j;
  81.             end.numElements = end.numElements/2 + 1;
  82.             end.next = nptr;  
  83.             end = nptr;          
  84.         }        
  85.     }
  86.     /* Function to display list */
  87.     public void display()
  88.     {
  89.         System.out.print("\nUnrolled Linked List = ");
  90.         if (nNode == 0) 
  91.         {
  92.             System.out.print("empty\n");
  93.             return;
  94.         }
  95.         System.out.println();
  96.         ULLNode ptr = start;
  97.         while (ptr != null)
  98.         {
  99.             for (int i = 0; i < ptr.numElements; i++)
  100.                 System.out.print(ptr.array[i] +" ");
  101.             System.out.println();            
  102.             ptr = ptr.next;
  103.         }
  104.     }
  105.  
  106. }
  107.  
  108. /*  Class UnrolledLinkedListTest  */
  109. public class UnrolledLinkedListTest
  110. {    
  111.     public static void main(String[] args)
  112.     {             
  113.         Scanner scan = new Scanner(System.in);
  114.         System.out.println("Unrolled Linked List Test\n");  
  115.         System.out.println("Enter array size of each node");       
  116.         /* Creating object of class UnrolledLinkedList */
  117.         UnrolledLinkedList ull = new UnrolledLinkedList(scan.nextInt() ); 
  118.  
  119.         char ch;
  120.         /*  Perform list operations  */
  121.         do
  122.         {
  123.             System.out.println("\nUnrolled Linked List Operations\n");
  124.             System.out.println("1. insert");
  125.             System.out.println("2. check empty");
  126.             System.out.println("3. get size");  
  127.             System.out.println("4. clear");            
  128.             int choice = scan.nextInt();            
  129.             switch (choice)
  130.             {
  131.             case 1 :  
  132.                 System.out.println("Enter integer element to insert");
  133.                 ull.insert( scan.nextInt() );                     
  134.                 break;                          
  135.             case 2 : 
  136.                 System.out.println("Empty status = "+ ull.isEmpty());
  137.                 break;                   
  138.             case 3 : 
  139.                 System.out.println("Size = "+ ull.getSize() +" \n");
  140.                 break; 
  141.             case 4 : 
  142.                 System.out.println("List Cleared\n");
  143.                 ull.makeEmpty();
  144.                 break;                        
  145.             default : 
  146.                 System.out.println("Wrong Entry \n ");
  147.                 break;   
  148.             }
  149.             /*  Display List  */ 
  150.             ull.display();
  151.  
  152.             System.out.println("\nDo you want to continue (Type y or n) \n");
  153.             ch = scan.next().charAt(0);                        
  154.         } while (ch == 'Y'|| ch == 'y');               
  155.     }
  156. }

Unrolled Linked List Test
 
Enter array size of each node
5
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
23
 
Unrolled Linked List =
23
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
7
 
Unrolled Linked List =
23 7
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
87
 
Unrolled Linked List =
23 7 87
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
19
 
Unrolled Linked List =
23 7 87 19
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
24
 
Unrolled Linked List =
23 7 87 19 24
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
6
 
Unrolled Linked List =
23 7 87
19 24 6
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
94
 
Unrolled Linked List =
23 7 87
19 24 6 94
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
28
 
Unrolled Linked List =
23 7 87
19 24 6 94 28
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
5
 
Unrolled Linked List =
23 7 87
19 24 6
94 28 5
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
1
Enter integer element to insert
63
 
Unrolled Linked List =
23 7 87
19 24 6
94 28 5 63
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
3
Size = 10
 
 
Unrolled Linked List =
23 7 87
19 24 6
94 28 5 63
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
4
List Cleared
 
 
Unrolled Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
Unrolled Linked List Operations
 
1. insert
2. check empty
3. get size
4. clear
2
Empty status = true
 
Unrolled Linked List = empty
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.