Sunday 6 October 2013

Circular Linked List program in C++

//circular linked list

#include<iostream>

using namespace std;

#include<conio.h>

template<class T>

class node

{

     public:

              T data;

              node<T> *link;

};

template<class T>

class list

{

      private:

              node<T> *first;

      public:

      list();

      ~list();

      void createlist();

      void deletion();

      void insertion();

      void display();
      
};

template<class T>

list<T>::list()

{

               first=NULL;

}

template<class T>

list<T>::~list()

{

                node<T> *next;

                while(first)

                {

                            next=first->link;

                            delete first;

                            first=next;

                }

}

template<class T>

void list<T>::createlist()

{

                     T a;
                     
                     node<T> *cur,*ptr;
                     
                     first=NULL;

                     cout<<"\nenter data for new node:";

                     cin>>a;

                     while(a)

                     {

                             cur=new node<T>;

                             cur->data=a;

                             cur->link=NULL;

                             if(first==NULL)

                             first=cur;

                             else

                             ptr->link=cur;

                             ptr=cur;

                             cout<<"\nenter data for new node :";

                             cin>>a;

                     }

              ptr->link=first;   

}

template<class T>

void list<T>::insertion()

{
                node<T> *cur,*ptr,*exmp;

                 T ele;
                   
                 char ch; 
          
                 ptr=exmp=first;

                 cur=new node<T>;

                 cout<<"\n enter data for new node :";

                 cin>>cur->data;

                 cur->link=NULL;

                 cout<<"\n do u want to insert first [y/n]:";

                 cin>>ch;

                 if(ch=='Y'||ch=='y')

                 {
                                     while(ptr->link!=first)

                                     {
                                                          
                                                          ptr=ptr->link;
                                                          
                                     }
                 
                                     cur->link=first;
                                     
                                     first=cur;
                                     
                                     ptr->link=first;
                 }
                 
                 else  
                 
                 {

                     cout<<"\n after which element do u want to insert :";

                     cin>>ele;

                     do
                     
                     {
                              
                              if(exmp->data==ele)
                              
                              {
                                                 
                                                 cur->link=exmp->link;
                               
                                                 exmp->link=cur;
                              
                              }
                              
                              exmp=exmp->link;
                              
                     }while(exmp!=first);

                 }

}

template<class T>

void list<T>::deletion()

{

                     T ele;

                     char ch;

                     node<T> *ptr,*ptr1;

                     ptr=ptr1=first;
                 
                     cout<<"\ndo u want to delete first element [y/n]:";

                     cin>>ch;

                     if(ch=='y'||ch=='Y')

                     {

                            while(ptr->link!=first)
                            
                            {
                                                   ptr=ptr->link;
                                                   
                            }
                            
                            first=ptr1->link;
                            
                            ptr->link=first;
                            
                      }

                     else

                     {

                         cout<<"\nwhich element do u want to delete :";

                         cin>>ele;

                         do
                         
                         {
                                  
                                  if(ptr1->link->data==ele)
                                  
                                  {
                                                           
                                                           ptr1->link=ptr1->link->link;
                                                           
                                                           break;
                                                           
                                  }
                                  
                                  ptr1=ptr1->link;
                                  
                         }while(ptr1!=first);
                    
                      }

}

template<class T>

void list<T>::display()

{

     node<T> *ptr;
     
     ptr=first;
     
     do
     
     {
             cout<<ptr->data<<"---->";
             
             ptr=ptr->link;  
               
     }while(ptr!=first);

}

int main()

{

    int n;

    list <int> l;

    l.createlist();

    do

    {

                   cout<<"\n 1.insertion \n2.deletion \n3.display \n4.exit \n";

                   cout<<"\n enter ur option : ";

                   cin>>n;

                   switch(n)

                   {

                            case 1: l.insertion();

                            break;

                            case 2: l.deletion();

                            break;

                            case 3: l.display();

                            break;

                            case 4:

                            break;

                   }

    }while(n<=4);
    
    _getch();

    return 0;

}

No comments:

Post a Comment