-
Notifications
You must be signed in to change notification settings - Fork 19.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class UnrolledLinkedList { | ||
private Node head; | ||
private int nodeCapacity; // Maximum capacity of each node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be better move it to javadoc |
||
|
||
// Constructor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add checking for wrong input.
|
||
} | ||
|
||
// Insert an element at the end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant method because usually standard interface for |
||
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(); | ||
} | ||
} |
There was a problem hiding this comment.
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.