废话不说...
第一期:在队列中,被删去的总是在集合中存在时间最长的那个元素:队列实现的是一种先进先出(first-in,first-out,FIFO)策略;
队列上的INSERT操作称之为入队(ENQUEUE),DELETE操作称为出队(DEQUEUE);正如栈的pop操作一样,DEQUEUE操作也没有元素参数。队列的先进先出特性类似于收银台前排队结账的一排顾客。队列有队头(head)和队尾(tail),当有一个元素入队时,它被放在队尾的位置;
下面是一个简单的队列操作,初始化队列,入队,出队处理;
#include <iostream>
#include <cstdlib>
using namespace std;
const int QueueMaxSize = 30; //栈空间
typedef int ElemType;
typedef struct Queue
{
ElemType queue[QueueMaxSize] ;
int head; //队头指针
int tail; //队尾指针
} QUEUE;
void InitQueue(Queue &Q)
{
Q.head = Q.tail = 0;
}
int QueueEmpty(QUEUE Q)
{
if(Q.head == Q.tail)
return true;
else
return false;
}
void EnQueue(QUEUE &Q, ElemType x) //入队
{
if((Q.tail + 1)% QueueMaxSize == Q.head) //判断是否队满
exit(0);
Q.queue[Q.tail] = x; //入队
Q.tail = (Q.tail + 1) % QueueMaxSize; //队尾向后延伸
}
ElemType DeQueue(QUEUE &Q) //出队,并获取队头元素
{
if (QueueEmpty(Q)) //空队列
exit(0);
ElemType x = Q.queue[Q.head]; //获取队头元素
Q.head = (Q.head + 1) % QueueMaxSize;
return x;
}
int main()
{
Queue Q;
InitQueue(Q);
if(QueueEmpty(Q))
cout<<"队为空"<<endl;
else
cout<<"非空队"<<endl;
EnQueue(Q, 5);
cout<<DeQueue(Q)<<endl;
EnQueue(Q, 1);
EnQueue(Q, 5);
cout<<DeQueue(Q)<<endl;
return 0;
}
初始化队列InitQueue,令head = tail = 0;
接下来可进行入队EnQueue处理,若是队满则退出,否则正常进行入队,队尾延伸;
进行出队处理,若是对空则退出,否则正常进行出队,队头延伸;
运行如下:
后续队列处理不定期更新...
*∩_∩*