设计实现双端队列。
你的实现需要支持以下操作:
MyCircularDeque(k):构造函数,双端队列的大小为k。
insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。
insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。
deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。
isEmpty():检查双端队列是否为空。
isFull():检查双端队列是否满了。
示例:
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已经满了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 4
提示:
所有值的范围为 [1, 1000]
操作次数的范围为 [1, 1000]
请不要使用内置的双端队列库。
基础题,注意边界值,双向链表,一个头节点,一个尾节点
#include <iostream>
using namespace std;
class MyCircularDeque {
private:
struct node{
int val;
struct node* next;
struct node* pre;
node(int x):val(x),next(NULL),pre(NULL){}
};
int now_cap;
int total_cap;
node* head;
node* tail;
public:
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
now_cap = 0;
total_cap = k;
head = new node(0);
tail = new node(0);
head->next = tail;
tail->pre = head;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if(now_cap<total_cap){
now_cap++;
node* insert = new node(value);
if(head->next!=NULL){
node* first = head->next;
head->next = insert;
insert->pre = head;
insert->next = first;
first->pre = insert;
}else{
head->next =insert;
insert->pre = head;
}
return true;
}else{
return false;
}
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if(now_cap<total_cap){
now_cap++;
node* insert = new node(value);
node* pre = tail->pre;
pre->next =insert;
insert->pre = pre;
insert->next = tail;
tail->pre = insert;
return true;
}else{
return false;
}
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if(now_cap<=0)
return false;
if(head->next==NULL)
return false;
else{
now_cap--;
node* first =head->next;
node* second = first->next;
first->next =NULL;
first->pre = NULL;
if(second==NULL){
head->next = NULL;
}else{
head->next = second;
second->pre = head;
}
delete(first);
return true;
}
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if(now_cap<=0)
return false;
node *last =tail->pre;
node *pre = last->pre;
pre->next = tail;
tail->pre = pre;
last->next = NULL;
last->pre =NULL;
now_cap--;
delete(last);
return true;
}
/** Get the front item from the deque. */
int getFront() {
if(head->next!=tail){
return head->next->val;
}else{
return -1;
}
}
/** Get the last item from the deque. */
int getRear() {
if(tail->pre!=head){
return tail->pre->val;
}else{
return -1;
}
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
if(now_cap==0)
return true;
else
return false;
}
/** Checks whether the circular deque is full or not. */
bool isFull() {
if(now_cap==total_cap)
return true;
else
return false;
}
// void print(){
// while(head->next!=NULL){
// cout<<"test:"<<head->next->val<<endl;
// head =head->next;
// }
// }
};
int main(){
// MyCircularDeque* obj = new MyCircularDeque(3);
// cout<<obj->insertLast(1)<<endl;
// cout<<obj->insertLast(2)<<endl;
// cout<<obj->insertFront(3)<<endl;
// cout<<obj->insertFront(4)<<endl;
// cout<<obj->getRear()<<endl;
// cout<<obj->isFull()<<endl;
// cout<<obj->deleteLast()<<endl;
// cout<<obj->insertFront(4)<<endl;
// cout<<obj->getFront()<<endl;
MyCircularDeque* obj = new MyCircularDeque(4);
cout<<obj->insertFront(9)<<endl;
cout<<obj->deleteLast()<<endl;
cout<<obj->getRear()<<endl;
cout<<obj->getFront()<<endl;
cout<<obj->getFront()<<endl;
cout<<obj->deleteFront()<<endl;
// cout<<obj->insertFront(3)<<endl;
// cout<<obj->insertFront(4)<<endl;
// cout<<obj->isFull()<<endl;
// cout<<obj->deleteLast()<<endl;
// cout<<obj->insertFront(4)<<endl;
// cout<<obj->getFront()<<endl;
return 0;
}