end
- redef fun iterator: ListIterator[E] do return new ListIterator[E](_head)
+ redef fun iterator: ListIterator[E] do return new ListIterator[E](self)
# Build an empty list.
init do end
special IndexedIterator[E]
redef fun item do return _node.item
- # redef fun item=(e) do _node.item = e
+ fun item=(e: E) do _node.item = e
redef fun is_ok do return not _node == null
end
# Build a new iterator from `node'.
- private init(node: nullable ListNode[E])
+ private init(list: List[E])
do
- _node = node
+ _list = list
+ _node = list._head
_index = 0
end
+ # The current list
+ var _list: List[E]
+
# The current node of the list
var _node: nullable ListNode[E]
# The index of the current node
redef readable var _index: Int
+
+ # Remove the current item
+ fun delete
+ do
+ _list.remove_node(_node.as(not null))
+ end
+
+ # Insert before the current item
+ fun insert_before(element: E)
+ do
+ _list.insert_before(element, _node.as(not null))
+ end
end
# Linked nodes that constitute a linked list.
--- /dev/null
+1, 2, 3, 4, 5, 6
+11, 12, 13, 14, 15, 16
+1, 21, 2, 22, 3, 23, 4, 24, 5, 25, 6, 26
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var l = new List[Int]
+l.add_all([1, 2, 3, 4, 5, 6])
+print l.join(", ")
+var it = l.iterator
+while it.is_ok do
+ it.item += 10
+ it.next
+end
+print l.join(", ")
+var it = l.iterator
+while it.is_ok do
+ it.insert_before(it.item-10)
+ it.insert_before(it.item+10)
+ it.delete
+ it.next
+end
+print l.join(", ")