每次运行Doubly Linked List时,除了从列表后面删除之外,所有方法都有效。我有一个4,3,9的List。我从前面移除了(它拿走了4)。然后,我调用了这个应该只删除了9的方法。相反,当我调用DL列表时,它返回null(也删除了3)。请帮助。
public Object removeLast() {
Object result = null;
if (!isEmpty()) {
result = lastNode.getData();
lastNode = lastNode.getPreviousNode();
if (lastNode == null)
firstNode = null;
else
lastNode.setNextNode(null);
}
return result;
}
以下是代码的其余部分(扩展了一个接口;生成的代码对于问题不是必需的,所以我们还没有填写它...即Node之后的东西无关紧要)正确的代码:
public class DList implements DoubleEndedListInterface {
public Node lastNode;
public Node firstNode; // reference to first node
public int numberOfEntries;
@Override
public String toString() {
return "" + firstNode;
}
public Node getNodeAt(int givenPosition) {
assert !isEmpty() && (1 <= givenPosition)
&& (givenPosition <= numberOfEntries);
Node currentNode = firstNode;
// traverse the list to locate the desired node
for (int counter = 1; counter < givenPosition; counter++)
currentNode = currentNode.getNextNode();
assert currentNode != null;
return currentNode;
} // end getNodeAt
public void addFirst(Object newEntry) {
Node newNode = new Node(newEntry);
if (firstNode == null)
{
firstNode = newNode;
lastNode = newNode;
numberOfEntries++;
}
else
{
newNode.setNextNode(firstNode);
firstNode.setPreviousNode(newNode);
firstNode = newNode;
numberOfEntries++;
} // end if
} // end add
public void addLast(Object newEntry) {
Node newNode = new Node(newEntry);
if (firstNode == null)
{
firstNode = newNode;
//lastNode = newNode;
numberOfEntries++;
}
else
{
lastNode.setNextNode(newNode);
lastNode = newNode;
numberOfEntries++;
} // end if
} // end add
@Override
public Object removeFirst() {
Object result = firstNode.getData();
firstNode = firstNode.getNextNode();
if (numberOfEntries == 1)
lastNode = null; // solitary entry was removed
numberOfEntries--;
return result; // return removed entry, or
// null if operation fails
} // end remove
@Override
public Object removeLast() {
Object result = null;
if (!isEmpty()) {
result = lastNode.getData();
lastNode = lastNode.getPreviousNode();
if (lastNode == null)
firstNode = null;
else
lastNode.setNextNode(null);
}
return result;
}
@Override
public Object getFirst() {
// TODO Auto-generated method stub
return firstNode.getData();
}
@Override
public Object getLast() {
// TODO Auto-generated method stub
return lastNode.getData();
}
@SuppressWarnings("unchecked")
@Override
public void moveToEnd() {
Object temp = (Object) remove(1);
add(numberOfEntries + 1, temp);
}
public class Node {
public Object data; // entry in bag
public Node next; // link to next node
public Node previous;
public Node(Object dataPortion) {
this(dataPortion, null, null);
} // end constructor
public Node(Object dataPortion, Node nextNode, Node previousNode) {
data = dataPortion;
next = nextNode;
previous = previousNode;
} // end constructor
public Object getData() {
return data;
} // end getData
public void setData(Object newData) {
data = newData;
} // end setData
public Node getNextNode() {
return next;
} // end getNextNode
public Node getPreviousNode() {
return previous;
}
public void setPreviousNode(Node previous) {
this.previous = previous;
}
public void setNextNode(Node nextNode) {
next = nextNode;
} // end setNextNode
@Override
public String toString() {
return "Node [data=" + data + ", next=" + next + "]";
}
} // end Node
@Override
public void add(Object newEntry) {
// TODO Auto-generated method stub
}
@Override
public boolean add(int newPosition, Object newEntry) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object remove(int givenPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public boolean replace(int givenPosition, Object newEntry) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object getEntry(int givenPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean contains(Object anEntry) {
// TODO Auto-generated method stub
return false;
}
@Override
public int getLength() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
boolean result;
if (numberOfEntries == 0) // or getLength() == 0
{
assert firstNode == null;
result = true;
} else {
assert firstNode != null;
result = false;
} // end if
return result;
} // end isEmpty
@Override
public void swap(int i, int j) {
// TODO Auto-generated method stub
}
@Override
public boolean isTooBig() {
// TODO Auto-generated method stub
return false;
}
@Override
public void reduceArray() {
// TODO Auto-generated method stub
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public void makeRoom(int newPosition) {
// TODO Auto-generated method stub
}
@Override
public void removeGap(int givenPosition) {
// TODO Auto-generated method stub
}
@Override
public boolean removeObject(Object anObject) {
// TODO Auto-generated method stub
return false;
}
@Override
public int getPosition(Object anObject) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void append(ListInterface bList) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
removeLast
非常有意义,但可能缺少--numberOfEntries
。
其余代码我没有阅读,因为它有点太多了。
但是add
很奇怪:似乎使用局部变量lastNode
对某些字段lastNode
不执行任何操作。
答案 1 :(得分:0)
这就是我所看到的:
addFirst
看起来没问题,好像这个方法应该正常运行。
addLast
似乎不正确。特别是,如果通过addLast
将节点添加到空列表中,则lastNode
成员变量将没有有效值。
然后removeFirst
和removeLast
都需要帮助,特别是在处理numberOfEntries
成员并检查空列表时。
对于“list starts empty”条件,你还应该将“next”和“previous”指针设置为null,这样就不会有杂散数据干扰的可能性。