Can I use java.util.LinkedList to build a circular / circular linked list?

I would like to create a circular / circular linked list in which the tail of the list will return to the top of the list. Can I use java.util.LinkedList and change the tail of the node after creating the list to make it circular / circular? If so, can you show me some code on how this will happen?

If I cannot use java.util.LinkedList , how can I create my own implementation of circular / circular linked lists? Can you show me skeletons about how this implementation will look?

Let me know if you need more information and I will eliminate any confusion.

+6
java linked-list data-structures circular-list
source share
4 answers
 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; } } 
+18
source share

For practical use (for example, not only for games or training), I personally would prefer the Guava Iterables.cycle method - see http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect /Iterables.html#cycle%28java.lang.Iterable%29 .

+10
source share

java.util.LinkedList is one of the Collections data types. The purpose of the Collections is to provide service structures without stopping the programmer from worrying about his internal implementation. If you have internal functions that work in a certain way, and java.util does not guarantee that they work, then they are not for you.

To implement a circular linked list, first create a ListNode class:

 class ListNode { ListNode next; ListNode prev; Object data; } 

Then save the ListNode head and make sure that prev of head points to the "end" of the list and next "end" points to head . Honestly, there is a slight difference between a bidirectional list containing a pointer to a tail and a circular linked list.

+2
source share

Contact here for a comprehensive implementation of Single Linked List in Java. Making it circular is just setting up this code.

+1
source share

All Articles