队列
队列基础
队列是一种线性结构,有队头(front)队尾(rear)两个指针,每次拉进来一个元素会让队尾加一,而每次删除一个元素会让队头加一,这是一种先进先出的结构。
队列特殊情况判断
空队列 front=rear
满队列 rear-front=N(N指的是开的数组的大小)
POP 从队头删元素
push 拉元素到队尾struct duilie
{
int a[N];
int front ,rear;
void restart()
{
front=rear=0;
}
bool empty()
{
return (rear-front)==0?true:false;
}
bool full()
{
return rear-front==N-1?true:false;
}
int top()
{
return a[front];
}
bool push(int key)
{
if(full())//如果满了就不能拖进去
{
return false;
}
a[++rear]=key;
return true;
}
bool pop()
{
if(empty())
{
return false;
}
front++;
return true;
}
};
循环队列
上面一种队列并不是真的有n个空间,如果pop了元素那空间就要减小,这样便浪费了许多空间,而用模运算组成一个循环可以使空间充分利用
特殊情况判断
空 : front=rear
满 :( rear+1)% N=front
代码实现
const int Queue_Size=100;
typedef struct circlQueue
{
char *elem;
int rear;
int front;
int queueSize;
}circlQueue;
/*初始化*/
void initQueue_C(circlQueue &Q)
{
Q.elem=new char[Queue_Size];
Q.front=Q.rear=0;//首尾指针相等说明队列为空。
Q.queueSize=Queue_Size;
}
/*销毁队列*/
void destroyQueue_C(circlQueue &Q)
{
delete []Q.elem;
Q.front=Q.rear=0;
Q.queueSize=0;
}
/*求队列的长度*/
int lengthQueue_C(circlQueue &Q)
{
int length;
length=(Q.rear-Q.front+Q.queueSize)%Q.queueSize;/*一般情况下,rear在front的上方,此种算法是用于
rear已到front的下方,即已出现假溢出的情况。*/
return length;
}
/*入队列*/
void enterQueue_C(circlQueue &Q,char x)
{
if(((Q.rear+1)%Q.queueSize)==Q.front)//判断栈满的情况
cout<<"Queue OverFlow!";
Q.elem[Q.rear]=x;
Q.rear=(Q.rear+1)%Queue_Size;//尾指针应以此种方式加1,才会实现循环队列。
}
/*出队列*/
char outputQueue_C(circlQueue &Q)
{
char e;
if(Q.rear==Q.front)
cout<<"Queue Empty";
e=Q.elem[Q.front];
Q.front=(Q.front+1)%Q.queueSize;;//头指针应以此种方式加1,才会实现循环队列。
return e;
}
————————————————
版权声明:本文为CSDN博主「hackerain」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hackerain/article/details/5934240
队列的函数
push():脱元素到队尾
pop()删队首元素
size()
front()返回队首元素
back()返回队尾元素
注意
front无法使用迭代器,因为queue在物理地址上不连续,所以queue无法遍历,想要遍历只能不停top POP,这样会销毁队列
应用
- HDU 1387
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
Input
The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
ENQUEUE x - enter element x into the team queue
DEQUEUE - process the first element and remove it from the queue
STOP - end of test case
The input will be terminated by a value of 0 for t.
Output
For each test case, first print a line saying “Scenario #k”, where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.Sample Input
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Sample Output
Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001