Skip to content

Create Unrolled Linked List #5835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class UnrolledLinkedList {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also very useful to have multiple Junit tests.

private Node head;
private int nodeCapacity; // Maximum capacity of each node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be better move it to javadoc


// Constructor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant comment. Will be better to remove it.

public UnrolledLinkedList(int capacity) {
head = null;
nodeCapacity = capacity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add checking for wrong input.

public UnrolledLinkedList(int capacity) {
    if (capacity <= 0) {
        throw new IllegalArgumentException("Node capacity must be positive");
    }
    this.head = null;
    this.nodeCapacity = capacity;
}

}

// Insert an element at the end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also, will be better to have like javadoc

public void insert(int value) {
if (head == null) {
head = new Node(nodeCapacity);
head.data[0] = value;
head.count = 1;
} else {
Node current = head;
while (current != null) {
if (current.count < nodeCapacity) {
current.data[current.count] = value;
current.count++;
return;
}
current = current.next;
}

// If we reach here, all nodes are full, so create a new node
Node newNode = new Node(nodeCapacity);
newNode.data[0] = value;
newNode.count = 1;

Node tail = head;
while (tail.next != null) {
tail = tail.next;
}
tail.next = newNode;
}
}

// Display the unrolled linked list
public void display() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant method because usually standard interface for List data structure not include it. You can discover standard java List interface.

Node current = head;
while (current != null) {
for (int i = 0; i < current.count; i++) {
System.out.print(current.data[i] + " ");
}
current = current.next;
}
System.out.println();
}
}