-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_circular_queue.c
81 lines (64 loc) · 2.47 KB
/
_circular_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
typedef struct {
int* items;
unsigned int head;
unsigned int tail;
unsigned int cap;
} MyCircularQueue;
/** Initialize your data structure here. Set the size of the queue to be k. */
MyCircularQueue* myCircularQueueCreate(int k) {
printf("NEW: %d\n", k);
MyCircularQueue* queue = malloc(sizeof(MyCircularQueue));
queue -> items = malloc(sizeof(int)*(k+1));
queue -> head = 0;
queue -> tail = 1;
queue -> cap = k+1;
return queue;
}
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueIsEmpty(MyCircularQueue* 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 -> items[obj -> tail] = value;
obj -> tail = (obj -> tail + 1) % (obj -> cap);
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 -> head = (obj -> head + 1) % (obj -> cap); return true;
}
/** Get the front item from the queue. */
int myCircularQueueFront(MyCircularQueue* obj) {
return (myCircularQueueIsEmpty(obj)) ? -1 : obj -> items[(obj -> head + 1) % obj -> cap];
}
/** Get the last item from the queue. */
int myCircularQueueRear(MyCircularQueue* obj) {
printf("TAIL: %d\n", obj -> tail);
return (myCircularQueueIsEmpty(obj)) ? -1 : obj -> items[(obj -> tail != 0) ? (obj -> tail - 1) : (obj -> cap - 1)];
}
/** Checks whether the circular queue is empty or not. */
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return ((((obj -> head) + 1) % (obj -> cap)) == (obj -> tail % obj -> cap));
}
/** Checks whether the circular queue is full or not. */
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return ((obj -> head) == (obj -> tail) % (obj -> cap));
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj -> items);
free(obj);
}
/**
* Your MyCircularQueue struct will be instantiated and called as such:
* struct 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);
*/