Thursday 4 August 2011

Circular Linked List Program

#include<iostream>
using namespace std;
class cll
{
        struct std
        {
                int rno;
                char name[20];
                struct std *link;
        }*head;
        int size;
        public:
                cll()
                {
                        head=NULL;
                }
                void display();
                void insertbegin();
                void insertmiddle(int rno);
                void insertend();
                void deletebegin();
                void deletemiddle();
};
void cll::display()
{
        std *t;
        t=head;
        while(t->link!=head)
        {
                cout<<"\n name is: "<<t->name;
                cout<<"\n rno is: "<<t->rno;
                t=t->link;
        }
        cout<<"\n name is: "<<t->name;
        cout<<"\n rno is: "<<t->rno;
}
void cll::insertbegin()
{
        std *t;
        t=new std;
        cout<<"\n enter rno,name ";
        cin>>t->rno>>t->name;
        if(head==NULL)
        {
                t->link=t;
                head=t;
        }
        else
        {
                std *p=head;
                while(p->link!=head)
                        p=p->link;
                t->link=head;
                p->link=t;
                head=t;
        }
}
void cll::insertmiddle(int rno)
{
        std *t;
        t=new std;
        cout<<"\n enter rno&name ";
        cin>>t->rno>>t->name;
        if(head==NULL)
        {
                head=t;
                t->link=head;
        }
        else
        {
                std *p;
                p=head;
                while(p->rno!=rno && p->link!=head)
                        p=p->link;
                t->link=p->link;
                p->link=t;
        }
}
void cll::insertend()
{
        std *t;
        t=new std;
        cout<<"\n enter rno,name ";
        cin>>t->rno>>t->name;
        if(head==NULL)
        {
                t->link=t;
                head=t;
        }
        else
        {
                std *p=head;
                while(p->link!=head)
                        p=p->link;
                t->link=head;
                p->link=t;
        }
}
void cll::deletebegin()
{
        if(head==NULL)
        {
                cout<<"\n cll is empty ";
        }
        else
        if(head->link==head)
        {
                delete head;
                head==NULL;
        }
        else
        {
                std *p=head;
                while(p->link!=head)
                        p=p->link;
                p->link=head->link;
                std *t;
                t=head;
                head=head->link;
                delete t;
        }
}
void cll::deletemiddle()
{
        struct std *t,*p;
        p=head;
        int no;
        if(head==NULL)
        {
                cout<<"list is empty \n";
        }
        else if(head->link==head)
        {
                delete head;
                head=NULL;
        }
        else
        {
                cout<<"enter the rno you want to delete : ";
                cin>>no;
                while(p->link->rno!=no&p->link!=head)
                p=p->link;
                t=p->link;
                p->link=t->link;
                t->link=NULL;
                delete(t);
        }
}
int main()
{
        int option,rno,n=1;
        cll ob;
        while(n==1)
        {
                cout<<"\n enter your option: ";
                cout<<"\n 1) display \n 2) insertbegin \n 3) insertmiddle \n 4) insertend \n 5) deletebegin \n 6) deletemiddle \n 7) exit \n ";
                cin>>option;
                switch(option)
                {
                        case 1:
                                ob.display();
                                break;
                        case 2:
                                ob.insertbegin();
                                break;
                        case 3:
                                cout<<"\n enter the rno after which you want to insert: ";
                                cin>>rno;
                                ob.insertmiddle(rno);
                                break;
                        case 4:
                                ob.insertend();
                                break;
                        case 5:
                                ob.deletebegin();
                                break;
                        case 6:
                                ob.deletemiddle();
                                break;
                        case 7:
                                n=0;
                                break;
                }
        }
}

No comments:

Post a Comment