الأربعاء، 9 مارس 2011

كود سي بلس بلس لتمثيل الكيو لنك لست

برنامج اكثر من رائع لن تجده الا هنا لتمثيل الكيو لنك لست بلغة السي بلس بلس فهذا البرنامج يقوم ببناء  الكيو لنك لست باستخدام خاصية التمبليت التي تمكنك من اضافة اي نوع من المتغيرات الى الكيو   يحتوي البرنامج على كل الدوال التي تخص الكيو فقد تم بناء دالة الاضافة append بطريقة مبسطة وسهلة للفهم ودالة الحذف serve لحذف اول قيمة فالكيو فالكيو المميز فيها انها تتعامل ب FIFO بمعنى اول قيمة تمت اضافتها هيا اول قيمة يتم التعامل معها 

يوجد في هذا البرنامج دالة موف MOVE  التي تمكنك من تبديل اي قيمة بالكيو ووضعها على الفرونت


# include <iostream>
# include <conio>
# include <stdlib>

template <class T>
struct node
{

  T num;
  node<T> *next;

};

template <class T>
class queue
{

 private:
          node<T> *front;
          node<T>* rear;
          int count;

 public:
           queue();
          void append(int num);
          void move(int n);
          void print();
          void serve();
          bool isempty();

};

template <class T>
queue<T>::queue()
{
   front=NULL;
   rear=NULL;
   count=0;
 }

   template <class T>
bool queue<T>::isempty()
{
   if (front==NULL)

     return 1;
     else
     return 0;
 }
 template <class T>
void queue<T>::append(int num)
{
     node<T> *newnode;
     newnode=new node<T>;
     newnode->num=num;
     newnode->next=NULL;

     if(front==NULL)
     {
       front=newnode;
       rear=newnode;
     }
     else
     {
        rear->next=newnode;
        rear=newnode;

      }
      count++;

 }

 template <class T>
void queue<T>::move(int n)
{
   if(n<count && n!=0)
   {
   node<T>*current;
   current=front;
   int c= front->num;
   for(int i=1;i<=n;i++)
   {
     current=current->next;
     if(i==n)
     {
      front->num=current->num;
     }
   }

   current->num=c;
   }
   else
   {
   cout<<" \t \n The index not correct ."<<endl;
   }
 }

 template <class T>
 void queue<T>::serve()
 {

     node <T> *temp;
     temp=front;
     front=front->next;
     delete temp;
     count--;


 }

  template <class T>
  void queue<T>::print()
  {
     textcolor(90);
    cprintf("Queue is :");
    node <T> * current;
    current=front;
    while(current != NULL)
    {
      cout<<current->num<<"    ";
      current=current->next;
    }


    cout<<endl<<endl;
   }

 int main()
 {
    int n ,x,item,m,z;
    queue<int> q;

    cout<<"======  QUEUELINKLIST =========="<<endl<<endl;
    cout<<"  1: append item to the queue  "<<endl;
    cout<<"  2: move element to the front  "<<endl;
    cout<<"  3: serve from queue           "<<endl;
    cout<<"  press ( 0) to exist           "<<endl<<endl;
    cout<<"==============================="<<endl;
    cout<<" ===== PART ONE ( INTEGER )  ======";

      cout<<endl<<endl;

    do
    {

      cout<<" \t Enter your choice : ";
      cin>>x;
       cout<<endl;
       switch(x)
       {
            case 1 :
                   {
                       cout<<"Enter number of item to append :";
                       cin>>n;
                       for(int i=1;i<=n;i++)
                       {
                             cout<<"Enter The item  :";
                             cin>>item;
                             q.append(item);
                             cout<<endl;
                        }
                        q.print();

                     break;
                    }

            case 2 :
                 {
                      if(q.isempty())
                      {
                        cout<<"Queu is empty >"<<endl;
                      }
                      else
                      {
                    cout<<"Enter index of item to move :";
                    cin>>m;
                    q.move(m);
                    cout<<endl;
                    q.print();
                    cout<<endl;
                    }


                  break;
                  }
           case 3 :
                 {
                 if(q.isempty())
                 {
                    cout<<"Queue is empty ."<<endl;
                  }
                  else
                  {

                   cout<<"Enter number of item to serve :";
                   cin>>z;
                   if( z>n)
                   { cout<<"The#of item to sreve is larger tham # tem in queue ";
                     cout<<endl;
                   }
                   else
                   {
                   for(int i=0;i<z;i++)
                   {
                      q.serve();

                    }
                       q.print();
                       cout<<endl;
                  }

                 }
                    break;
                   }


         }
      }
      while( x != 0);


      return 0;
}

ليست هناك تعليقات:

إرسال تعليق