/// <summary> /// Moves the iterator to the previous element in the list. /// </summary> /// <returns>the previous element in the list.</returns> public T previous() { modCheck(); my_index--; my_parent.rangeCheck(my_index); //check for double removal if (my_removed) { my_removed = false; } //get the next value normally and return it my_current = my_current.prev; return(my_current.value); }
/// <summary> /// Removes an element from the list that was last returned by /// a call to previous() or next(). /// </summary> /// <returns>the removed element.</returns> public T remove() { modCheck(); //check for double removal if (my_removed) { throw new IllegalStateException("You cannot remove an item twice before advancing the iterator."); } my_removed = true; T ret_value = my_current.value; my_current = my_parent.removeNode(my_current); my_index--; return(ret_value); }
/// <summary> /// Moves the iterator to the next element after the current /// iterator position. /// </summary> /// <returns>the next element in the list.</returns> public T next() { modCheck(); my_index++; my_parent.rangeCheck(my_index); //check for double removal if (my_removed) { my_removed = false; } //check whether we are on the first element, in this case just return the value if (my_index == 0) { return(my_current.value); } else //advance normally here { my_current = my_current.next; return(my_current.value); } }
/// <summary> /// Sets up the node with an element, a link to the next and previous nodes. /// </summary> /// <param name="the_value">the element value.</param> /// <param name="the_next">the next node link.</param> /// <param name="the_prev">the previous node link.</param> public LL_Node(T the_value, LL_Node <T> the_next, LL_Node <T> the_prev) { my_value = the_value; my_next = the_next; my_prev = the_prev; }
private bool my_removed = false; //flag to avoid double removals /// <summary> /// Sets up the iterator with a link to the parent list. /// </summary> /// <param name="the_parent"></param> public LL_ListIterator(LinkedList <T> the_parent) { my_current = the_parent.headNode(); my_parent = the_parent; my_mod_count = my_parent.getModCount(); }