题目
MyCircularDeque(k):构造函数,双端队列的大小为k。
insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。
insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。
deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。
isEmpty():检查双端队列是否为空。
isFull():检查双端队列是否满了。
思考
- 使用 (下标+容量 +/- 1) % 容量 取于的方式实现下标循环
- 用size存队列的大小,count计算是否存满
代码
typedef struct {
int* arr;
int head;
int last;
int size;
int count;
} MyCircularDeque;
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque* myCircularDequeCreate(int k) {
MyCircularDeque* obj = malloc(sizeof(MyCircularDeque));
obj->arr = malloc(sizeof(int) * k);
obj->head = 0;
obj->last = 0;
obj->count = 0;
obj->size = k;
return obj;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertFront(MyCircularDeque* obj, int value) {
if (obj->count == obj->size) return false;
obj->head = (obj->head - 1 + obj->size) % obj->size;
obj->arr[obj->head] = value;
obj->count++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeInsertLast(MyCircularDeque* obj, int value) {
if (obj->count == obj->size) return false;
obj->arr[obj->last] = value;
obj->last = (obj->last + 1 + obj->size) % obj->size;
obj->count++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteFront(MyCircularDeque* obj) {
if (obj->count == 0) return false;
obj->head = (obj->head + 1) % obj->size;
obj->count--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool myCircularDequeDeleteLast(MyCircularDeque* obj) {
if (obj->count == 0) return false;
obj->last = (obj->last - 1 + obj->size) % obj->size;
obj->count--;
return true;
}
/** Get the front item from the deque. */
int myCircularDequeGetFront(MyCircularDeque* obj) {
if (obj->count == 0) return -1;
return obj->arr[obj->head];
}
/** Get the last item from the deque. */
int myCircularDequeGetRear(MyCircularDeque* obj) {
if (obj->count == 0) return -1;
return obj->arr[(obj->last - 1 + obj->size) % obj->size];
}
/** Checks whether the circular deque is empty or not. */
bool myCircularDequeIsEmpty(MyCircularDeque* obj) {
return obj->count == 0;
}
/** Checks whether the circular deque is full or not. */
bool myCircularDequeIsFull(MyCircularDeque* obj) {
return obj->count == obj->size;
}
void myCircularDequeFree(MyCircularDeque* obj) {
free(obj->arr);
free(obj);
return;
}
/**
* Your MyCircularDeque struct will be instantiated and called as such:
* MyCircularDeque* obj = myCircularDequeCreate(k);
* bool param_1 = myCircularDequeInsertFront(obj, value);
* bool param_2 = myCircularDequeInsertLast(obj, value);
* bool param_3 = myCircularDequeDeleteFront(obj);
* bool param_4 = myCircularDequeDeleteLast(obj);
* int param_5 = myCircularDequeGetFront(obj);
* int param_6 = myCircularDequeGetRear(obj);
* bool param_7 = myCircularDequeIsEmpty(obj);
* bool param_8 = myCircularDequeIsFull(obj);
* myCircularDequeFree(obj);
*/