简体   繁体   中英

Converting Linked List to a Circular Linked List in JAVA

Is there any way to convert an object of LinkedList class to a circular linked list.

Or is there any predefined class like CircularLinkedList in java.util Some thing like this (some thing like http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html )

Any help would be really appreciated....

Thanks in Advance :-)

No, the LinkedList is encapsulated in a way which makes it impossible to connect its tail to its head. I don't think that any of the default collections supports that, because then it wouldn't fulfill the contract for Iterable anymore, which says that an iterator got to get to the end sometime.

When you need a data structure like that, you have to implement it yourself.

Have a look at Guava's Iterables.cycle method.

public static <T> Iterable<T> cycle(Iterable<T> iterable)

Returns an iterable whose iterators cycle indefinitely over the elements of iterable.

That iterator supports remove() if iterable.iterator() does. After remove() is called, subsequent cycles omit the removed element, which is no longer in iterable. The iterator's hasNext() method returns true until iterable is empty.

Refer doc : http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html#cycle%28java.lang.Iterable%29 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM