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;
}

Thursday, November 8, 2012

SinglyLinkedList

#include <cstdlib>
#include <iostream>

using namespace std;
class SLNode{
int element;

SLNode *next;

SLNode(int evalue, SLNode *nvalue){
            element=evalue; next=nvalue;
            }
            friend class SinglyLinkedList;
            };
class SinglyLinkedList
{
      SLNode *head;
      SLNode *tail;
    public:
           SinglyLinkedList();
           ~SinglyLinkedList();
           bool IsEmpty();
           void MakeEmpty();
           int First();
           int Last();
           void InsertFirst(int item);
           void InsertLast(int item);
           void InsertBefore(int key, int item);
           void InsertAfter(int key, int item);
           void InsertPosition(int pos, int item);
           bool FindItem(int item);
           void DeleteFirst();
           void DeleteLast();
           void DeleteBefore(int item);
           void DeleteAfter(int item);
           void DeletePosition(int pos);
           void DeleteItem(int item);
           int Count();
           void Print();
           };
          
          
           SinglyLinkedList::SinglyLinkedList(){
           head=tail=NULL;
           }
           SinglyLinkedList::~SinglyLinkedList(){
           MakeEmpty();
           }
           void SinglyLinkedList::MakeEmpty()
           {                                                                          
                SLNode *curr=NULL;
                while(head !=NULL){
                      curr=head;
                      head=head->next;
                      delete curr;
           }
            tail=NULL;
          }
          bool SinglyLinkedList::IsEmpty(){
               if(head==NULL && tail==NULL)
                    return true;
                   return false;
          }
          void SinglyLinkedList::InsertFirst(int item)
          {
               SLNode *curr=new SLNode(item, head);
               if(IsEmpty()==true)
                  tail=curr;
                  head=curr;
          }
          void SinglyLinkedList::InsertLast(int item)
          {
               SLNode *curr=new SLNode(item, NULL);
               if(IsEmpty()==true)
                  head=curr;
               else
                  tail->next=curr;
                  tail=curr;
          }
          void SinglyLinkedList::InsertBefore(int key, int item){
               SLNode *prevcurr=NULL;
               SLNode *nextcurr=head;
               if(IsEmpty()==false){
                    while(nextcurr->element!=key && nextcurr!=NULL){
                          prevcurr=nextcurr;
                          nextcurr=nextcurr->next;
          }
                          if(nextcurr==NULL)
                             cout<<"ugugdsun element oldsongvi";
                          if(nextcurr->element==key) {
                             SLNode *curr=new SLNode(item, nextcurr);
                             prevcurr->next=curr;
                          }
             }
             else
                cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::InsertAfter(int key, int item) {
               SLNode *prevCurr=NULL;
               SLNode *nextCurr=head;
               if(IsEmpty()==false){
                    while(prevCurr->element!=key && nextCurr!=NULL){
                    prevCurr=nextCurr;
                    nextCurr=nextCurr->next;
          }   
               if(nextCurr==NULL)
                  cout<<"ugugdsun element oldsongvi";
               if(prevCurr->element==key){
                  SLNode *curr=new SLNode(item, nextCurr);
                  prevCurr->next=curr;
               }                           
            }
            else
                cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::InsertPosition(int pos, int item)
          {
               if(IsEmpty()==false){
                  SLNode *prevCurr=NULL;
                  SLNode *nextCurr=head;
                  int i=1;
                  while(i!=pos && nextCurr!=NULL){
                       prevCurr=nextCurr;
                       nextCurr=nextCurr->next;
                       i++;
               }
               if(nextCurr==NULL)
                      cout<<"bairlal buruu";
               if(i==pos) {
                   SLNode *curr=new SLNode(item, nextCurr);
                   prevCurr->next=curr;
               }
            }
            else
                cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::Print(){
               if(IsEmpty()==false){
                    SLNode *curr=head;
                    cout<<"SinglyLinkedList:";
                    while(curr!=NULL){
                        cout<<curr->element<<"->";
                        curr=curr->next;
                    }
               }
               else
                    cout<<"SinglyLinkedList is empty"<<endl;
          }                                               
          bool SinglyLinkedList::FindItem(int key){
               if(IsEmpty()==false){
               SLNode *curr=head;
               while(curr!=NULL && curr->element!=key){
                     curr=curr->next;
              }
               if(curr->element==key) return true;
               return false;
               }
               else
                return false;
          }
          int SinglyLinkedList::Count(){
              if(IsEmpty()==false){
                   SLNode *curr=head;
                   int counter=0;
                   while(curr!=NULL){
                         counter++;
                         curr=curr->next;
                       }
                        return counter;
                   }
                   else
                        return 0;
          }
          int SinglyLinkedList::First(){
               if(IsEmpty()==true)
                  cout<<"SinglyLinkedList is empty";
                 return head->element;
          }
          int SinglyLinkedList::Last(){
               if(tail==NULL)
                  cout<<"SinglyLinkedList is empty";
                  return tail->element;
          }
          void SinglyLinkedList::DeleteFirst(){
               if(IsEmpty()==false){
                    SLNode *curr=head;
                    head=curr->next;
                    delete curr;
               }
               else
                    cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::DeleteLast(){
               if(IsEmpty()==false){
                    SLNode *prevCurr=NULL;
                    SLNode *curr=head;
                    while(curr!=tail)
                    {
                     prevCurr=curr;
                     curr=curr->next;
                    }
                if(curr==tail){
                    prevCurr->next=curr->next;
                    tail=prevCurr;
                   }
                    delete curr;
                }
                else
                    cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::DeleteBefore(int key){
               if(IsEmpty()==false){
                    SLNode *prevCurr=NULL;
                    SLNode *curr=NULL;
                    SLNode *nextCurr=head;
                    while(nextCurr!=NULL && nextCurr->element!=key)
                    {
                          prevCurr=curr;
                          curr=nextCurr;
                          nextCurr=nextCurr->next;
                    }
                    if(nextCurr==NULL)
                       cout<<"key element oldsongvi"<<endl;
                    if(nextCurr==head)
                       cout<<"key elementiin umnu element bhgvi bna"<<endl;
                  else if(nextCurr->element==key){
                       if(curr==head)
                          head=nextCurr;
                       else
                          prevCurr->next=nextCurr;
                       delete curr;
                  }
                }
                 else
                      cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::DeleteAfter(int key){
               if(IsEmpty()==false){
                    SLNode *prevCurr=NULL;
                    SLNode *curr=head;
                    while(curr!=NULL && prevCurr->element!=key)
                    {
                      prevCurr=curr;
                      curr=curr->next;
                    }
                    if(curr==NULL)
                      cout<<"key element oldsongvi"<<endl;
                    if(prevCurr==tail)                                                                                                                                                  
                      cout<<"key elementiin hoino element bhgvvi bna"<<endl;
               else if(prevCurr->element==key){
                    prevCurr->next=curr->next;
                    delete curr;
               }
            }
             else
                cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::DeleteItem(int key){
               if(IsEmpty()==false){
                    SLNode *prevCurr=NULL;
                    SLNode *curr=head;
                    while(curr!=NULL && curr->element!=key)
                    {
                      prevCurr=curr;
                      curr=curr->next;
                    }
                if(curr==NULL)
                      cout<<"key element oldsongvi"<<endl;
                if(curr==tail)
                   tail=prevCurr;
                if(curr==head)
                   head=prevCurr;
                if(curr->element==key){
                   prevCurr->next=curr->next;
                   delete curr;
                }
               }
                else
                     cout<<"SinglyLinkedList is empty"<<endl;
          }
          void SinglyLinkedList::DeletePosition(int pos){
               if(IsEmpty()==false){
                    int i=0;
                    SLNode *prevCurr=NULL;
                    SLNode *curr=head;
                    while(curr!=NULL && i!=pos)
                    {
                          prevCurr=curr;
                          curr=curr->next;
                          i++;
                    }
                    if(curr==NULL)
                       cout<<"bairlal oldsongvi"<<endl;
                    if(i==pos)
                    if(curr==head)
                       head=curr->next;
                    else
                       prevCurr->next=curr->next;
                    if(curr==tail)
                       tail=prevCurr;
                       delete curr;
                    }
             
               else
                     cout<<"SinglyLinkedList is empty"<<endl;
          }                                                                                                                                                                                                                                                                                                                           


int main(int argc, char *argv[])
{
    SinglyLinkedList list;
    list.InsertFirst(21);
    list.InsertFirst(24);
    list.Print();


    system("PAUSE");
    return EXIT_SUCCESS;
}

Tuesday, October 30, 2012

Сэтгэлийн үг.



Уншаад л баймаар байна.


Дусал нулимсанд ч дурлал байдаг
Дуулаагүй дуунд ч эгшиг байдаг
Нулимснаас гашуун ус байдаг
Нууцхан булиглах зүрхэнд хайр байдаг юм шүү!


Мартдаа.

Мартдаа залуу минь намайг март
Манант өдөр шиг дүрийг минь ч март
Хичнээн дурлаад хайр болоогүй
Энэхэн дурлалыг минь бүр март
Азгүй дурлалын боол явсан
Арчаагүй тэнэг занг минь март
Амьдралын хүрднээс арчиж март
Ариун нулимсанаараа угааж март даа
Тэнд надаас хувь илүү нэгэн
Ганцхан чиний л хайраар дутаж яваа
Эргэж харсны хэрэггүй ээ яанаг минь
Энэ орчлонд намайг байгаагүй гээл итгэ
Мартдаа хайрт минь март намайг байгаагүй гэдэгт итгэ!

 


Saturday, October 13, 2012

Шинэ Шүлэг

Эгэл юм бүхэнд дуртай
Энгийн бус жирийн юманд дуртай
Элдэв маяггүй чиний төрхөнд дуртай
Энхрий ялдам охин чамд хайртай ^_^

Шохоорхов, сонжив түүнийг
Шоолон инээв, найздаа ярив
Бодоод догдлов, барааг л хараад уйлав
Би яачихав...
Худлыг мянга яриад өөртөө үнэмшив
Хуурамчхан шохоорхол биш гэхээс дотроо эмээв
Чи харин юу ч мэдэхгүй байгаад гунив
Чимээгүйхэн нэг л өдөр ирнэ гэж итгэв
Хайр өмнө нь надад байсан
Хагацал бас байсан
Итгэлээр гэрэлтсэн цаг хугацаа байсан
Ирэх өдрүүд тийм байгаасай ^_^