C + + Implementation queue

Source: Internet
Author: User

#ifndef  __queue__#define __queue__class cqueue{      int *  m_pData;      int m_nHead,m_nTail;       int m_ncount;   public:      cqueue (int nCount=10 );       bool isempty ();       bool isfull ();       void push (CONST INT & V);       bool pop (int &v);}; Cqueue::cqueue (Int ncount) {    m_pdata = new int[ncount];     m_nhead = m_ntail = 0;    m_ncount = ncount;} Void cqueue::p ush (const int & value) {    if (!isFull ())      {        m_pdata[m_ntail++] = value;                 if (M_ntail >= m_ncount)           m_ntail =  0;    }}bool cqueue::p op  (Int & value) {     if (IsEmpty ()) return false;    ch = m_pdata[m_nhead++];     if (M_nhead >= m_ncount)        m_nHead =  0;    return true;} Bool cqueue::isfull () {     return  ((m_ntail+1)%m_nCount == m_ Nhead);} Bool cqueue::isempty () {     return m_nhead == m_ntail;} #endif

The

method is called as follows:

#include  <iostream> #include   "queue.h" Using namespace std;int main (INT&NBSP;ARGC ,  char* argv[]) {     cqueue queue = 5;      queue.push (2);      queue.push (3);      queue.push (4);      queue.push (5);      queue.push (6);      queue.push (7);      queue.push (8);      int c  = 0;     queue.pop (c);     cout <<  c <<endl;     queue.pop (c);     cout  << c <<endl;     queue.pop (c);      Cout << c <<endl;     queue.push (9);      bool re&nBsp;= queue.pop (c);     cout << re << ":" <<  c <<endl;     re = queue.pop (c);      cout << re <<  ":" << c <<endl;      re = queue.pop (c);     cout << re << ":"  << c <<endl;     getchar ();}

After the

Templating process, the following:

#ifndef  __QUEUE__#define __QUEUE__template<class T>class CQueue{       T * m_pData;      int m_nHead,m_nTail;       int m_nCount;   public:       Cqueue (int ncount=10);       bool isempty ();       bool isfull ();       void push (const T & &NBSP;V);       bool pop (t &v);}; Template<class t>cqueue<t>::cqueue (int ncount) {    m_pdata =  new T[nCount];    m_nHead = m_nTail = 0;     m_ncount = ncount;} Template<class t>void cqueue<t&gt::p ush (Const t & value) {     if (!isfull ())     {        m_pdata[m_ntail++] = value;                 if (M_nTail  >= m_ncount)           m_nTail = 0;     }}template<class t>bool cqueue<t>::p op  (T &  CH) {    if (IsEmpty ()) return false;    ch = m_pdata[m_ Nhead++];    if (M_nhead >= m_ncount)         m_nhead = 0;    return true;} Template<class t>bool cqueue<t>::isfull () {     return  (m_ ntail+1)%m_ncount == m_nhead);} Template<class t>bool cqueue<t>::isempty () {     return m_ Nhead == m_ntail;}#endif 


C + + Implementation queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.