无法解决Java Deque迭代错误

无法解决Java Deque迭代错误,java,iterator,doubly-linked-list,deque,reverse-iterator,Java,Iterator,Doubly Linked List,Deque,Reverse Iterator,在测试过程中,我发现了代码中的bug 我很难找到我的Deque迭代器有什么问题。它没有正确地迭代,我不知道如何修复它。我已经包括了失败测试的截图和我完成的代码 旁注:我的教授希望迭代器在进行正向或反向迭代时,将元素从头部出列,这就是为什么迭代器要复杂一点的原因 失败的单元测试屏幕截图: 非常感谢你的帮助 完整的源代码: import java.util.Iterator; import java.util.NoSuchElementException; public class Deque&l

在测试过程中,我发现了代码中的bug

我很难找到我的
Deque
迭代器有什么问题。它没有正确地迭代,我不知道如何修复它。我已经包括了失败测试的截图和我完成的代码

旁注:我的教授希望迭代器在进行正向或反向迭代时,将元素从头部出列,这就是为什么迭代器要复杂一点的原因

失败的单元测试屏幕截图:

非常感谢你的帮助

完整的源代码:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Deque<E> implements Iterable<E>
{
    private class DequeNode<E>
    {
        private DequeNode<E> _next;
        private DequeNode<E> _previous;
        private E _data;

        private DequeNode(E data)
        {
            _data = data;
        }
    }

    private DequeNode<E> _head;
    private DequeNode<E> _tail;
    private int _depth;
    private static final int EXCLUSIVE_INDEX_START = -1;


    public Deque()
    {
        _head = null;
        _tail = null;
        _depth = 0;
    }

    public boolean enqueue(E element) // This is INSERTION at the TAIL //TODO: DONE!!
    {
        DequeNode<E> newNode = new DequeNode<E>(element);

        if (isEmpty())
        {
            _head = newNode;
        }
        else
        {
            _tail._next = newNode;
            newNode._previous = _tail;
        }
        _tail = newNode;
        _depth++;

        return true;
    }

    public void enqueueAll(Iterable<E> elements)
    {
        for (E eachElement : elements)
        {
            enqueue(eachElement);
        }
    }

    public boolean enqueueHead(E element) // This is insertion at the head //TODO: DONE!!
    {
        DequeNode<E> newNode = new DequeNode<E>(element);

        if (isEmpty())
        {
            _tail = newNode;
        }
        else
        {
            _head._previous = newNode;
            newNode._next = _head;
        }
        _head = newNode;
        _depth++;

        return true;
    }

    public E head()
    {
        checkForStackUnderflow();
        return _head._data;
    }

    public E tail()
    {
        checkForStackUnderflow();
        return _tail._data;
    }

    public E dequeue() // This is removal at the head //TODO: DONE!!
    {
        checkForStackUnderflow();

        E removed = _head._data;

        if (_head._next == null)
        {
            _tail = null;
        }
        else
        {
            _head._next._previous = null;
        }
        _head = _head._next;
        _depth--;

        return removed;
    }

    public E dequeueTail() // This is removal at the tail //TODO: DONE!!
    {
        checkForStackUnderflow();

        E removed = _tail._data;

        if (_head._next == null)
        {
            _head = null;
        }
        else
        {
            _tail._previous._next = null;
        }
        _tail = _tail._previous;
        _depth--;

        return removed;
    }

    public void clear()
    {
        _head = _tail = null;
        _depth = 0;
    }

    public int depth()
    {
        return _depth;
    }

    public boolean isEmpty()
    {
        return _depth == 0;
    }

    public Iterator<E> iterator()
    {
        return new DequeIterator(this, false);
    }

    public Iterator<E> reverseIterator()
    {
        return new DequeIterator(this, true);
    }

    private void checkForStackUnderflow()
    {
        if (isEmpty())
        {
            throw new NoSuchElementException("Stack underflow captain!!!");
        }
    }

    private class DequeIterator implements Iterator<E>
    {
        // The instance variables used throughout the program.
        private int _index;
        private Deque<E> _theList;
        private DequeNode<E> _current;
        private boolean _iterateReverse;


        public DequeIterator(Deque<E> deque, boolean reverse)
        {
            _theList = deque;
            _iterateReverse = reverse;
            _index = 0;
            _current = _head;

            /* If the flag for reverse iteration is detected, then just start
            the index at the end and iterate from the tail.
             */
            if (reverse)
            {
                _index = _theList._depth - 1;
                _current = _theList._tail;
            }
        }

        public boolean hasNext()
        {
            boolean results;

            // If we are reverse iterating check if the index is > than -1.
            if (_iterateReverse)
            {
                results = _index > EXCLUSIVE_INDEX_START;
            }
            /* Getting here means that we are forward iterating so check if the
            index is less than size AKA if we have more elements to go through.
             */
            else
            {
                results = _index < _theList._depth;
            }
            return  results;
        }


        public E next()
        {
            // If we don't have more elements to iterate through then we throw.
            if (!hasNext())
            {
                throw new NoSuchElementException();
            }
            // Update the current to descent if we are reverse iterating.
            if (_iterateReverse)
            {
                _current = _current._previous;
                _index--;
            }
            // If we get to here then just update the current value forward.
            else
            {
                _current = _current._next;
                _index++;
            }

            return dequeue();
        }
    }
}
import java.util.Iterator;
导入java.util.NoSuchElementException;
公共类Deque实现了Iterable
{
私有类DequeNode
{
下一步是私人会议;
私有DequeNode_先前;
私人E_数据;
私有DequeNode(E数据)
{
_数据=数据;
}
}
私人董事会主席;
私有DequeNode_tail;
私有内部深度;
私有静态final int EXCLUSIVE_INDEX_START=-1;
公共部门()
{
_head=null;
_tail=null;
_深度=0;
}
公共布尔排队(E元素)//这是尾部插入//TODO:DONE!!
{
DequeNode newNode=新的DequeNode(元素);
if(isEmpty())
{
_头=新节点;
}
其他的
{
_tail.\u next=newNode;
newNode.\u previous=\u tail;
}
_tail=newNode;
_深度++;
返回true;
}
public void排队所有(Iterable元素)
{
对于(每个元素:元素)
{
排队(每队);
}
}
public boolean enqueueHead(E元素)//这是在头部插入//TODO:DONE!!
{
DequeNode newNode=新的DequeNode(元素);
if(isEmpty())
{
_tail=newNode;
}
其他的
{
_head.\u previous=newNode;
newNode.\u next=\u head;
}
_头=新节点;
_深度++;
返回true;
}
公共行政主任()
{
检查底流();
返回头数据;
}
公共E-tail()
{
检查底流();
返回_tail._数据;
}
public E dequeue()//这是从头部删除//TODO:完成!!
{
检查底流();
E已移除=_头。_数据;
if(_head._next==null)
{
_tail=null;
}
其他的
{
_head.\u next.\u previous=null;
}
_头=_头。_下一步;
_深度--;
移除返回;
}
public E dequeueTail()//这是尾部删除//TODO:DONE!!
{
检查底流();
E已删除=_tail._数据;
if(_head._next==null)
{
_head=null;
}
其他的
{
_tail.\u previous.\u next=null;
}
_tail=_tail._previous;
_深度--;
移除返回;
}
公共空间清除()
{
_头=尾=空;
_深度=0;
}
公共整数深度()
{
返回深度;
}
公共布尔值为空()
{
返回_depth==0;
}
公共迭代器迭代器()
{
返回新的DequeIterator(this,false);
}
公共迭代器反转器()
{
返回新的DequeIterator(this,true);
}
私有无效检查下溢()
{
if(isEmpty())
{
抛出新的NoSuchElementException(“堆栈下溢队长!!!”);
}
}
私有类DequeIterator实现迭代器
{
//在整个程序中使用的实例变量。
私有整数指数;
私人文件清单;
私有DequeNode_电流;
私有布尔迭代;
公共DequeIterator(DequeDeque,布尔反向)
{
_列表=deque;
_迭代反向=反向;
_指数=0;
_电流=_头;
/*如果检测到反向迭代的标志,则只需开始
索引位于末尾,并从尾部开始迭代。
*/
如果(反向)
{
_索引=_theList._depth-1;
_当前=\u列表。\u尾部;
}
}
公共布尔hasNext()
{
布尔结果;
//如果我们是反向迭代,请检查索引是否大于-1。
if(_iterateReverse)
{
结果=\u索引>排他\u索引\u开始;
}
/*到达这里意味着我们正在向前迭代,所以请检查
如果我们有更多的元素要通过,索引将小于大小。
*/
其他的
{
结果=指数<列表深度;
}
返回结果;
}
公共教育
{
//如果我们没有更多的元素来迭代,那么我们抛出。
如果(!hasNext())
{
抛出新的NoTouchElementException();
}
//如果我们进行反向迭代,则将当前更新为下降。
if(_iterateReverse)
{
_当前=_当前。_先前;
_索引--;
}
//如果我们到达这里,只需向前更新当前值。
其他的
{
_当前=_当前。_下一步;
_索引++;
}
返回退出队列();
}
}
}

由于代码会在每次迭代时更新索引值,并在每次迭代的出列操作时减少深度值,因此会出现深度小于索引值的用例,从而返回错误的hasNext()值(这似乎是所有失败的根本原因)