#include <iostream>
using std::cout;
using std::endl;
template<typename T,int size=11>
class Queue
{
public:
Queue();
bool isFull();
bool empty();
void push(const int elem);
void pop();
T get_back();
T get_front();
private:
T _arr[size];
int _front;
int _rear;
};
template<typename T,int size>
Queue<T,size>::Queue()
:_front(0)
,_rear(0){
}
template<typename T,int size>
bool Queue<T, size>::empty() {
return _rear==_front;
}
template<typename T,int size>
bool Queue<T, size>::isFull() {
return (_rear + 1) % size == _front;
}
template<typename T,int size>
void Queue<T, size>::push(const int elem) {
if (!isFull())
_arr[_rear++] = elem;
else
cout << "队列已经满了" << endl;
}
template<typename T,int size>
void Queue<T, size>::pop() {
if (!empty()) {
++_front;
_front %= size;
}
else
cout << "队列已经满了" << endl;
}
template<typename T,int size>
T Queue<T, size>::get_front() {
return _arr[_front];
}
template<typename T,int size>
T Queue<T, size>::get_back() {
return _arr[(_rear - 1 + size) % size];
}
int main() {
Queue<int> queint;
cout << "queue is empty " << queint.empty() << endl;
queint.push(1);
cout << "after push 1,queue is empty " << queint.empty() << endl;
for (int i = 2; i !=11; i++)
{
queint.push(i);
}
cout << "queue is full? " << queint.isFull() << endl;
while (!queint.empty())
{
cout << queint.get_front() << " ";
queint.pop();
}
cout <<endl;
system("pause");
return 0;
}
queue is empty 1
after push 1,queue is
queue is full? 1
1 2 3 4 5 6 7 8 9 10