双链表的迭代器,如何实现next()、remove()?

发布于 2024-12-01 13:21:26 字数 2046 浏览 1 评论 0原文

我遇到了下一个()和删除()问题。 对于 next() 我想返回列表中的下一个元素。 对于remove() 从底层集合中删除迭代器返回的最后一个元素(可选操作)。

我明白我应该做什么,但我在编写代码时遇到困难。有人可以给我一些提示吗?或者向我解释我应该做什么。

这是我的代码,它是一大堆乱七八糟的东西。

    class DoublyLinkedList12Iterator implements Iterator
    {
    private Node cursor;
    private Node lastNodeReturned;
    private Node cursorNext = cursor._next;
    private int nextIndex = 0;
    // private int prevIndex = -1;
    private boolean _hasNextBeenCalled = false;
    private int _currentIndex = -1;


    //Returns true if the iteration has more elements
        public boolean hasNext() {
            return _currentIndex < (_size -1);
        }


    //returns the next element in the iteration
        public Object next() 
        {


        _currentIndex++;
        _hasNextBeenCalled = true;

        /*if(nextIndex == 0)
        {
           nextIndex++;
           return _head._next;
        }*/


        if(cursor != null)
        {
           cursor = cursor._next;
        }
        else
        {
           throw new NoSuchElementException();
        }


        //cursor = cursor._next;
        lastNodeReturned = cursor;
        return cursor._data;




        /*prevIndex--;
        nextIndex++;
        return cursor;

        this._prev = this._next;
        if(this._next != null);
        return the first node  

                Node cursor = _head;
                    for(int i = _currentIndex; i < _size ; i++)
                    {
                    cursor = cursor._next;
                    }
                    return cursor._data;
        */
            }


        public void remove() 
        {


        if(!_hasNextBeenCalled)
        {
           throw new IllegalStateException();
        }

        _hasNextBeenCalled = false;

        if(cursor == lastNodeReturned)
        {
           cursor = cursor._next;
        }
        else
        {
           nextIndex--;
        }

        lastNodeReturned._prev = lastNodeReturned._next;

        _size--;



            }

    }

The next() and remove() i'm having trouble with.
For the next() i want to return the next element in the list.
For the remove() Removes from the underlying collection the last element returned by the iterator (optional operation).

I understand what i'm suppose to do, but i'm having trouble writing the code. Could someone give me some hints? or explain to me what i'm suppose to do.

Here's my code, its a big piece of mess.

    class DoublyLinkedList12Iterator implements Iterator
    {
    private Node cursor;
    private Node lastNodeReturned;
    private Node cursorNext = cursor._next;
    private int nextIndex = 0;
    // private int prevIndex = -1;
    private boolean _hasNextBeenCalled = false;
    private int _currentIndex = -1;


    //Returns true if the iteration has more elements
        public boolean hasNext() {
            return _currentIndex < (_size -1);
        }


    //returns the next element in the iteration
        public Object next() 
        {


        _currentIndex++;
        _hasNextBeenCalled = true;

        /*if(nextIndex == 0)
        {
           nextIndex++;
           return _head._next;
        }*/


        if(cursor != null)
        {
           cursor = cursor._next;
        }
        else
        {
           throw new NoSuchElementException();
        }


        //cursor = cursor._next;
        lastNodeReturned = cursor;
        return cursor._data;




        /*prevIndex--;
        nextIndex++;
        return cursor;

        this._prev = this._next;
        if(this._next != null);
        return the first node  

                Node cursor = _head;
                    for(int i = _currentIndex; i < _size ; i++)
                    {
                    cursor = cursor._next;
                    }
                    return cursor._data;
        */
            }


        public void remove() 
        {


        if(!_hasNextBeenCalled)
        {
           throw new IllegalStateException();
        }

        _hasNextBeenCalled = false;

        if(cursor == lastNodeReturned)
        {
           cursor = cursor._next;
        }
        else
        {
           nextIndex--;
        }

        lastNodeReturned._prev = lastNodeReturned._next;

        _size--;



            }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-12-08 13:21:26
public T next() {
if (nextnode == null)
    throw new NoSuchElementException();
currentnode = nextnode;
previousnode = currentnode.previous;
nextnode = currentnode.next;
return currentnode.element;
}

public void remove() {
if (previousnode != null)
    previousnode.next = nextnode;
if (nextnode != null)
    nextnode.previous = previousnode;
}
public T next() {
if (nextnode == null)
    throw new NoSuchElementException();
currentnode = nextnode;
previousnode = currentnode.previous;
nextnode = currentnode.next;
return currentnode.element;
}

public void remove() {
if (previousnode != null)
    previousnode.next = nextnode;
if (nextnode != null)
    nextnode.previous = previousnode;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文