Thursday, August 29, 2013

C. IMPLEMENTATION OF CLL


/* Circular Linked List( CLL ) */

/**********************************************
   NAME                       :-  JABIR DAUD PATHAN
PROGRAM              :-  IMPLEMENTATION OF CLL
**********************************************/

/*------------<<  Header File  >>------------*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct node
 {
  int data;
  struct node*next;
 };

/*-------<<  Declaration of class cll >>--------*/

class cll
 {
  private:
                  node*head;
  public :
                 cll()   // Default constructor
                  {
                   head=NULL;
                  }
                 void create();            // [ Functions
                 void display();           //   declaraton ]
                 void insert_beginning();
                 void insert_between();
                 void insert_end();
                 void delete_beginning();
                 void delete_between();
                 void delete_end();
                 void search();

 };

/*-------<< Function defination for create cll >>-------*/

void cll :: create()
 {
   node*p;
   int n,i,j;

   cout<<"\nEnter the no of nodes :";
   cin>>n;
   for(i=0;i<n;i++)
    {
      cout<<"\n\nEnter values of node==>";
      cin>>j;
      if(head==NULL)
       {
         head=new node;
         head->data=j;
         head->next=head;
       }
      else
       {
         p=new node;
         p->data=j;
         p->next=head->next;
         head->next=p;
         head=p;
       }
    }
  }

/*-------<< Function defination for display cll >>-------*/

void cll :: display()
 {
  node*p;
  cout<<"\n\nCreated link list : ";
  p=head->next;
  do
   {
     cout<<" "<<p->data;
     p=p->next;
   }while(head->next!=p);
 }

/*---<< Function defination for inserting node at beginning >>---*/

void cll :: insert_beginning()
 {
  node*p;
  p=new node;
  p->next=NULL;
  cout<<"\nEnter the value to be inserted==>";
  cin>>p->data;
  if(head==NULL)
     head=p;
  else
   {
     p->next=head->next;
     head->next=p;
   }
 }

/*---<< Function defination for inserting node in between >>----*/

void cll :: insert_between()
 {
  node*p,*temp;
  int j;
  p=new node;
  p->next=NULL;
  cout<<"\nEnter the value to be inserted==>";
  cin>>p->data;
  cout<<"\nEnter the value after which you want to insert new value==>";
  cin>>j;

  if(head==NULL)
      head=p;
  else
   {
    temp=head;
    while(temp!=NULL&&temp->data!=j)
       temp=temp->next;

    p->next=temp->next;
    temp->next=p;
   }
 }

/*----<< Function defination for inserting node at end >>-----*/

void cll :: insert_end()
 {
  node*p;
  p=new node;
  p->next=NULL;
  cout<<"\nEnter the value to be inserted==>";
  cin>>p->data;
  if(head==NULL)
     head=p;
  else
   {
     p->next=head->next;
     head->next=p;
     head=p;
   }
 }

/*----<< Function defination for deleting beginning node >>-----*/

void cll::delete_beginning()
  {
            node *p;
            if(head->next==head)
              {
                        delete head;
                        head=NULL;
              }
            else
              {
                        p=head->next;
                        head->next=p->next;
                        delete p;
              }
  }

/*---<< Function defination for deleting any node from between >>---*/

void cll :: delete_between()
 {
  node *p,*q;
  int x;
  cout<<"\nEnter the data to be deleted : ";
  cin>>x;
  if(head->next->data==x)
     {
       p=head->next;
       head->next=p->next;
       delete p;
     }
  else
   {
    q=head->next;
    while(q->next->data!=x && q!=head)
     {
       q=q->next;
     }
     p=q->next;
     q->next=p->next;
     delete p;
   }
}

/*-----<<  Function defination for deleting end node >>-----*/

void cll :: delete_end()
 {
  node*p;
  if(head->next==head)
   {
     delete head;
     head=NULL;
   }
  else
   {
    p=head->next;
    while(p->next!=head)
      {
               p=p->next;
      }
    p->next=head->next;
    delete head;
    head=p;
  }
}

/*-----<<  Function defination for search any node >>------*/

void cll :: search()
{
  node *p;
  int m,cnt=1;
  cout<<"\n\nEnter the data to be searched: ";
  cin>>m;
  p=head->next;
  do
   {
    if(p->data==m)
     {
       cout<<"\n\tFound at location "<<cnt;
       break;
     }
    cnt++;
    p=p->next;
   }while(head->next!=p);
 }

/*-------------<<  Main Function Starts  >>------------*/

