I would like to create a circular/cyclic linked list where the tail of the list would point back to the head of the list. So can I use java.util.LinkedList
and modify the tail node after creation of the list to make it circular/cyclic? If so, can you show me some code on how that would happen?
If I can't use java.util.LinkedList
, how should I create my own circular/cyclic linked list implementation? Can you show me the skeletons of how this implementation would look?
Let me know if you need more details and I'll clear up any confusion.
Similarly, the complete list can be traversed starting with any node. The queue data structure implementation in Java uses a circular linked list as its internal implementation.
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list, the first node and the last node are connected to each other which forms a circle. There is no NULL at the end.
To implement a circular singly linked list, we take an external pointer that points to the last node of the list. If we have a pointer last pointing to the last node, then last -> next will point to the first node. The pointer last points to node Z and last -> next points to node P.
5. Which of the following application makes use of a circular linked list? Explanation: Generally, round robin fashion is employed to allocate CPU time to resources which makes use of the circular linked list data structure. Recursive function calls use stack data structure.
For practical application (e.g. not only playing around or learning) I would personally prefer Guava's Iterables.cycle
method - see Iterables.cycle
class ListNode {
public ListNode next;
public Object data;
public ListNode(Object data, ListNode next) {
this.next = next;
this.data = data;
}
}
class CircularLinkedList {
private ListNode head = null;
private int numberOfElements = 0;
private ListNode actualElement = null;
private int index = 0;
public boolean isEmpty() {
return (numberOfElements == 0);
}
public int getNumberOfElements() {
return numberOfElements;
}
public void insertFirst(Object data) {
if (!(isEmpty())) {
index++;
}
ListNode listNode = new ListNode(data, head);
head = listNode;
numberOfElements++;
}
public void insertAfterActual(Object data) {
ListNode listNode = new ListNode(data, actualElement.next);
actualElement.next = listNode;
numberOfElements++;
}
public boolean deleteFirst() {
if (isEmpty())
return false;
if (index > 0)
index--;
head = head.next;
numberOfElements--;
return true;
}
public boolean deleteActualElement() {
if (index > 0) {
numberOfElements--;
index--;
ListNode listNode = head;
while (listNode.next.equals(actualElement) == false)
listNode = listNode.next;
listNode.next = actualElement.next;
actualElement = listNode;
return true;
}
else {
actualElement = head.next;
index = 0;
return deleteFirst();
}
}
public boolean goToNextElement() {
if (isEmpty())
return false;
index = (index + 1) % numberOfElements;
if (index == 0)
actualElement = head;
else
actualElement = actualElement.next;
return true;
}
public Object getActualElementData() {
return actualElement.data;
}
public void setActualElementData(Object data) {
actualElement.data = data;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With