#include<iostream>#include"MyQueue.h"using namespace std;MyQueue::MyQueue(int queueCapacity){ m_iQueueCapacity = queueCapacity; m_pQueue = new int[m_iQueueCapacity]; ClearQueue();}MyQueue::~MyQueue(){ delete []m_pQueue; m_pQueue = NULL;}void MyQueue::ClearQueue(){ m_iHead = 0; m_iTail = 0; m_iQueueLen = 0;} bool MyQueue::QueueEmpty() const {//法1: if (m_iQueueLen == 0) { return true; } else { return false; }//法2: //return m_iQueueLen == 0 ? true : false; }// 获取 队列长度 int MyQueue::QueueLength() const { return m_iQueueLen; } //获取 队列是否为满 bool MyQueue::QueueFull() const { if (m_iQueueLen == m_iQueueCapacity) { return true; } else { return false; } } //新元素入队 bool MyQueue::EnQueue(int element) { if (QueueFull()) { return false; } else { m_pQueue[m_iTail] == element; m_iTail++; m_iTail = m_iTail % m_iQueueCapacity; m_iQueueLen++; return true; } } //首元素出队 bool MyQueue::DeQueue(int &element) { if (QueueEmpty()) { return false; } else { element = m_pQueue[m_iHead]; m_iHead++; m_iHead = m_iHead % m_iQueueCapacity; m_iQueueLen--; return true; } } //遍历队列 void MyQueue::QueueTraverse() { cout << "遍历队列" <<endl; for (int i = m_iHead; i < m_iQueueLen + m_iHead; i++) { cout << m_pQueue[i % m_iQueueLen] << endl; } cout << endl; }
目前暂无任何回答
- 0 回答
- 0 关注
- 1429 浏览
添加回答
举报
0/150
提交
取消