void main()
 {
   cll p;
   char ans;
   int ch;
do
 {
  clrscr();
  cout<<"\n\t\t\t---<< Operations on CLL >>---";
  cout<<"\n\n\t\t\t\t1.Create";
  cout<<"\n\t\t\t\t2.Display";
  cout<<"\n\t\t\t\t3.Insert";
  cout<<"\n\t\t\t\t4.Delete";
  cout<<"\n\t\t\t\t5.Search";
  cout<<"\n\t\t\t\t6.Exit";
  cout<<"\n\n\t\t\t-----------------------------";

  cout<<"\n\n\t\t\tEnter your choice : ";
  cin>>ch;

  switch(ch)
   {
    case 1:
                   p.create();
                   break;
    case 2:
                   p.display();
                   break;
    case 3:
                   p.display();
                   do
                          {
                           cout<<"\n\n\tInsert:";
                           cout<<"\n\t\t1.Beginning";
                           cout<<"\n\t\t2.In Between";
                           cout<<"\n\t\t3.End";
                           cout<<"\n\n\t\tEnter your choice : ";
                           cin>>ch;
                           switch(ch)
                             {
                              case 1:
                                                    p.insert_beginning();
                                                    p.display();
                                                    break;

                              case 2:
                                                    p.insert_between();
                                                    p.display();
                                                    break;

                              case 3:
                                                    p.insert_end();
                                                    p.display();
                                                    break;
                            }
                             cout<<"\n\n\n\t\tDo you want to continue(Y/N)? :";
                             ans=getche();
                         }while(ans=='Y'||ans=='y');
                          break;

     case 4:
                   p.display();
                   do
                          {
                           cout<<"\n\n\tDelete:";
                           cout<<"\n\t\t1.Beginning";
                           cout<<"\n\t\t2.In Between";
                           cout<<"\n\t\t3.End";
                           cout<<"\n\n\t\tEnter your choice : ";
                           cin>>ch;
                           switch(ch)
                             {
                              case 1:
                                                    p.delete_beginning();
                                                    p.display();
                                                    break;

                              case 2:
                                                    p.delete_between();
                                                    p.display();
                                                    break;

                              case 3:
                                                    p.delete_end();
                                                    p.display();
                                                    break;
                             }
                             cout<<"\n\n\n\t\tDo you want to continue(Y/N)? :";
                             ans=getche();
                         }while(ans=='Y'||ans=='y');
                          break;

     case 5:
                    p.display();
                    p.search();
                    break;

     case 6:
                    exit(0);
                    break;
   }
   cout<<"\n\n\n\t\tDo you want to continue(Y/N)? :";
   ans=getche();
  }while(ans=='Y'||ans=='y');
   getch();
 }
/*-------------<<  End Of Main Function  >>---------------*/





/*-----------------<< OUTPUT SCREEN  >>-------------------*/


                        ---<< Operations on CLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Exit

                        -----------------------------

                        Enter your choice : 1

Enter the no of nodes :3

Enter values of node==>10

Enter values of node==>15

Enter values of node==>20


                Do you want to continue(Y/N)? :y

                        ---<< Operations on CLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Exit

                        -----------------------------

                        Enter your choice : 2


Created link list :  10 15 20

                Do you want to continue(Y/N)? :y




                        ---<< Operations on CLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Exit

                        -----------------------------

                        Enter your choice : 3


Created link list :  10 15 20

        Insert:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 1

Enter the value to be inserted==>5


Created link list :  5 10 15 20

                Do you want to continue(Y/N)? :y

        Insert:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 2

Enter the value to be inserted==>7

Enter the value after which you want to insert new value==>5


Created link list :  5 7 10 15 20


                Do you want to continue(Y/N)? :y

        Insert:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 3

Enter the value to be inserted==>25


Created link list :  5 7 10 15 20 25


                Do you want to continue(Y/N)? :n

                Do you want to continue(Y/N)? :y


                        ---<< Operations on CLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Exit

                        -----------------------------

                        Enter your choice : 4


Created link list :  5 7 10 15 20 25

        Delete:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 1


Created link list :  7 10 15 20 25


                Do you want to continue(Y/N)? :y

        Delete:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 2

Enter the data to be deleted : 15


Created link list :  7 10 20 25


                Do you want to continue(Y/N)? :y


        Delete:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 3


Created link list :  7 10 20


                Do you want to continue(Y/N)? :n

                Do you want to continue(Y/N)? :y


                        ---<< Operations on CLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Exit

                        -----------------------------

                        Enter your choice : 5


Created link list :  7 10 20

Enter the data to be searched: 20

        Found at location 3


                Do you want to continue(Y/N)? :y


                        ---<< Operations on CLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Exit

                        -----------------------------

                        Enter your choice : 6


/**************************************************************/

No comments:

Post a Comment