my code stock.com

malithiim@gmail.com

Circular Linked List
by [email protected]

Snippet options

Download: Download snippet as circular-linked-list.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package circularlist;

class Node {

    Node left, right;
    int index;    
    
    String tag;

    public Node() {
    }

    public Node(int value) {
        this.index = value;
    }
}

public class CircularList {

    Node zero;
    int size=0;

    public CircularList() {
    }
    
    public CircularList(int size) {
        makeCircularList(size);
    }

    void add(Node n) {   //add a node to the right of zero th node 
        if (zero == null) {
            zero = n;
            zero.left = zero;
            zero.right = zero;
            size=1;
        } else {
            n.right = zero.right;
            n.left = zero;
            zero.right = n;
            n.right.left = n;
            size++;
        }

    }
    
    void add_one(){
        add(new Node(zero.right.index+1));
        size++;
    }

    Node getNode(int val) {   //gives the node whose 'index' equals to 'val'

        Node temp = zero;

        while (temp.index != val) {
            temp = temp.left;

            if (temp.equals(zero)) {
                return null;
            }
        }

        return temp;

    }

    boolean delNode(int val) {  //deletes the node whose 'index' is 'val'

        Node n = getNode(val);
        if (n != null) {
            n.right.left = n.left;
            n.left.right = n.right;
            
            if(n.equals(zero)) zero=n.left;
            
            size--;
            
            return true;
        } else {
            return false;
        }

    }

    final void makeCircularList(int size) {   //make a circular list of size 'size'
        if (zero==null) {
            for (int i = 0; i < size; i++) {
                add(new Node(i));
            }
        }

    }
    
    void circulate_clockwise(){  // print indexes of each node circulating clockwise direction
        if(zero!=null){
            Node temp=zero;
            System.out.print(temp.index+" ");
            temp=temp.left;
            while (!temp.equals(zero)) {
                System.out.print(temp.index+" ");
                temp=temp.left;
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) {
        
        System.out.println("Made circular list of 10 elements");
        CircularList cir=new CircularList(10);
        cir.circulate_clockwise();
        
        System.out.println("\nDeleted node 6");
        cir.delNode(6);
        cir.circulate_clockwise();
        
        System.out.println("\nDeleted node 0");
        cir.delNode(0);
        cir.circulate_clockwise();
        
        System.out.println("\nAdded node");
        cir.add_one();
        cir.circulate_clockwise();
        
        System.out.println("\nAdded node");
        cir.add_one();
        cir.circulate_clockwise();
        
        System.out.println("\nDeleted node 2");
        cir.delNode(2);
        cir.circulate_clockwise();
        
        System.out.println("\nAdded node");
        cir.add_one();
        cir.circulate_clockwise();
    }
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.