[英]Why does this deque not produce the expected result for addLast
I'm writing code for a deque(implemented as a doubly linked list), when I'm running the test client (the main method), the results are not as I expected.我正在为一个双端队列(实现为双向链表)编写代码,当我运行测试客户端(主要方法)时,结果与我预期的不一样。 Can anyone explain why this is occurring, and possibly, how to fix this bug?谁能解释为什么会发生这种情况,以及如何解决这个错误? I assume it either has to do with addLast
我认为它要么与addLast
有关
Expected Result:预期结果:
Task: Begin Elementary Sorts
Task: Finish Implementing RandomizedQueue
Task: Finish Implementing Deque
Task: Testing
Task: Testing
Task: Testing
Task: Testing
Result Obtained:获得的结果:
Task: Begin Elementary Sorts
Task: Finish Implementing RandomizedQueue
Task: Finish Implementing Deque
Task: null
Task: Testing
Task: Testing
Task: Testing
Code代码
// Implemented via a Doubly Linked-List.
public class Deque<Item> implements Iterable<Item> {
private int size;
private Node first, last;
private class Node
{
Item item;
Node next;
Node previous;
}
private class DequeIterator implements Iterator<Item>
{
private Node current = first;
public boolean hasNext()
{
return current.next != null;
}
public Item next()
{
if (hasNext() == false) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
// construct an empty deque
public Deque()
{
Node n = new Node();
first = n;
last = first;
}
// is the deque empty?
public boolean isEmpty(){ return size == 0; }
// return the number of items on the deque
public int size() { return size; }
// add the item to the front
public void addFirst(Item item)
{
if (item == null) throw new IllegalArgumentException();
Node n = new Node();
Node oldFirst = first;
n.item = item;
n.next = oldFirst;
first = n;
oldFirst.previous = n;
size++;
return;
}
// add the item to the back
public void addLast(Item item)
{
if (item == null) throw new IllegalArgumentException();
Node n = new Node();
last.next = n;
n.previous = last;
n.item = item;
last = n;
size++;
return;
}
// remove and return the item from the front
public Item removeFirst()
{
if (size == 0) throw new NoSuchElementException();
Node oldFirst = first;
first = first.next;
oldFirst.next = null;
first.previous = null;
size--;
return oldFirst.item;
}
// remove and return the item from the back
public Item removeLast()
{
if (size == 0) throw new NoSuchElementException();
Node oldLast = last;
last = last.previous;
last.next = null;
oldLast.previous = null;
size--;
return oldLast.item;
}
// return an iterator over items in order from front to back
public Iterator<Item> iterator(){ return new DequeIterator(); }
// unit testing (required)
public static void main(String[] args)
{
Deque<String> todo = new Deque<String>();
todo.addFirst("Finish Implementing Deque");
todo.addFirst("Finish Implementing RandomizedQueue");
todo.addFirst("Begin Elementary Sorts");
todo.addLast("Testing");
todo.addLast("Testing");
todo.addLast("Testing");
todo.addLast("Testing");
// Iterator<String> tasks = todo.iterator();
// while (tasks.hasNext())
// {
// String task = tasks.next();
// StdOut.println(task);
// }
// System.out.println(todo.removeFirst());
// System.out.println(todo.removeLast());
// System.out.println("After Removal:");
// if (!todo.isEmpty() && todo.size() != 0)
for (String task: todo)
System.out.println("Task: " + task);
}
}
Your addLast
function implementation needs to handle cases where the last.item
are null.您的addLast
function 实现需要处理last.item
为 null 的情况。
To reach the target output, you should implement a check for last.item == null
and then put that value in your last
.要达到目标 output,您应该检查last.item == null
,然后将该值放入您的last
中。
Here's my implementation:这是我的实现:
// add the item to the back
public void addLast(Item item)
{
if (item == null) throw new IllegalArgumentException();
Node n = new Node();
if (last.item == null) {
last.item = item;
}
else {
last.next = n;
n.previous = last;
n.item = item;
last = n;
}
size++;
}
Output: Output:
Task: Begin Elementary Sorts
Task: Finish Implementing RandomizedQueue
Task: Finish Implementing Deque
Task: Testing
Task: Testing
Task: Testing
Don't forget to accept the question if it solves your described issue.如果它解决了您描述的问题,请不要忘记接受该问题。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.