#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>::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