-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
151 lines (136 loc) · 2.68 KB
/
Copy pathqueue.c
File metadata and controls
151 lines (136 loc) · 2.68 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
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
/*
* =====================================================================================
*
* Filename: queue.c
*
* Description:
*
* Version: 1.0
* Created: 2012年10月05日 16时26分19秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <errno.h>
#include <string.h>
typedef int ElementType;
struct QueueRecord {
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
} /* optional variable list */ ;
typedef struct QueueRecord *Queue;
#define MiniQueueSize (5)
static int IsEmpty(Queue Q)
{
return Q->Size == 0;
}
static int IsFull(Queue Q)
{
return Q->Size == Q->Capacity;
}
static void MakeEmpty(Queue Q)
{
Q->Front = 1;
Q->Rear = 0;
Q->Size = 0;
}
static Queue CreatQueue(int MaxElements)
{
Queue Q;
if (MaxElements < MiniQueueSize) {
error_at_line(1, (int) NULL, __FILE__, __LINE__, "%s",
"Queue size is too small!!!");
}
Q = malloc(sizeof(struct QueueRecord));
if (Q == NULL) {
error_at_line(1, (int) NULL, __FILE__, __LINE__, "%s",
"Out of space!!!");
}
Q->Array = malloc(sizeof(ElementType) * MaxElements);
if (Q->Array == NULL) {
error_at_line(1, (int) NULL, __FILE__, __LINE__, "%s",
"Out of space!!!");
}
Q->Capacity = MaxElements;
MakeEmpty(Q);
return Q;
}
static void DisposeQueue(Queue Q)
{
if (Q != NULL) {
free(Q->Array);
free(Q);
}
}
static int Succ(int value, Queue Q)
{
if (++value == Q->Capacity) {
value = 0;
}
return value;
}
static void Enqueue(ElementType X, Queue Q)
{
if (IsFull(Q)) {
error_at_line(1, (int) NULL, __FILE__, __LINE__, "%s",
"Full queue!!!");
} else {
Q->Size++;
Q->Rear = Succ(Q->Rear, Q);
Q->Array[Q->Rear] = X;
}
}
static ElementType Front(Queue Q)
{
return Q->Array[Q->Front];
}
static void Dequeue(Queue Q)
{
if (IsEmpty(Q)) {
error_at_line(1, (int) NULL, __FILE__, __LINE__, "%s",
"Empty queue!!!");
} else {
Q->Size--;
Q->Front = Succ(Q->Front, Q);
}
}
static ElementType FrontAndDequeue(Queue Q)
{
ElementType Temp;
if (IsEmpty(Q)) {
error_at_line(1, (int) NULL, __FILE__, __LINE__, "%s",
"Empty queue!!!");
} else {
Q->Size--;
Temp = Q->Array[Q->Front];
Q->Front = Succ(Q->Front, Q);
return Temp;
}
}
void queue_test(void)
{
int i;
Queue Q;
Q = CreatQueue(16);
printf("Enqueue:\n");
for (i = 0; i < 16; i++) {
Enqueue(i, Q);
printf("%d ", i);
}
printf("\n");
printf("Dequeue:\n");
for (i = 0; i < 20; i++) {
printf("%d ", FrontAndDequeue(Q));
}
printf("\n");
}