Monday, October 20, 2014

Circular Dubly Linked List in C++

#include <iostream>
using namespace std;

class node 
{
   int value;
   node *head;
   node *tail;
   node *next;
   node *previous;
   node *current;
public:
  
   node()
   {
       value =0;
       head=NULL;
       tail=NULL;
       next=NULL;
   }
  
   bool isEmpty ()
   {
      if (head==NULL) return true;
   else
      return false ;
   }

   void SetFirstElement (int value)
   {
       if (isEmpty () )
        {
           node *n = new node ;
           n->next=NULL;
           n->value=value;
           n->previous=NULL;
           head=tail=current=n;
        }

   }

   void AddItem (int value)
   {
       if (isEmpty () ) SetFirstElement (value);
  
       else
        {
          node *n= new node;
          n->value=value;
          n->previous=current;
          n->next=NULL;
          tail->next=n;
          current=tail=n;
      
        }
    }
  
   void Remove () 
   {
      if (head!=NULL)
      {
         node *temp;
         temp=head;
         head=head->next;
         delete temp;
      }
      else
         cout <<"EMPTY"<<endl;
   }

  
   void print ()
  {
           if(isEmpty ())
           {
               cout<<"List is empty"<<endl;
           }
          
           else
               cout<<"The list is"<<endl;
  
           current=head;
          
           while(current!=NULL)
           {
               cout<<current->value<<endl;
               current=current->next;
           }
   }
   void add_at_begining (int value)
   {
      node *temp= new node ;
      temp->value=value;
      temp->previous=NULL;
      temp->next=head;
      head=temp;
   }

   bool search (int value)
   {
    current=head;
   
    while (current!=NULL)
      {
          if(current->value==value) return true;
          current=current->next;
      }
   }

   void AddItem(int back, int value)
   {
    current=head;
   
    if(search(back))
    {
      while(current!=NULL)
         {
            if(current->value==back)
               {
                  node *temp=new node;
                  temp=current->next;
                  node *temp1=new node;
                  temp1->value=value;
                  temp1->next=temp;
                  temp1->previous=current;
                  current->next=temp1;
               }
            current=current->next;
         }
      }
   }

   void sort ()
   {
       for(int i=0;i<10;i++)
       {
           current=head;
           while(current!=NULL)
           {  
               if(current->next==NULL) break;
               if(current->value>current->next->value)
               {
                      swap(current,current->next);
               }
      current=current->next;
           }
       }
   }
  
   void Remove (int value)
   {
    current=head;
   
    while(current!=NULL)
    {
      if(current->value==value)
      {
         node *temp=new node;
         temp=current;
         node *temp1=new node;
         temp1->previous=temp->previous;
         temp1->value=current->next->value;
         temp1->next=current->next->next;
         current=temp1;
         delete temp;
      }
     
      current=current->next;
    }
  }

  void print_previous (int value)
  {
    if(search(value))
    {
      current=head;
      while (current!=NULL)
         {
            if(current->value==value)
            {
                cout<<"CURRENT"<<current->value<<endl<<"Previous:"<<current->previous->value<<endl;
            }
           
            current=current->next;  
         }
     }
   }

   int check_elements();

   void swap(node *element1,node *element2 )
   {
      node *temp1,*temp2= new node;
      int temp=element1->value;

      temp1=element1;
      temp1->value=element2->value;
      element1=temp1;

      temp2=element2;
      temp2->value=temp;

      element2=temp2;
   }

  void check (int value)
  {
     current=head;
     while(current!=NULL)
     {
         if(current->value==value){ swap(current,current->next);break;}
     
         current=current->next;
     }
  }

  int max_value ()
  {
      current=head;
  
      int temp=0;
      while(current!=NULL)
        {
           if(temp<current->value) temp=current->value;
           current=current->next;
        }
     return temp;
  }

};

int main()
{
    return 0;
}

No comments:

Post a Comment