Wednesday, November 14, 2012

#include <cstdlib>
#include <iostream>

using namespace std;

typedef int Data;

class CLNode
{
int element;
CLNode*next;
CLNode(int evalue,CLNode*nvalue){
     element=evalue;
     next=nvalue;
     }
     friend class CircularLinkedList;
     };
    
 class CircularLinkedList
 {
       CLNode *ListData;
   public:
   CircularLinkedList();  
  ~CircularLinkedList();
     bool IsEmpty();
      void MakeEmpty();
      void InsertItem(int item);
      bool FindItem(int key);
      void DeleteItem(int item);
      void Print();
};


CircularLinkedList::CircularLinkedList()
{
                ListData=NULL;
}

CircularLinkedList::~CircularLinkedList()
{
    MakeEmpty();
}

void CircularLinkedList::MakeEmpty(){
     CLNode*location=ListData;
     while(ListData->next!=ListData){
           location=ListData->next;
           ListData->next=location->next;
           delete location;
           }
           if (ListData->next==ListData){
               location=ListData;
               ListData=NULL;
               delete location;
               }
}

bool CircularLinkedList::IsEmpty(){
         if (ListData==NULL)
             return true;
         return false;
}
void CircularLinkedList::InsertItem(int item){
      if (IsEmpty()==true){
      CLNode*location=new CLNode(item,location);
      location->next=location;
      ListData=location;
      }
      else{
      CLNode*location=new CLNode(item,ListData->next);
      ListData->next=location;
      ListData=location;
      }
}
void CircularLinkedList::Print(){
     if (IsEmpty()==false){
       CLNode*location=ListData;
       cout<<"CircularLinkedList:";
     if (location==ListData){
         cout<<location->element<<"->";
         location=location->next;
         }
         while (location!=ListData){
               cout<<location->element<<"->";
               location=location->next;
               }
               cout<<endl;
               }else
               cout<<"CircularLinkedList is empty"<<endl;
}
bool CircularLinkedList::FindItem (int key){
       if (IsEmpty()==true){
           CLNode*location=ListData;
           while (location->element!=key && location->next==ListData)
                   {
                      location=location->next;
                   }
                      if (location->element==key)
                      return true;
                      return false;
                   }else
                    return false;
                   }
void CircularLinkedList::DeleteItem(int Item){
     if (IsEmpty()==false){
                           CLNode*predLoc=ListData;                                            
                           CLNode*location=ListData;
                           while (location->element!=Item && location->next!=ListData){
                                 predLoc=location;
                                 location=location->next;
                                 }
                                 if (location->next==ListData)
                                 cout<<"ugugdsun element baikhgvi bna"<<endl;
                                 else
                                 if (location->element==Item){
                                 if (location==ListData){
                                    while(predLoc->next!=location)
                                    predLoc=predLoc->next;
                                    ListData=ListData->next;
                                    }
                                    predLoc->next=location->next;
                                    delete location;
                                    }
                                    }else
                                    cout<<"CircularLinkedList is empty"<<endl;
}                                                                            
int main(int argc, char *argv[])
{
    CircularLinkedList List;
    List.InsertItem(10);
    List.InsertItem(15);
    List.InsertItem(5);
    List.InsertItem(25);
    List.Print();
   
    bool f = List.IsEmpty();
    cout<<f<<endl;
   
    List.MakeEmpty();
    List.Print();
   
    List.InsertItem(2);
    List.InsertItem(5);   
    List.InsertItem(10);       
    List.Print();
   
    bool m = List.FindItem(5);
    cout<<m<<endl;
   
   
    List.DeleteItem(10);
    List.Print();
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

No comments:

Post a Comment