import java.util.Iterator;
public class Deque<Item> implements Iterable<Item> {
private class Node {
Item item;
Node next;
Node pre;
}
private class QueueIterator implements Iterator<Item> {
public boolean hasNext() {
return current != null;
}
public Item next() {
Item tmp = current.item;
current = current.next;
return tmp;
}
private Node current = front;
}
public Iterator<Item> iterator() {
return new QueueIterator();
}
boolean isEmtpy() {
return front == null;
}
int size() {
return n;
}
void pushLeft(Item x) {
Node oldfront = front;
front = new Node();
front.item = x;
front.next = oldfront;
front.pre = null;
if(oldfront == null)
rear = front;
else
oldfront.pre = front;
n++;
}
void pushRight(Item x) {
Node oldrear = rear;
rear = new Node();
rear.item = x;
rear.next = null;
rear.pre = oldrear;
if(oldrear != null)
oldrear.next = rear;
else
front = rear;
n++;
}
Item popLeft() {
if(isEmtpy())
return null;
Item tmp = front.item;
front = front.next;
if(front == null)
rear = null;
else
front.pre = null;
n--;
return tmp;
}
Item popRight() {
if(isEmtpy())
return null;
Item tmp = rear.item;
rear = rear.pre;
if(rear != null)
rear.next = null;
else
front = null;
n--;
return tmp;
}
private Node front;
private Node rear;
int n;
}
Ingsuifon
发布了4 篇原创文章 · 获赞 0 · 访问量 49
私信
关注