循环队列的操作
2019-09-22 14:16:23
本文总阅读量

​ 为充分利用向量空间,克服假溢出现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular Queue)。循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
#define ERROR 1
#define OK 0

typedef int Status;
typedef int QueueElemType;
typedef enum { false, true } bool;

typedef struct
{
QueueElemType *data;
int front;
int rear;
}SqQueue;

Status InitQueue(SqQueue **pQ);
Status DestoryQueue(SqQueue *Q);
Status ClearQueue(SqQueue *Q);
bool QueueEmpty(SqQueue *Q);
bool QueueFull(SqQueue *Q);
Status GetHead(SqQueue *Q, QueueElemType *e);
Status EnQueue(SqQueue *Q, QueueElemType e);
Status DeQueue(SqQueue *Q, QueueElemType *e);
void PrintALLQueue(SqQueue *Q);
void PrintQueue(SqQueue *Q);

int main()
{
int status = -1;
SqQueue *Q = NULL;
status = InitQueue(&Q);
if (status == ERROR)
{
printf("func InitQueue() error!\n");
}
PrintALLQueue(Q);
PrintQueue(Q);

int choice = -1;
int e = 0;
while (1)
{
printf("1 添加 2 删除 0 退出\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Please enter e:");
scanf("%d", &e);
status = EnQueue(Q, e);
if (status == ERROR)
{
printf("FULL!\n");
}
PrintALLQueue(Q);
PrintQueue(Q);
break;
case 2:
status = DeQueue(Q, &e);
if (status == ERROR)
{
printf("EMPTY!\n");
}
else
printf("The number is %d.\n", e);
PrintALLQueue(Q);
PrintQueue(Q);
break;
case 0:exit(0);
}
}
DestoryQueue(Q);
Q = NULL;
return 0;
}
Status InitQueue(SqQueue **pQ)
{
SqQueue *Q;
Q = (SqQueue*)malloc(sizeof(SqQueue));
if(Q == NULL)
{
printf("分配内存失败!\n");
return ERROR;
}
Q->data = (QueueElemType*)malloc(MAXSIZE * sizeof(QueueElemType));
if (Q->data == NULL)
{
printf("分配内存失败!\n");
return ERROR;
}
Q->front = 0;
Q->rear = 0;
*pQ = Q;
return OK;
}
Status DestoryQueue(SqQueue *Q)
{
if (Q->data != NULL)
{
free(Q->data);
Q->data = NULL;
return OK;
}
else
return ERROR;
}
Status ClearQueue(SqQueue *Q)
{
Q->front = 0;
Q->rear = 0;
return OK;
}
bool QueueEmpty(SqQueue *Q)
{
if (Q->front == Q->rear)
return true;
else
return false;
}
bool QueueFull(SqQueue *Q)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
return true;
else
return false;
}
Status GetHead(SqQueue *Q, QueueElemType *e)
{
if (QueueEmpty(Q) == true)
{
return ERROR;
}
else
*e = Q->data[Q->front];
return OK;
}
Status EnQueue(SqQueue *Q, QueueElemType e)

{
if (QueueFull(Q) == true)
return ERROR;
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return OK;
}
Status DeQueue(SqQueue *Q, QueueElemType *e)
{
if (QueueEmpty(Q) == true)
{
return ERROR;
}

*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;

return OK;
}
Status QueueLength(SqQueue *Q, int *length)
{
*length = (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
return OK;
}
void PrintALLQueue(SqQueue *Q)
{
for (int i = 0; i < MAXSIZE; i++)
{
printf("%d ", Q->data[i]);
}
putchar('\n');
}
void PrintQueue(SqQueue *Q)
{
int status = -1;
int length = 0;
QueueLength(Q, &length);
for (int i = 0; i < length; i++)
{
printf("%d ", (Q->data[(Q->front + i) % MAXSIZE]));
}
putchar('\n');
}