#include <iostream>
#include <string>
using namespace std;
template <typename T>
class Myqueue
{
private:
int front; //头元素下标
int rear; //尾元素下标
int maxSize; //队列总空间大小
T *array;
public:
Myqueue(int size = 10) //构造函数
{
front = 1;
rear = 0;
maxSize = size + 1;
array = new T[maxSize];
}
T back()
{
if (empty())
{
cout << "队列为空" << endl;
return NULL;
}
return array[rear];
}
bool empty()
{
if (size() == 0)
return true;
return false;
}
T frontValue()
{
if (empty())
{
cout << "队列为空" << endl;
return NULL;
}
else
{
return array[front];
}
}
void pop()
{
if (empty())
cout << "队列已经为空" << endl;
else
front = (front + 1) % maxSize;
}
void push(T value)
{
if (size() == maxSize - 1)
{
cout << "队列已满,无法继续添加元素" << endl;
}
else
{
rear = (rear + 1) % maxSize;
array[rear] = value;
}
}
int size()
{
return (rear + maxSize - front + 1) % maxSize;
}
};
int main()
{
Myqueue<string> q(5);
q.push("abc");
q.push("def");
q.push("ghi");
q.push("jkl");
q.push("mno");
q.push("pqr");
int len = q.size();
cout << q.back() << endl;
for (int i = 0; i < len; i++)
{
cout << q.frontValue() << endl;
q.pop();
}
cout << q.empty() << endl;
return 0;
}
参考: