双链表的迭代器,如何实现next()、remove()?
我遇到了下一个()和删除()问题。 对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)