算法记录
LeetCode 题目:
??设计实现双端队列。
说明
一、题目
??实现相关的方法。
二、分析
class Node{
Integer value;
Node first;
Node last;
public Node(int val) {
value = val;
first = null;
last = null;
}
}
class MyCircularDeque {
private Integer size;
private Integer count;
private Node front;
private Node rear;
public MyCircularDeque(int k) {
size = k;
count = 0;
}
public boolean insertFront(int value) {
if(isFull()) return false;
Node temp = new Node(value);
if(isEmpty()) {
front = rear = temp;;
} else {
temp.last = front;
front.first = temp;
front = front.first;
}
count++;
return true;
}
public boolean insertLast(int value) {
if(isFull()) return false;
Node temp = new Node(value);
if(isEmpty()) {
front = rear = temp;
} else {
rear.last = temp;
temp.first = rear;
rear = rear.last;
}
count++;
return true;
}
public boolean deleteFront() {
if(isEmpty()) return false;
front = front.last;
count--;
return true;
}
public boolean deleteLast() {
if(isEmpty()) return false;
rear = rear.first;
count--;
return true;
}
public int getFront() {
if(isEmpty()) return -1;
return front.value;
}
public int getRear() {
if(isEmpty()) return -1;
return rear.value;
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == size;
}
}
总结
熟悉队列的存储性质。
|