//双链表
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;
}
}
用双链表实现双端队列
原创
©著作权归作者所有:来自51CTO博客作者p340223的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:单链表实现队列、栈
下一篇:mysql登录密码忘了怎么办
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
4.带头节点的双链表的实现(C语言)
双链表基本运算的代码实现,前插、后插操作
双链表 头结点 插入操作 删除操作 -
Python实现双端队列
Python实现双端队列
Python实现双端队列 Python双端队列 双端队列 Python顺序双端队列 Python链双端队列 -
java实现双端队列 双端队列和栈
栈和队列是运算受限的线性表。栈:只能后进来的元素先出去。队列:只能先进来的元素先出去。
java实现双端队列 出栈 栈和队列 双端队列