隊列(queue)——C++實現

來源:互聯網
上載者:User

標籤:names   typename   函數   []   void   enqueue   space   pac   ret   

隊列是常用的資料結構之一,可以採用表直接很容易實現,為了弄清原理,我們採用數組實現

 1 /************************************************************************/ 2 /* 隊列的實現,可以通過表直接實現,這裡採用數組實現 3 /************************************************************************/ 4  5 #ifndef QUEUE_H 6  7 #define QUEUE_H 8  9 #include <assert.h>10 11 namespace stl12 {13     template <typename Object>14     class Queue15     {16         Queue(int QueueSize)17             :Front(0)18             ,Rear(1)19             ,Size(0)20             ,Capacity(QueueSize)21         {22             Array = new Object[QueueSize];23         }24 25         ~Queue()26         {27             assert(Array);28             delete[] Array;29         }30 31         //入隊函數32         void EnQueue(Object x)33         {34             if (IsFull())35             {36                 throw();37             }38             Array[Rear++] = x;39             Rear = (++Rear) % Capacity;40             ++Size;41         }42 43         //出隊函數44         void DeQueue()45         {46             if (IsEmpty())47             {48                 throw();49             }50             ++Front;51             --Size;52         }53 54         bool IsFull()55         {56             return Size == Capacity;57         }58 59         bool IsEmpty()60         {61             return Size == 0;62         }63 64     private:65         Object* Array;66         int Capacity;    //隊列最大容量67         int Front;         //隊列頭位置68         int Rear;          //隊列尾的位置69         int Size;           //隊列中對象的數量70     };71 }72 #endif // !QUEUE_H

 

隊列(queue)——C++實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.