-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Circular_queue.c
83 lines (73 loc) · 1.57 KB
/
4Circular_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*************************************************************************
> File Name: 3queue.c
> Author: snowflake
> Mail: ︿( ̄︶ ̄)︿
> Created Time: 2018年08月17日 星期五 15时10分01秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Queue {
int *data;
int size;
int count;
int head, tail;
} Queue;
Queue *init(int n) {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->data = (int *)malloc(sizeof(Queue) * n);
q->head = 0;
q->tail = -1;
q->count = 0;
q->size = n;
return q;
}
int push(Queue *q, int data) {
if (q->count >= q->size) return 0;
q->tail = (q->tail + 1) % q->size;
q->data[q->tail] = data;
q->count++;
return 1;
}
int empty(Queue *q) {
return q->count == 0;
}
int pop(Queue *q) {
if (q->count <= 0) return 0;
q->head = (q->head + 1) % q->size;
q->count--;
return 1;
}
int front(Queue *q) {
return q->data[q->head];
}
void clear(Queue *q) {
free(q->data);
free(q);
return ;
}
void output(Queue *q) {
printf("queue:[");
for (int i = 0, h = q->head; i < q->count; i++, h = (h + 1) % q->size) {
printf("%d ", q->data[h]);
}
printf("]");
return ;
}
int main() {
srand(time(0));
Queue *q = init(20);
for (int i = 0; i < 20; i++) {
push(q, i);
}
output(q);
for (int i = 0; i < 5; i++) {
pop(q);
}
output(q);
for (int i = 0; i < 4; i++) {
push(q, i);
}
output(q);
return 0;
}