队列是一种动态集合,且在其上进行的DELETE操作所移除的元素是预先设定的。在队列中, 被删去的总是在集合存在时间最长的那个元素:队列实现的是一种先进先出策略。
队列也分为两种:顺序队列和循环队列
一、顺序队列(c++实现):
//顺序队列;
#include <iostream>
using namespace std;
template <typename T>
class orderQueue
{
public:
//构造函数;
orderQueue()
{
m_iCount = 0;
m_iHead = 0;
m_iRail = 0;
memset(m_Array,0,sizeof(m_Array));
}
//析构函数;
~orderQueue()
{
}
private:
//队列首;
int m_iHead;
//队列尾;
int m_iRail;
//队列元素数目;
int m_iCount;
//容量;
enum{m_iCapacity = 10};
//存放元素的数组;
T m_Array[m_iCapacity];
public:
//获得队列中元素数目;
const int& size()
{
return m_iCount;
}
//判断队列空;
bool isEmpty()
{
if (m_iHead == m_iRail)
{
return true;
}
return false;
}
//判断队列满;
bool isFull()
{
if(m_iRail == m_iCapacity-1)
return true;
return false;
}
//入队列;
void push_back(const T& _element)
{
if (isFull())
{
cout<<"queue is full! push_back fail!"<<endl;
return;
}
m_Array[m_iRail] = _element;
++m_iRail;
++m_iCount;
}
//出队列;
void pop_front()
{
if (isEmpty())
{
cout<<"queue is empty!pop fail!"<<endl;
return;
}
m_Array[m_iHead] = NULL;
++m_iHead;
--m_iCount;
}
//取得队头元素;
const T& front()
{
if (isEmpty())
{
cout<<"queue is empty!"<<endl;
return NULL;
}
return m_Array[m_iHead];
}
//清空队列;
void clear()
{
m_iCount = 0;
m_iHead = 0;
m_iRail = 0;
memset(m_Array,0,sizeof(m_Array));
}
};
//测试代码;
//测试顺序队列;
orderQueue<int*> _orderQueue;
cout<<"1 _orderQueue size : "<<_orderQueue.size()<<endl;
int * pp[10];
int a1[] = {0,1,2};
int a2[] = {10,21,2};
int a3[] = {0,11,2};
int a4[] = {0,1,22};
int a5[] = {40,51,2};
int a6[] = {30,1,2};
int a7[] = {20,1,2};
int a8[] = {80,1,2};
int a9[] = {10,1,2};
int a10[] = {50,1,2};
pp[0] =a1;
pp[1] =a2;
pp[2] =a3;
pp[3] =a4;
pp[4] =a5;
pp[5] =a6;
pp[6] =a7;
pp[7] =a8;
pp[8] =a9;
pp[9] =a10;
for (int j = 0 ; j<10; j++)
{
_orderQueue.push_back(pp[j]);
}
cout<<"2 _orderQueue size : "<<_orderQueue.size()<<endl;
for (int j = 0; j<3; j++)
{
cout<<" "<<_orderQueue.front()[j]<<endl;
}
for (int j = 0; j<10; j++)
{
_orderQueue.pop_front();
}
cout<<"3 _orderQueue size : "<<_orderQueue.size()<<endl;
for (int j = 0 ; j<10; j++)
{
_orderQueue.push_back(pp[j]);
}
cout<<"4 _orderQueue size : "<<_orderQueue.size()<<endl;
_orderQueue.clear();
cout<<"5 _orderQueue size : "<<_orderQueue.size()<<endl;
for (int j = 0 ; j<10; j++)
{
_orderQueue.push_back(pp[j]);
}
cout<<"6 _orderQueue size : "<<_orderQueue.size()<<endl;
二、循环队列(c++实现):
//由于顺序队列存在假上溢现象(当尾指针移动到队列最末端,而实际容量并不一定满,但是由于为尾指针不能再向后移动,所以不能再向队列中添加元素,这种现象称为假上溢现象),所以一般采用循环队列解决问题。
#include <iostream>
using namespace std;
//循环队列;
template <typename T>
class circleQueue
{
public:
circleQueue()
{
m_iHead = 0;
m_iRail = 0;
m_iCount = 0;
memset(m_Array,0,sizeof(m_Array));
}
~circleQueue()
{
}
private:
//队列首;
int m_iHead;
//队列尾;
int m_iRail;
//队列元素数目;
int m_iCount;
//容量;
enum{m_iCapacity = 10};
//存放元素的数组;
T m_Array[m_iCapacity];
public:
//获得队列中元素数目;
const int& size()
{
return m_iCount;
}
//判断队列空;
bool isEmpty()
{
if (m_iCount == 0)
{
return true;
}
return false;
}
//判断队列满;
bool isFull()
{
if (m_iCount == m_iCapacity)
{
return true;
}
return false;
}
//入队列;
void push_back(const T& _element)
{
if (isFull())
{
cout<<"queue is full! push back fail!"<<endl;
return;
}
m_Array[m_iRail] = _element;
m_iRail = (m_iRail+1)%m_iCapacity;
++m_iCount;
}
//出队列;
void pop_front()
{
if (isEmpty())
{
cout<<"queue is empty!pop fail!"<<endl;
return;
}
m_Array[m_iHead] = NULL;
m_iHead = (m_iHead+1)%m_iCapacity;
--m_iCount;
}
//取得队头元素;
const T& front()
{
if (isEmpty())
{
cout<<"queue is empty!"<<endl;
return NULL;
}
return m_Array[m_iHead];
}
//清空队列;
void clear()
{
m_iHead = 0;
m_iRail = 0;
m_iCount = 0;
memset(m_Array,0,sizeof(m_Array));
}
};
//测试循环队列;
int * pp[10];
int a1[] = {0,1,2};
int a2[] = {10,21,2};
int a3[] = {0,11,2};
int a4[] = {0,1,22};
int a5[] = {40,51,2};
int a6[] = {30,1,2};
int a7[] = {20,1,2};
int a8[] = {80,1,2};
int a9[] = {10,1,2};
int a10[] = {50,1,2};
pp[0] =a1;
pp[1] =a2;
pp[2] =a3;
pp[3] =a4;
pp[4] =a5;
pp[5] =a6;
pp[6] =a7;
pp[7] =a8;
pp[8] =a9;
pp[9] =a10;
circleQueue<int*> _circleQueue;
cout<<"1 _circleQueue size : "<<_circleQueue.size()<<endl;
for (int j = 0 ; j<11; j++)
{
_circleQueue.push_back(pp[j]);
}
cout<<"2 _circleQueue size : "<<_circleQueue.size()<<endl;
for (int j = 0; j<3; j++)
{
cout<<" "<<_circleQueue.front()[j]<<endl;
}
for (int j = 0; j<8; j++)
{
_circleQueue.pop_front();
}
cout<<"3 _circleQueue size : "<<_circleQueue.size()<<endl;
for (int j = 0 ; j<11; j++)
{
_circleQueue.push_back(pp[j]);
}
cout<<"4 _circleQueue size : "<<_circleQueue.size()<<endl;
_circleQueue.clear();
cout<<"5 _circleQueue size : "<<_circleQueue.size()<<endl;
for (int j = 0 ; j<11; j++)
{
_circleQueue.push_back(pp[j]);
}
cout<<"6 _circleQueue size : "<<_circleQueue.size()<<endl;