using System;
public class UnrollNode {
public UnrollNode next;
public int num_elements;
public int[] array;
public UnrollNode(int n)
{
next = null;
num_elements = 0;
array = new int[n];
}
}
public class UnrollLinkList {
private UnrollNode start_pos;
private UnrollNode end_pos;
int size_node;
int nNode;
public UnrollLinkList(int capacity)
{
start_pos = null;
end_pos = null;
nNode = 0;
size_node = capacity + 1;
}
public void Insert(int num)
{
nNode++;
if (start_pos == null) {
start_pos = new UnrollNode(size_node);
start_pos.array[0] = num;
start_pos.num_elements++;
end_pos = start_pos;
return;
}
if (end_pos.num_elements + 1 < size_node) {
end_pos.array[end_pos.num_elements] = num;
end_pos.num_elements++;
}
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;
}
}
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();
}
}
public class UnrolledLinkedList_Check {
public static void Main(String[] args)
{
Random rand = new Random();
UnrollLinkList ull = new UnrollLinkList(5);
for (int i = 0; i < 12; i++) {
int rand_int1 = rand.Next(100);
Console.WriteLine("Entered Element is " + rand_int1);
ull.Insert(rand_int1);
ull.display();
}
}
}