//双链表
public static class Node<V>{
public V value;
public Node<V> last;
public Node<V> next;
public Node(V v){
value = v;
last = null;
next = null;
}
}
//双端队列 前后皆可进出
public static class MyDeque<V>{
private Node<V> head;
private Node<V> tail;
private int size;
public MyDeque(){
head = null;
tail = null;
size = 0;
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
//从头进
public void pushHead(V value){
Node<V> cur = new Node<>(value);
if (head == null){
head = cur;
tail = cur;
}else{
cur.next = head;
head.last = cur;
head = cur;
}
size++;
}
//从尾进
public void pushTail(V value){
Node<V> cur = new Node<>(value);
if (head == null){
head = cur;
tail = cur;
}else{
tail.next = cur;
cur.last = tail;
tail = cur;
}
size++;
}
//从头出
public V pollHead(){
V ans = null;
if (head == null)
return null;
size--;
ans = head.value;
if (head == tail){
head = null;
tail = null;
}else{
head = head.next;
head.last = null;
}
return ans;
}
//从尾出
public V pollTail(){
V ans = null;
if (head == null)
return null;
size--;
ans = tail.value;
if (head == tail){
head = null;
tail = null;
}else{
tail = tail.last;
tail.next = null;
}
return ans;
}
//从头查
public V peekHead(){
if (head != null)
return head.value;
else
return null;
}
//从尾查
public V peekTail(){
if (head != null)
return tail.value;
else
return null;
}
}
标签:Node,head,cur,队列,双端,tail,双链,null,public
From: https://blog.51cto.com/u_5774355/6196397