-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathQueue.h
52 lines (43 loc) · 861 Bytes
/
Queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include "public.h"
typedef struct {
Block *base;
int front;
int rear;
}SqQueue;
void initQueue(SqQueue &Q)
{
Q.base = (Block*)malloc(MAXQSIZE * sizeof(Block));
if (!Q.base)
return;
Q.front = Q.rear = 0;
}
void enQueue(SqQueue &Q, Block e)
{
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
}
int getLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
void deQueue(SqQueue &Q, Block &e)
{
if (Q.front == Q.rear)
return;
srand((unsigned)time(NULL));
int randIdx = rand() % getLength(Q);
e = Q.base[randIdx];
Q.base[randIdx] = Q.base[Q.rear - 1];
Q.base[Q.rear - 1] = e;
Q.rear = (Q.rear - 1) % MAXQSIZE;
}
bool isEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
return true;
else
return false;
}