Team:(queue.h) #include <iostream> #include <string> using namespace std;//Queue empty and full //tail pointer is empty The //tail pointer points to the next unit that can hold the data, if the tail pointer is offset by one cell and the same head pointer, the queue is fullTemplate<class T,int num> class Queue {public:queue (); ~queue (); bool Empty (); bool Full (); BOOL Push (T elem); BOOL Pop (t& tmp); int size (); Private:int _front; int _real; T _arr[num]; }; Template<class t,int num> queue<t,num>::queue (): _front (0), _real (0) {} Template<class t,int num> Queue<t,num>::~queue () {} Template<class t,int num> bool Queue<t,num>::empty () {return _front = = _real;} Template<class t,int num> bool Queue<t,num>::full () {return _front = = (_real+1)%num;} Template<class t,int num> bool queue<t,num>::p ush (T elem) {if (!full ()) {_arr [_real] = Elem; _real = (_real+1)%num; return true; } else return false; } Template<class t,int num> bool Queue<t,num>::p op (T &tmp) {if (!empty ()) { TMP = _arr[_front]; _front = (_front+1)%num; return true; } else return false; } Template<class t,int num> int queue<t,num>::size () {return(_real-_front+num)%num;} |
Test file(queueTest.cpp) #include "queue.h" int main () {queue<int,10> Q1;Q1.push (3); Q1.push (5); int tmp; Cout<<q1.size () <<endl; Q1.pop (TMP); cout<<tmp<<endl; cout<< "----------------------" <<endl; Queue<string,5> Q2; Q2.push ("Hello"); Q2.push ("World"); Cout<<q2.size () <<endl; String tmpstring; Q2.pop (tmpstring); Cout<<q2.size () << "" <<tmpString<<endl; return 0; } |
Team (queue), C + + template implementation