-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularBuffer.c
75 lines (60 loc) · 2.14 KB
/
CircularBuffer.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
typedef struct {
int head, tail, count, size;
int* q;
} MyCircularQueue;
/** Initialize your data structure here. Set the size of the queue to be k. */
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* queue = malloc(sizeof *queue);
queue->q = malloc(sizeof(int) * k);
queue->head = queue->tail = queue->count = 0;
queue->size = k;
return queue;
}
/** Checks whether the circular queue is empty or not. */
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->count == 0;
}
/** Checks whether the circular queue is full or not. */
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return obj->count == obj->size;
}
/** Get the front item from the queue. */
int myCircularQueueFront(MyCircularQueue* obj) {
if (myCircularQueueIsEmpty(obj)) return -1;
return obj->q[obj->head];
}
/** Get the last item from the queue. */
int myCircularQueueRear(MyCircularQueue* obj) {
if (myCircularQueueIsEmpty(obj)) return -1;
int id = (obj->tail - 1 + obj->size) % obj->size;
return obj->q[id];
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj);
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if (myCircularQueueIsFull(obj)) return false;
obj->q[obj->tail] = value;
obj->tail = (obj->tail+1) % obj->size;
obj->count++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if (myCircularQueueIsEmpty(obj)) return false;
obj->count--;
obj->head = (obj->head + 1) % obj->size;
return true;
}
/**
* Your MyCircularQueue struct will be instantiated and called as such:
* MyCircularQueue* obj = myCircularQueueCreate(k);
* bool param_1 = myCircularQueueEnQueue(obj, value);
* bool param_2 = myCircularQueueDeQueue(obj);
* int param_3 = myCircularQueueFront(obj);
* int param_4 = myCircularQueueRear(obj);
* bool param_5 = myCircularQueueIsEmpty(obj);
* bool param_6 = myCircularQueueIsFull(obj);
* myCircularQueueFree(obj);
*/