-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
76 lines (59 loc) · 1.44 KB
/
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
#include <stdlib.h>
#include <string.h>
#include "queue.h"
QUEUE * queue_init(QUEUE *q) {
if(q == NULL)
q = (QUEUE*)malloc(sizeof(QUEUE));
if(q == NULL)
return NULL;
q->size = QUEUE_SIZE;
q->arr = malloc(sizeof(void*) * q->size);
q->start = 0;
q->end = 0;
return q;
}
int enqueue(QUEUE *q, void *val) {
void **tmp;
int count;
if(q->start == (q->end + 1) % q->size) {
// 원소를 하나 더 추가하면 큐가 꽉 차는 경우
// 큐를 확장한다
tmp = malloc(sizeof(void*) * (q->size * 2));
if(tmp == NULL)
return -1;
if(q->end < q->start) {
// 한바퀴 돌아서 end가 start보다 앞에 있을 경우
memcpy(tmp, q->arr + q->start, sizeof(void*) * (q->size - q->start));
memcpy(tmp + q->size - q->start, q->arr, sizeof(void*) * q->end);
count = q->size - q->start + q->end;
} else {
// 한바퀴 돌지 않은 경우
memcpy(tmp, q->arr + q->start, sizeof(void*) * (q->end - q->start));
count = q->end - q->start;
}
free(q->arr);
q->arr = tmp;
q->size *= 2;
q->start = 0;
q->end = count;
}
q->arr[q->end] = val;
q->end = (q->end + 1) % q->size;
return 0;
}
void *dequeue(QUEUE *q) {
void *res;
if(q->start == q->end)
return NULL;
res = q->arr[q->start];
q->start = (q->start + 1) % q->size;
return res;
}
int left_in_queue(QUEUE *q) {
if(q->start == q->end)
return 0;
else if(q->start < q->end)
return q->end - q->start;
else
return q->size - q->start + q->end;
}