class MyQueue {
public:
MyQueue(int queueCapacity); //创建队列
virtual ~MyQueue(); //销毁队列
void ClearQueue(); //清空队列
bool QueueEmpty() const; //判断队列是否为空
bool QueueFull() const; //判断队列是否为满
int Queuelength() const; //队列长度
bool EnQueue(int element); //新元素入队
bool DeQueue(int& element); //首元素出队
void QueueTraverse(); //遍历队列
private:
int* m_pQueue; //队列数组指针
int m_iQueueLen; //队列元素个数
int m_iQueueCapacity; //队列数组容量
int m_iHead;
int m_iTail;
};
MyQueue::MyQueue(int queueCapacity)
{
this->m_iQueueCapacity = queueCapacity;
this->m_pQueue = new int[m_iQueueCapacity];
ClearQueue();
}
MyQueue::~MyQueue()
{
delete[] m_pQueue;
m_pQueue = NULL;
}
void MyQueue::ClearQueue()
{
this->m_iHead = 0;
this->m_iTail = 0;
this->m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const
{
return m_iQueueLen == 0 ? true : false;
}
bool MyQueue::QueueFull() const
{
return m_iQueueLen == m_iQueueCapacity ? true : false;
}
int MyQueue::Queuelength() const
{
return m_iQueueLen;
}
bool MyQueue::EnQueue(int element)
{
if (QueueFull()) return false;
this->m_pQueue[m_iTail] = element;
m_iTail = (m_iTail + 1) % m_iQueueCapacity;
m_iQueueLen++;
return true;
}
bool MyQueue::DeQueue(int& element)
{
if (QueueEmpty()) return false;
element = m_pQueue[m_iHead];
m_iHead = (m_iHead + 1) % m_iQueueCapacity;
m_iQueueLen--;
return true;
}
void MyQueue::QueueTraverse()
{
for (int i = m_iHead; i < m_iQueueLen+m_iHead; i++)
cout << m_pQueue[i % m_iQueueCapacity] << endl;
}