add

Thursday, 9 February 2017

c++ circular link list source code

 C++ Circular link List source code

Dear readers  this code is simplest form of  circular link list, copy paste the code in your  IDE then you try to understand if you get any trouble to understanding the code comment me I give you answer for make you understand.

#include<iostream>
#include <stdlib.h>
using namespace std;
void welcome();
void welcome()
{
 system("color 1A");
 cout<<"\n\n\n";
 cout<<"\t\t\t\t\t*******************************************   W E L L    C O M E    ******************************************"<<endl;
 cout<<"\t\t\t\t\t|!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!|"<<endl;
 cout<<"\t\t\t\t\t|                                              | title |                                           |"<<endl;
 cout<<"\t\t\t\t\t|                                             *------------------*                                           |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|    Topic    :_)                   C I R C U L A R        L I N K E D      L I S T                          |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                ( STUDENT INFORMATION )                                     |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                    XXXXXXXXX                                         |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                   REG NO:     XXXXXX                                        |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                    SEMISER       xxxxxxx                                        |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                    DATE          10-02-017                                |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                    S U B M I T T E D       T O     xxxxxxxxxxxxxxxxxxxx                    |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t**************************************************************************************************************"<<endl;
  cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t";
 system("pause");
  system("cls");


}



      class  list
         {
         struct node
         {
         int values;
         node *next;
         };
                               
                               
          public:
         list()
           {
           head=NULL;
           }
         
                                  
          void create(int data)
            {
            node *temp,*temp1;
            if(head==NULL)
               {
               temp=new node;
               temp->values=data;
               head=temp;
               temp->next=head;
               }
           else
               {
            temp=head;
            while(temp->next!=head)
               temp=temp->next;
               temp1=new node;
               temp1->values=data;
               temp1->next=head;
               temp->next=temp1;
             }
              }
             
             
             
             
          void beg_insert(int data)
               {
                node *temp,*temp1;
                 temp=new node;
                 temp1=head;
              if(temp1==NULL)
                cout<<"\n Firstely create using 1 , list not exist";
              else
                 {
                 while(temp1->next!=head)
                   temp1=temp1->next;

                 temp->values=data;
                 temp->next=head;
                 head=temp;
                 temp1->next=head;
                }

                }
               
               
               
            void in_insert(int pos,int data)
                  {
                  node *temp,*t;
                  int a;
                  a=pos;
                  temp=head;
                  while(pos>1)
                {
                temp=temp->next;
                pos--;
                }
               if(temp==NULL)
                   cout<<"\n\t firstely create using 1 , List not exist ";
              else  if(a==0)
                beg_insert(data);

                 else
                   {
                   t=new node;
                   t->values=data;
                   t->next=temp->next;
                   temp->next=t;
                   }
                }
               
               
               
               
               
            void del( )
            {
            node *temp,*t;
            temp=head;
            if(temp==NULL)
              cout<<"\n \tList not Exists ";
            else if(temp->next==head)
                {
                delete temp;
                head=NULL;
                }
            else
              {
              t=head->next;
              while(t->next!=head)
                 {
                 t=t->next;
                 temp=temp->next;
                 }
               // t=temp->next->next;
                temp->next=head;
                delete t;
                }
             }


              void beg_del()
             {
              node *temp,*temp1;
              temp=head;
              if(head==NULL)
                cout<<"\n\t Link list has no nodes";
              else
               {
                temp1=head;
                while(temp1->next!=head)
                   temp1=temp1->next;
              head=temp->next;
              temp1->next=head;
              delete temp;
               }
              }
             
             
              void mid_del(int pos)
              {
              node *temp,*t;
                int a=pos;
                temp=head;
               while(pos>1)
                  {
                  temp=temp->next;
                   pos--;
                   }
             if(a==1 )
                   beg_del();

             else  if(temp==NULL)
                 cout<<"\n\t List not Exist ..";
              else
               {
               t=temp->next;
               temp->next=temp->next->next;
               delete t;
               }
              }
             
             
             
             
             

            void show()
              {
              node *temp;
              temp=head;
              cout<<"\n\t";
              if(temp==NULL)
             cout<<"\n \tList not exist";
              cout<<temp->values;
              temp=temp->next;
              while(temp!=head)
                 {
                 cout<<" psition no"<<" "<<temp->values;
                 temp=temp->next;
                 }
               }
              
              
              
                private:
                                node *head;
             };
            
            
int main() {
               
               
               
                system("mode 1000");
    system("color 4E");
    welcome();
    list l;
            int choice,position;
            while(1)
              {
              cout<<"\n\t\t\t\t\t    | E N T E R     Y O U R      C H O I S E |"<<endl;
              cout<<"\n\t\t\t\t\t    *----------------------------------------*"<<endl;
              cout<<"\n\n\n\t\t\t\t\t 1: To create a  Node";
                                                  cout<<"\n\t\t\t\t\t 2: To insert at Begining";
                                                  cout<<"\n\t\t\t\t\t 3: To insert at any position";
              cout<<"\n\t\t\t\t\t 4: To delete";
                                                  cout<<"\n\t\t\t\t\t 5: To delete at begining";
                                                  cout<<"\n\t\t\t\t\t 6: To Delete at any position ";
              cout<<"\n\t\t\t\t\t 7: To Display";
                                                  cout<<"\n\t\t\t\t\t 8: To exit";
              cout<<"\n\n";
              cin>>choice;
              if(choice==1)
              {
              cout<<"\n\t Enter data to Node: ";
              cin>>choice;
              l.create(choice);
              }
             else if(choice==2)
               {
               cout<<"\n\t Enter element to insert at begining : " ;
               cin>>choice;
               l.beg_insert(choice);
               }
              else if(choice==3)
            {
            cout<<"\nEnter element to insert at specific position  :  ";
            cin>>choice;
            cout<<"\n Enter position  : ";
            cin>>position;
               l.in_insert(position,choice);
               }
          else if(choice==4)
             l.del();
          else if (choice==5)
            l.beg_del();
          else if(choice==6)
          {
            cout<<"\n Enter position where u have to delete";
            cin>>position;
            l.mid_del(position);
              }
           else if(choice==7)
               l.show();
             
            else if(choice==8)
              {
              cout<<"\n Press Enter to Exit";
              break;
              }
             else
               cout<<"\n\t Wrong Choice   ";
            }
  
    system("pause");

}

0 comments:

Post a Comment