Wednesday 27 June 2012

java program to create and maintian circular linked list.

import java.io.*;
class Node
{    private Node next,prev;
    private int data;
    public Node(int data)
    {    this.data=data;
        next=null;
    }
   
    public void setNext(Node next)
    {
        this.next=next;
    }



    public Node getNext()
    {
        return next;
    }
    public void setPrev(Node prev)
    {
        this.prev=prev;
    }
    public Node getPrev()
    {
        return prev;
    }
    public void setData(int data)
    {
    this.data=data;
    }
    public int  getData()
    {
        return data;
    }
}

class LinkList
{    private Node head,tail;
    public LinkList( )
    {    head=null;
        tail=null;
    }
    public void insertAppend(int data)
    {
        Node newNode=new Node(data);
            if(head==null)
            {
                head=tail=newNode;
                tail.setNext(head);
            }
            else
            {    tail.setNext(newNode);
                newNode.setPrev(tail);
                tail=newNode;
                tail.setNext(head);
            }
    }
    public int count()
    {    int i=0;
        Node trav=head;
            while(trav != null)
            {
            trav=trav.getNext();   
            i++;
            }   
        return i;   
       
    }
    public void insertPosition(int data,int pos)
    {    Node newNode=new Node(data);
        int c=count();
            if(pos > c+1)
                System.out.println("empty");
            else if(pos==1)
            {
                Node t=head;
                head=newNode;
                head.setNext(t);
                newNode.setPrev(head);
            }
            else
            {        int i=1;
                    Node trav=head;
                    while(i < pos-1)   
                    {
                    trav=trav.getNext();
                    i++;
                    }
                if(trav.getNext()==null)
                {
                    newNode.setPrev(trav);
                    trav.setNext(newNode);
                }
                else
                {
                    newNode.setNext(trav.getNext());
                    trav.getNext().setPrev(newNode);
                    trav.setNext(newNode);
                    newNode.setPrev(trav);
                }
            }
    }
    public void deleteLast()
    {        if(head==null)
                System.out.println("Empty ");   
            else
            {
            Node trav=head;
            while(trav.getNext() !=null)
                trav=trav.getNext();
                       
            trav.getPrev().setNext(null);
            }
           
    }
    public void deletePosition(int pos)
    {   
        int c=count();
        int i=1;   
            if(pos > c)
            {
            System.out.println("not possible");
            return ;
            }
           
            else if(head==null)
            {
            System.out.println("empty");
            }
            else if(pos==1)
            {
            Node t=head.getNext();
            head=t;
            }
            else
            {
           
            Node trav=head;
           
            while(i<pos-1)   
                {
                trav=trav.getNext();
                i++;
                }
            if(trav.getNext().getNext()==null)
           
                trav.setNext(null);
            else
                trav.setNext(trav.getNext().getNext());   
           
            }
           
    }
    public void display()
    {
        System.out.println("LinkList is: ");
       
        Node temp=head;
        if(head==null)
        {
            System.out.println("Empty ");
        }
        else
        {
            while(temp.getNext() !=null)
            {
                System.out.print(temp.getData() +"<-->");
                temp=temp.getNext();
            }
            System.out.println(temp.getData());
        }
    }
}

class CLinkMain
{
    public static void main(String args[]) throws IOException
    {   
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        LinkList l1=new LinkList();
        int d,p;
        int c;
        do
        {
            System.out.println("Menu");
            System.out.println("1.insert append ");
            System.out.println("2.Delete Last");
            System.out.println("3.Display ");
            System.out.println("4.insert By position ");
            System.out.println("5.Delete By position ");
            System.out.println("6.Exit ");
            System.out.println("enter your choice:");
            c=Integer.parseInt(br.readLine());
            switch(c)
            {
                case 1:
                    System.out.println("enter your data:");
                    d=Integer.parseInt(br.readLine());
                    l1.insertAppend(d);
                    break;
                case 2:    l1.deleteLast();
                    break;
                case 3:    l1.display();
                    break;
                case 4:    System.out.println("enter your data:");
                    d=Integer.parseInt(br.readLine());   
                    System.out.println("enter your position:");
                    p=Integer.parseInt(br.readLine());
                    l1.insertPosition(d,p);
                     break;
                case 5:     System.out.println("enter your position to delete:");
                    p=Integer.parseInt(br.readLine());
                    l1.deletePosition(p);
                    break;
                case 6: break;
            }
        }while(c !=6);
    }   
}

No comments:

Post a Comment