【数据结构】循环队列
Time Limit: 1 s
Memory Limit: 128 MB
Submission:126
AC:36
Score:98.32
Description
对于给定的代码,完成循环队列。
ps:当队列中有9个元素的时候,队列视为满
你只需要提交函数
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
#include<stdio.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
typedef int BOOL;
static const int MAXN = 10;
struct Queue
{
int Q[10];
int rear;
int front;
int size;
};
typedef struct Queue Queue;
void InitQueue(Queue *q);
BOOL QueueIsEmpty(Queue *q);
int QueueSize(Queue *q);
void PushQueue(Queue *q, int value);
int PopQueue(Queue *q);
int FrontQueue(Queue *q);
int main()
{
int n;
char ctl[5];
Queue Q1;
InitQueue(&Q1);
scanf("%d", &n);
while (n--)
{
scanf("%s", ctl);
if (strcmp(ctl, "push") == 0)
{
int x;
scanf("%d", &x);
if (QueueSize(&Q1) <= 8)PushQueue(&Q1, x);
else printf("Queue is full.\n");
}
else if (strcmp(ctl, "pop") == 0)
{
if (!QueueIsEmpty(&Q1))printf("%d\n", FrontQueue(&Q1)), PopQueue(&Q1);
else printf("Queue is empty.\n");
}
}
}
Input
Output
Samples
input
20
push 1
push 2
push 3
push 4
push 5
push 6
push 7
push 8
push 9
push 10
pop
pop
pop
pop
pop
pop
pop
pop
pop
pop
output
Queue is full.
1
2
3
4
5
6
7
8
9
Queue is empty.