-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-queue.c
65 lines (53 loc) · 1.06 KB
/
array-queue.c
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
//1.array queue
// 数组队列
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define QUEUE_SIZE 10
struct queue {
unsigned int array[QUEUE_SIZE];
int count;
};
void queue_init(struct queue *q)
{
q->count = 0;
}
int queue_empty(struct queue *q)
{
return (q->count == 0);
}
int queue_full(struct queue *q)
{
return (q->count == QUEUE_SIZE);
}
int enqueue(struct queue *q, int value)
{
if (queue_full(q))
return -1;
q->array[q->count++] = value;
return 0;
}
int dequeue(struct queue *q)
{
if (queue_empty(q))
return -1;
return q->array[--q->count];
}
/**************test sample*******************/
int main(int argc, char *argv[])
{
struct queue queue;
queue_init(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
enqueue(&queue, 4);
enqueue(&queue, 5);
printf("dequeue:%d\n", dequeue(&queue));
printf("dequeue:%d\n", dequeue(&queue));
printf("dequeue:%d\n", dequeue(&queue));
printf("dequeue:%d\n", dequeue(&queue));
printf("dequeue:%d\n", dequeue(&queue));
printf("dequeue:%d\n", dequeue(&queue));
return 0;
}