設計實現雙端隊列。
你的實現需要支持以下操作:
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;
}