利用双向链表实现队列和栈
public class BlinkConversion<T> {
public Node<T> head;
public Node<T> tail;
//从头部加节点
public void addFromHead(T value){
Node<T> cur = new Node<>(value);
if(head==null){
head =cur;
tail =cur;
}else{
cur.next = head;
head.pre = cur;
head = cur;
}
}
//从尾部加节点
public void addFromTail(T value){
Node<T> cur = new Node<>(value);
if(head == null){
head = cur;
tail = cur;
}else{
tail.next = cur;
cur.pre = tail;
tail = cur;
}
}
//从头部弹出
public T popFromHead(){
if(head == null){
return null;
}
Node<T> cur = head;
if(head == tail){
head = null;
tail = null;
}else{
head = head.next;
head.pre = null;
cur.next = null;
}
return cur.value;
}
//从尾部取出节点
public T popFromTail(){
if(head == null){
return null;
}
Node<T> cur = tail;
if(head == tail){
head = null;
tail = null;
}else{
tail = tail.pre;
tail.next = null;
cur.pre = null;
}
return cur.value;
}
public boolean isEmpty(){
return head == null;
}
public class Node<T>{
public T value;
public Node pre;
public Node next;
public Node(T data){
value = data;
}
}
}