队(queue),C++模板实现

队:(queue.h)

#include<iostream>
#include<string>
using namespace std;
//队列判空和判满
//头尾指针相同为空
//尾指针指向下一个可存放数据的单元,如果尾指针偏移一个单元和头指针相同,队列为满
template<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>::push(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>::pop(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;
}

测试文件 (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;
}

猜你喜欢

转载自www.cnblogs.com/meihao1203/p/9190124.html