package dequeue;
import java.util.Iterator;
public class Deque<Item> implements Iterable<Item> {
public class Node{
Item value = null;
Node next = null;
Node prev = null;
}
public class terminal extends Node{
private int cp;
terminal(int cp){
this.cp = cp;
}
}
public terminal head = new terminal(-1);
public terminal tail = new terminal(-1);
private int size;
public Deque(){
head.next = tail;
tail.prev = head;
size = 0;
}
public boolean isEmpty() {
return size == 0 ;
}
public int size() {
return size;
}
public void addFirst(Item item) {
Node tmp = new Node();
tmp.value = item;
tmp.prev = head;
tmp.next = head.next;
head.next = tmp;
size++;
}
public void addLast(Item item){
// add the item to the end
Node tmp = new Node();
tmp.value = item;
tmp.prev = tail.prev;
tail.prev.next = tmp;
【JAVA数据结构】带dummy节点的双向队列
最新推荐文章于 2023-06-10 16:24:25 发布