Circular Linked List in Java

Had to implement a circular linked list recently. Here’s my take, suggestions are welcome. The list can be parameterized for any type which has a meaningful equals method defined. The main method shows a sample usage of the Circular linked list with the String type. Only add, remove and size method are implemented.

also read:

The Circular Linked List class is created from scratch as well as uses its own Node class. It differs from the regular linked list because the last node points to the first node. CLL’s are like arrays but have some differences. This is an excellent class for college students learning java

package algo;

import java.util.NoSuchElementException;

public class CircularLinkedList<E> {
 private Entry<E> head;

 // Last element of the list. tail.next = head
 private Entry<E> tail;

 private int size = 0;

 /**
  * Class to hold the individual elements.
  * @param <E>
  */
 private static class Entry<E> {
  E element;

  Entry<E> next;

  Entry(E element, Entry<E> next) {
   this.element = element;
   this.next = next;
  }

This article is originally posted in Abhi Vuyyuru’s blog.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*