[英]Removing/undoing the only node in a doubly linked list
My code undos the added elements one by one starting with the latest add.我的代码从最新的添加开始,一一撤消添加的元素。 It does everything correctly unless there is only one node left in a list and my code does not undo/remove it from the list.除非列表中只剩下一个节点并且我的代码没有从列表中撤消/删除它,否则它会正确执行所有操作。 For example:例如:
[A, B, C, D, E] //call undo()
[A, B, C, D] //call undo()
[A, B, C] //call undo()
[A, B] //call undo()
[A] //call undo() and it throws Exception here <------------------
Exception in thread "main" java.lang.NullPointerException
If I undo [A]
it should return empty list []
.如果我撤消[A]
它应该返回空列表[]
。 NOTE: I HAVE TO USE A DUMMY NODE called beginMarker
and endMarker
which have a value of null
, so the last element looks like this:注意:我必须使用一个名为beginMarker
和endMarker
节点,它们的值为null
,所以最后一个元素看起来像这样:
beginMarker <-> "A" <-> endMarker
For the last element, the code checks if the size equals to 1
and it's true and proceeds further but does not empty out the list.对于最后一个元素,代码检查大小是否等于1
并且它为真并继续进行但不会清空列表。 Any help would be appreciated!任何帮助,将不胜感激!
public void add(x){
.........
undoStack.push(newNode);
}
public void undo(){
if(undoStack.isEmpty()){
throw new RuntimeException("Undo history is empty");
}
else{
Node<T> object = undoStack.topAndPop();
redoStack.push(object);
if(this.size() == 1){
beginMarker = object.next;
beginMarker.next = null;
//beginMarker.next = null;
}
if(object.prev == beginMarker){
beginMarker.next = object.next.prev;
object.next.prev = beginMarker;
}
else if(object.next == null){
object.prev.next = null;
}
else{
object.next.prev = object.prev;
object.prev.next = object.next;
}
theSize--;
modCount--;
countUndone++;
}
Class SimpleStack类SimpleStack
public class SimpleStack<AnyType>{
// Tracks the top node of stack.
private Node<AnyType> topOfStack;
// Construct the stack.
public SimpleStack( ) {
topOfStack = null;
}
// Test if the stack is logically empty.
// @return true if empty, false otherwise.
public boolean isEmpty( ) {
return topOfStack == null;
}
// Make the stack logically empty.
public void clear( ) {
topOfStack = null;
}
// Insert a new item into the stack.
// @param x the item to insert.
public void push( AnyType x ) {
topOfStack = new Node<AnyType>( x, topOfStack );
}
// Remove the most recently inserted item from the stack.
// @throws UnderflowException if the stack is empty.
public void pop( ) {
if( isEmpty( ) )
throw new RuntimeException( "SimpleStack pop" );
topOfStack = topOfStack.next;
}
// Get the most recently inserted item in the stack.
// Does not alter the stack.
// @return the most recently inserted item in the stack.
// @throws UnderflowException if the stack is empty.
public AnyType getTop( ) {
if( isEmpty( ) )
throw new RuntimeException( "SimpleStack empty in getTop" );
return topOfStack.data;
}
// Return and remove the most recently inserted item
// from the stack.
// @return the most recently inserted item in the stack.
// @throws UnderflowException if the stack is empty.
public AnyType topAndPop( ) {
if( isEmpty( ) )
throw new RuntimeException( "SimpleStack empty in topAndPop" );
AnyType topItem = topOfStack.data;
topOfStack = topOfStack.next;
return topItem;
}
// A singly linked Node which contains data and a link to another
public class Node<T>{
public T data;
public Node<T> next;
public Node(T d, Node<T> n ){
this.data = d;
this.next = n;
}
}
}
I would not be surprised the NPE occurs here :我不会对 NPE 发生在这里感到惊讶:
if(object.prev == beginMarker){
beginMarker.next = object.next.prev;
// ^^^^
// is it here ? object.next is null in your case
object.next.prev = beginMarker;
}
It should fix it to simply write它应该修复它以简单地写
if(object.prev == beginMarker && object.next != null) {
beginMarker.next = object.next.prev;
object.next.prev = beginMarker;
}
However, you don't really have to particularize the case size() == 1
.但是,您实际上不必特别说明 case size() == 1
。 A simpler implementation would be (assuming you add a pointer to the end of the list) :一个更简单的实现是(假设您添加一个指向列表末尾的指针):
public void undo() {
if (undoStack.isEmpty()) {
throw new NoSuchElementException("Undo history is empty");
} else {
Node<T> object = undoStack.topAndPop();
redoStack.push(object);
object.prev.next = object.next;
object.next.prev = object.prev;
theSize--;
modCount--;
countUndone++;
}
}
This should work since using an endMarker
will prevent you from having any null
node in the list.这应该有效,因为使用endMarker
会阻止您在列表中包含任何null
节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.