In: Computer Science
Java program to implement circular linked list.
public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /* move tail to the next node */ public void rotate() { } /* add element to the first of the linked list increase the size */ public void addFirst(E e) { } /* add element to the end of the linked list increase size */ public void addLast(E e) { } /* take out the first element decrease the size return first element or null */ public E removeFirst() { return null; }
------------------------------------------------------------------------------------------------------
implements
public class Node { // Instance variables: //element = data private E element; private Node next; /** Creates a node with null references to its element and next node. */ public Node() { this(null, null); } /** Creates a node with the given element and next node. */ public Node(E e, Node n) { element = e; next = n; } // Accessor methods: public E getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(E newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } }
public class CircularLinkedList {
public int size =0;
public Node head=null;
public Node tail=null;
//add a new node at the start of the linked list
public void addNodeAtStart(int data){
System.out.println("Adding node " + data + " at start");
Node n = new Node(data);
if(size==0){
head = n;
tail = n;
n.next = head;
}else{
Node temp = head;
n.next = temp;
head = n;
tail.next = head;
}
size++;
}
public void addNodeAtEnd(int data){
if(size==0){
addNodeAtStart(data);
}else{
Node n = new Node(data);
tail.next =n;
tail=n;
tail.next = head;
size++;
}
System.out.println("\nNode " + data + " is added at the end of the
list");
}
public void deleteNodeFromStart(){
if(size==0){
System.out.println("\nList is Empty");
}else{
System.out.println("\ndeleting node " + head.data + " from
start");
head = head.next;
tail.next=head;
size--;
}
}
public int elementAt(int index){
if(index>size){
return -1;
}
Node n = head;
while(index-1!=0){
n=n.next;
index--;
}
return n.data;
}
//print the linked list
public void print(){
System.out.print("Circular Linked List:");
Node temp = head;
if(size<=0){
System.out.print("List is empty");
}else{
do {
System.out.print(" " + temp.data);
temp = temp.next;
}
while(temp!=head);
}
System.out.println();
}
//get Size
public int getSize(){
return size;
}
public static void main(String[] args) {
CircularLinkedList c = new CircularLinkedList();
c.addNodeAtStart(3);
c.addNodeAtStart(2);
c.addNodeAtStart(1);
c.print();
c.deleteNodeFromStart();
c.print();
c.addNodeAtEnd(4);
c.print();
System.out.println("Size of linked list: "+ c.getSize());
System.out.println("Element at 2nd position: "+
c.elementAt(2));
}
}
class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}