Team (queue), C + + template implementation

Source: Internet
Author: User

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

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.