Circular Singly Linked List using Java Please review my implementation of a Circular Singly Linked List.

public class NodeS {

    public int num;
    public NodeS next;

    public NodeS(int n){
        this.num = n;
    }
}


public class SinglyCircularList {

    private static NodeS head = null;
    private static NodeS tail = null;
    private static int size = 0;

    public static int getSize() {
        return size;
    }

    public static void insert(int n) {
        NodeS node = new NodeS(n);
        node.next = tail;
        if (tail == null) {
            tail = node;
        } else {
            head.next = node;
        }
        head = node;
        size++;
    }

    public static int delete() {
        if (!isEmpty()) {
            NodeS deq = tail;
            tail = deq.next;
            size--;
            if (size == 0) {
                tail = null;
            }
            head.next = tail;
            return deq.num;
        } else {
            System.out.println("List Empty !!");
        }
        return -1;
    }

    public static boolean isEmpty() {
        return size == 0;
    }

    public void printList() {
        NodeS temp = tail;
        for (int i = 0; i &lt;= size; i++) {
            if (temp != null) {
                System.out.print(temp.num);
                temp = temp.next;
            }
        }
        System.out.println();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SinglyCircularList scl = new SinglyCircularList();
        scl.insert(1);
        scl.insert(2);
        scl.insert(3);
        scl.insert(4);
        scl.insert(5);
        scl.insert(6);
        scl.insert(7);
        scl.printList();
        System.out.println("Del-&gt;" + scl.delete());
        scl.insert(8);
        scl.printList();
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        System.out.println("Del-&gt;" + scl.delete());
        scl.printList();
        scl.insert(9);
        scl.insert(10);
        scl.printList();

    }

}
I have only a couple of points:

1

You implementation will be able to store only one list throughout your Java program:

SinglyCircularList list1 = new SinglyCircularList();
SinglyCircularList list2 = new SinglyCircularList();

list1.insert(1); // Here list1 = [1], and list2 = [1];
list2.insert(2); // Now  list1 = [1, 2], and list2 = [1, 2]


So, basically, you should remove the keyword static from everywhere except the main(String[] args).

2

It would be nicer if your delete method would throw an exception on deleting from an empty list.

3

Instead of printList() you could override the public String toString().

4

private static NodeS head = null;
private static NodeS tail = null;
private static int size = 0;


Whenever declaring class or object fields, references are initialized by default with null, and numeric fields to zero. You can write simply:

private NodeS head;
private NodeS tail;
private int size;


Summa summarum

I had something like that in mind:

import java.util.NoSuchElementException;

public class SinglyCircularList&lt;E&gt; {

    private static final class Node&lt;E&gt; {

        private final E datum;
        private Node&lt;E&gt; next;

        Node(final E datum) {
            this.datum = datum;
        }

        E getDatum() {
            return datum;
        }   

        Node&lt;E&gt; getNext() {
            return next;
        }

        void setNext(final Node&lt;E&gt; next) {
            this.next = next;
        }
    }

    private Node&lt;E&gt; head;
    private Node&lt;E&gt; tail;
    private int size;

    public int size() {
        return size;
    }

    public void insert(final E datum) {
        final Node&lt;E&gt; newnode = new Node&lt;&gt;(datum);

        if (head == null) {
            head = newnode;
            tail = newnode;
            size = 1;
            return;
        }

        tail.setNext(newnode);
        tail = newnode;
        size++;
    }

    public E delete() {
        if (isEmpty()) {
            throw new NoSuchElementException("Deleting from an empty list.");
        }

        final E ret = head.getDatum();

        if (size == 1) {
            head = null;
            tail = null;
        } else {
            head = head.getNext();
            tail.setNext(head);
        }

        size--;
        return ret;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("[");

        if (size &gt; 0) {
            sb.append(head.getDatum());
        } else {
            return "[]";
        }

        Node&lt;E&gt; currentNode = head.getNext();

        for (int i = 1; i &lt; size; ++i, currentNode = currentNode.getNext()) {
            sb.append(", ").append(currentNode.getDatum());
        }

        return sb.append("]").toString();
    }

    public static void main(String[] args) {
        SinglyCircularList&lt;Integer&gt; scl = new SinglyCircularList&lt;&gt;();

        System.out.println("Creating the list:");

        for (int i = 1; i &lt;= 8; ++i) {
            System.out.println(scl);
            scl.insert(i);
        }

        System.out.println(scl);

        System.out.println("Removing from the list:");

        while (!scl.isEmpty()) {
            scl.delete();
            System.out.println(scl);
        }
    }
}


Hope that helps.