anurag
anurag

Reputation: 35

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 :-)

Upvotes: 2

Views: 879

Answers (2)

Rahul
Rahul

Reputation: 16335

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 .

Upvotes: 1

Philipp
Philipp

Reputation: 69663

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.

Upvotes: 3

Related Questions