USING AN ITERATOR CLASS in C#

One problem the LinkedList class has is that you can’t refer to two positions in the linked list at the same time. We can refer to any one position in the list (the current node, the previous node, etc.), but if we want to specify two or more positions, such as if we want to remove a range of nodes from the list, we’ll need some other mechanism. This mechanism is an iterator class.
The iterator class consists of three data fields: a field that stores the linked list, a field that stores the current node, and a field that stores the current node. The constructor method is passed a linked list object, and the method sets the current field to the header node of the list passed into the method. Let’s look at our definition of the class so far:

public class ListIter
{
private Node current;
private Node previous;
LinkedList theList;
public ListIter(LinkedList list)
{
theList = list;
current = theList.getFirst();
previous = null;
}

The first thing we want an Iterator class to do is allow us to move from node to node through the list. The method nextLink does this:

public void NextLink()
{
previous = current;
current = current.link;
}

Notice that in addition to establishing a new current position, the previous node is also set to the node that is current before the method has finished executing. Keeping track of the previous node in addition to the current node makes insertion and removal easier to perform.
The getCurrent method returns the node pointed to by the iterator:
public Node GetCurrent()
{
return current;
}

Two insertion methods are built in the Iterator class: InsertBefore and InsertAfter. InsertBefore inserts a new node before the current node; InsertAfter inserts a new node after the current node. Let’s look at the Insert-Before method first.

The first thing we have to do when inserting a new node before the current object is check to see if we are at the beginning of the list. If we are, then we can’t insert a node before the header node, so we throw an exception. This exception is defined below. Otherwise, we set the new node’s Link field to the Link field of the previous node, set the previous node’s Link field to the new node, and reset the current position to the new node. Here’s the code:

public void InsertBefore(Object theElement)
{
Node newNode = new Node(theElement);
if (current == header)
throw new InsertBeforeHeaderException();
else
{
newNode.Link = previous.Link;
previous.Link = newNode;
current = newNode;
}
}

The InsertBeforeHeader Exception class definition is:

public class InsertBeforeHeaderException
{
public InsertBeforeHeaderException()
{
base("Can't insert before the header node.");
}
}

The InsertAfter method in the Iterator class is much simpler than the method we wrote in the LinkedList class. Since we already know the position of the current node, the method just needs to set the proper links and set the current node to the next node.

public void InsertAfter(Object theElement)
{
Node newnode = new Node(theElement);
newNode.Link = current.Link;
current.Link = newnode;
NextLink();
}

Removing a node from a linked list is extremely easy using an Iterator class. The method simply sets the Link field of the previous node to the node pointed to by the current node’s Link field:

public void Remove()
{
prevous.Link = current.Link;
}

Other methods we need in an Iterator class include methods to reset the iterator to the header node (and the previous node to null) and a method to test if we’re at the end of the list. These methods are shown as follows.

public void Reset()
current = theList.getFirst();
previous = null;
}
public bool AtEnd()
{
return (current.Link == null);
}

0 Responses to "USING AN ITERATOR CLASS in C#"