-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCircularQueue
More file actions
87 lines (73 loc) · 2.09 KB
/
MyCircularQueue
File metadata and controls
87 lines (73 loc) · 2.09 KB
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
84
85
86
87
typedef struct {
int* arry;//队列数组
int front;//队头下标
int rear;//队尾下标
int k;//队列大小
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* q = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
//初始化队列
//为队列数组开辟空间,大小为k的队列需开辟k+1个节点
q->arry = (int*)malloc(sizeof(int)*(k+1));
q->front = q->rear = 0;
q->k = k;
return q;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
//判断队列是否为满,如果满返回false
if(myCircularQueueIsFull(obj))
{
return false;
}
//入队
obj->arry[obj->rear] = value;
//队尾指针后移
obj->rear = (obj->rear + 1)%(obj->k+1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
obj->front = (obj->front + 1)%(obj->k + 1);
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
return obj->arry[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
int tail = obj->rear - 1;
if(tail == -1)
tail = obj->k;
return obj->arry[tail];
}
int myCircularQueueIsEmpty(MyCircularQueue* obj) {
if(obj->rear == obj->front)
return true;
return false;
}
int myCircularQueueIsFull(MyCircularQueue* obj) {
if((obj->rear + 1)%(obj->k + 1) == obj->front)
return true;
return false;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->arry);
free(obj);
}
/**
* 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);
*/