什么是队列?
队列(queue)是只允许在一端进行插入操作(入队),在另一端进行删除操作(出队)的线性表,允许插入的一端称为队尾,允许删除的一端称为队头。
队列的顺序存储结构
队列的顺序存储结构称为顺序队列,下面是用数组实现顺序队列的代码
#ifndef SEQUENTIALQUEUE_H_INCLUDED
#define SEQUENTIALQUEUE_H_INCLUDED
const int QueueSize=5;
template<class T>
class CirQueue
{
public:
CirQueue();
~CirQueue(){}
void EnCirQueue(T x); //进队
T OutCirQueue(); //出队
int Empty(); //判断列队是否为空
private:
T t[QueueSize];
int top,rear; //rear头队,top指向尾队的前一个空节点
};
#endif // SEQUENTIALQUEUE_H_INCLUDED
```cpp
#include <iostream>
#include "SequentialQueue.h"
using namespace std;
template<class T>
CirQueue<T>::CirQueue()
{
top=rear=0; //初始时top、rear均指向0
}
template<class T>
void CirQueue<T>::EnCirQueue(T x) //进队
{
if((rear+1)%QueueSize!=top) //当(rear+1)%QueueSize=top时队列为满
t[++rear]=x;
else
cout<<"上溢"<<endl;
}
template<class T>
T CirQueue<T>::OutCirQueue() //出队
{
if(top!=rear) //当top==rear时队列为空
{
T x=t[++top]; //top+1代表出队
return x;
}
cout<<"下溢"<<endl;
}
template<class T>
int CirQueue<T>::Empty() //判断列队是否为空
{
if(top==rear) return -1;
return 0;
}
```cpp
#include <iostream>
#include "SequentialQueue.cpp"
using namespace std;
int main()
{
CirQueue<int> C;
C.EnCirQueue(1);
C.EnCirQueue(2);
C.EnCirQueue(3);
C.EnCirQueue(4);
cout<<C.Empty()<<endl;
cout<<C.OutCirQueue()<<endl;
cout<<C.OutCirQueue()<<endl;
cout<<C.OutCirQueue()<<endl;
cout<<C.OutCirQueue()<<endl;
cout<<C.Empty()<<endl;
return 0;
}
输出结果 : 0 1 2 3 4 -1;
队列的链式储存结构
#ifndef LINKQUEUE_H_INCLUDED
#define LINKQUEUE_H_INCLUDED
template<class T>
struct node
{
T date;
node<T>*next;
};
template<class T>
class Link
{
public:
Link();
Link(T t[],int length);
void EnQueue(T x);
T OutQueue();
int Empty();
void println();
private:
node<T>*first;
};
#endif // LINKQUEUE_H_INCLUDED
#include <iostream>
#include "LinkQueue.h"
using namespace std;
template<class T>
Link<T>::Link() //建立空节点
{
first=new node<T>();
first->next=NULL;
}
template<class T>
Link<T>::Link(T t[],int length) //带参构造采用尾插法
{
first=new node<T>();
first->next=NULL;
node<T>*p=first;
for(int i=0;i<length;i++)
{
node<T>*s=new node<T>();
s->date=t[i];
p->next=s;
p=s;
}
p->next=NULL; //采用尾插法最后节点要指向null
}
template<class T>
void Link<T>::EnQueue(T x)
{
node<T>*p=first;
while(p->next!=NULL) //在尾部入队需要指针遍历到为节点,
{
p=p->next;
}
node<T>*s=new node<T>(); //入队操作
s->date=x;
p->next=s;
p=s;
p->next=NULL;
}
template<class T>
T Link<T>::OutQueue()
{
T x=(first->next)->date;
if(first->next!=NULL)
{
first->next=(first->next)->next; //在队头出队
return x;
}
else
cout<<"下溢"<<endl;
}
template<class T>
int Link<T>::Empty() //若队列为空则返回-1否则返回0
{
if(first->next==NULL)
return -1;
return 0;
}
template<class T>
void Link<T>::println() //打印队列中date
{
node<T>*p=first->next;
while(p!=NULL)
{
cout<<p->date<<" ";
p=p->next;
}
cout<<endl;
}
#include <iostream>
#include "LinkQueue.cpp"
using namespace std;
int main()
{
int a[4]={0,2,4,5};
Link<int>L(a,4);
L.EnQueue(6);
L.println();
cout<<L.OutQueue()<<endl;
cout<<L.OutQueue()<<endl;
cout<<L.OutQueue()<<endl;
cout<<L.OutQueue()<<endl;
cout<<L.OutQueue()<<endl;
cout<<L.Empty()<<endl;
return 0;
/*输出结果:0 2 4 5 6
0
2
4
5
6
-1
}