📄 dlistiterator.as
字号:
/**
* DATA STRUCTURES FOR GAME PROGRAMMERS
* Copyright (c) 2007 Michael Baczynski, http://www.polygonal.de
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package de.polygonal.ds
{
/**
* A list iterator.
*/
public class DListIterator implements Iterator
{
/**
* The node the iterator is pointing to.
*/
public var node:DListNode;
/**
* The list of the iterator is referenced.
*/
public var list:DLinkedList;
/**
* Initializes a new DListIterator instance pointing to a given node.
* Usually created by invoking SLinkedList.getIterator().
*
* @param list The linked list the iterator should use.
* @param node The iterator's initial node.
*/
public function DListIterator(list:DLinkedList, node:DListNode = null)
{
this.list = list;
this.node = node;
}
/**
* Moves the iterator to the start of the list.
*/
public function start():void
{
node = list.head;
}
/**
* Returns the current node's data while
* moving the iterator forward by one position.
*/
public function next():*
{
if (hasNext())
{
var obj:* = node.data;
node = node.next;
return obj;
}
return null;
}
/**
* Checks if the next node exists.
*/
public function hasNext():Boolean
{
return Boolean(node);
}
/**
* Read/writes the current node's data.
*
* @return The data.
*/
public function get data():*
{
if (node) return node.data;
return null;
}
public function set data(obj:*):void
{
node.data = obj;
}
/**
* Moves the iterator to the end of the list.
*/
public function end():void
{
node = list.tail;
}
/**
* Moves the iterator to the next node.
*/
public function forth():void
{
if (node) node = node.next;
}
/**
* Moves the iterator to the previous node.
*/
public function back():void
{
if (node) node = node.prev;
}
/**
* Checks if the current referenced node is valid.
*
* @return True if the node exists, otherwise false.
*/
public function valid():Boolean
{
return Boolean(node);
}
/**
* Removes the node the iterator is
* pointing to.
*
* @return True if the removal succeeded, otherwise false.
*/
public function remove():Boolean
{
return list.remove(this);
}
/**
* Returns a string representing the current object.
*/
public function toString():String
{
return "{DListIterator, data=" + (node ? node.data : "null") + "}";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -