我目前正在使用ListNodes定义我自己的Deque类myDeque
。我面临的问题涉及remove()
方法。你如何修改Deque指向后面的节点并删除曾经“返回”的节点?谢谢
private ListNode<E> front; // Reference to first ListNode in the Deque
private ListNode<E> back; // Reference to last ListNode in the Deque
private int numElems; // Current number of ListNodes in the Deque
public MyDeque() {
front = null;
back = null;
numElems = 0;
}
public void addLast(E e) {
if (numElems == 0){
front = new ListNode<E>(e,null);
back = front;
} else {
back.next = new ListNode<E>(e,null);
back = back.next;
}
numElems += 1;
}
public boolean add(E e) {
if (numElems == 0){
front = new ListNode<E>(e,null);
back = front;
} else {
ListNode<E> prevData = back;
back.next = new ListNode<E>(e,null);
back = back.next;
}
numElems += 1;
return true;
}
public boolean remove(Object o) {
if (numElems == 0){
throw new NoSuchElementException();
}
return false;
}
public E removeLast() {
if (numElems == 0){
throw new NoSuchElementException();
}
else{
numElems = numElems - 1;
}
return null;
}
答案 0 :(得分:0)
back = back.back; //modify the Deque to point to the node behind back
back.next = null; //and delete what used to be "back"