I'm trying to add a node in a circular list in Java. The problem is that my list has only a head (without a tail). Here is my code:
import java.io.PrintStream;
import java.util.EmptyStackException;
public class CircularList {
private Node head = null;
private class Node {
public Node(String payload, Node prev, Node next) {
super();
this.payload = payload;
this.prev = prev;
this.next = next;
}
private Node prev;
private Node next;
String payload = "";
}
public void push(String payload) {
Node n = new Node(payload, null, null);
if (isEmpty()) {
head = n;
n.next = n;
} else {
n.prev = head;
n.next = head;
}
}