不使用任何现有类(如LinkedList)的JavaDeque?
我必须在deque上写一段很短的代码,但是我不确定如何为这些方法编写代码,如果有人能帮助我使用其中一种方法,例如,在deque的from中添加一个对象的方法,那么我就可以开始了。我确信我可以管理其余的方法,就在我非常困惑的时候。我不确定您到底在追求什么,但是Deque的可用方法列在了中,Deque通常以双链接列表的形式实现。通过跟踪列表中的第一个和最后一个元素,并让每个元素跟踪其前一个和后一个元素,可以实现双链接列表不使用任何现有类(如LinkedList)的JavaDeque?,java,deque,Java,Deque,我必须在deque上写一段很短的代码,但是我不确定如何为这些方法编写代码,如果有人能帮助我使用其中一种方法,例如,在deque的from中添加一个对象的方法,那么我就可以开始了。我确信我可以管理其余的方法,就在我非常困惑的时候。我不确定您到底在追求什么,但是Deque的可用方法列在了中,Deque通常以双链接列表的形式实现。通过跟踪列表中的第一个和最后一个元素,并让每个元素跟踪其前一个和后一个元素,可以实现双链接列表 public class Deque<T> { priva
public class Deque<T> {
private class Node {
Node(T value) {
this.value = value;
}
T value;
Node next, prev;
}
private Node first, last;
public void addFront(T value) {
Node oldFirst = first;
first = new Node(value);
// The old first item is now the second item, so its the successor of
// the new first item
first.next = oldFirst;
// if first was null before, that means the deque was empty
// so first and last should both point to the new item
if(oldFirst == null) {
last = first;
} else {
// If there previously was a first element, this element is
// now the second element and its prev field should point to
// the new first item
oldFirst.prev = first;
}
}
}
你为什么这么做?使用一种标准的Deque实现有什么问题?