-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.1.cpp
More file actions
78 lines (70 loc) · 1.32 KB
/
5.1.cpp
File metadata and controls
78 lines (70 loc) · 1.32 KB
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
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int front;
int rear;
} SqQueue;
void InitQueue(SqQueue &Q) // 初始化
{
Q.front = 0;
Q.rear = 0;
}
void EnQueue(SqQueue &Q, ElemType e) // 进队
{
if ((Q.rear + 1) % MaxSize == Q.front)
{
printf("队列满\n");
return;
}
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MaxSize;
}
void DeQueue(SqQueue &Q, ElemType &e) // 出队
{
if (Q.front == Q.rear)
{
printf("队列空\n");
return;
}
e = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
}
void DestroyQueue(SqQueue &Q) // 销毁队列
{
Q.front = 0;
Q.rear = 0;
}
bool QueueEmpty(SqQueue &Q) // 判空
{
if (Q.front == Q.rear)
return true;
else
return false;
}
int QueueLength(SqQueue &Q) // 队列长度
{
return (Q.rear - Q.front + MaxSize) % MaxSize;
}
int main()
{
SqQueue Q;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
EnQueue(Q, 6);
ElemType e;
DeQueue(Q, e);
printf("出队元素: %d\n", e);
DeQueue(Q, e);
printf("出队元素: %d\n", e);
printf("当前队列长度: %d\n", QueueLength(Q));
DestroyQueue(Q);
return 0;
}