Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 Old CS 160 Iterators; Doubly-linked lists
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Iterators; Doubly-linked lists

by admin last modified 2005-05-25 15:41

Iterators; Doubly-linked lists

An iterator is an object which can be used to step through a collection, returning the elements of the collection in succession.  java.util defines the following Iterator interface:

interface Iterator {

    boolean hasNext(); // returns true if the iterator has more items and false otherwise

    Object next(); // if there is a next item in the sequence, returns that item and advances to the next; otherwise throws an exception

    void remove(); // removes from the collection the last item returned by the iterator

};

In addition, the Collection interface defines a method "iterator", which returns an iterator for the Collection.

example  Use an iterator to find the sum of Integer objects in a given List:

int sum(List list){
    Iterator i = list.iterator();

    int sum = 0;
    while(i.hasNext()){
        sum += ((Integer) i.next()).intValue();
        } 
    return sum;
}

example  Use an iterator to write a toString method for a List class:

String toString(){
    Iterator i = iterator();

    String result = "";  // start with a null string
    while(i.hasNext())
        result += i.next().toString();
       
    return result;
}


example  Write the "iterator" method for LinkedList.  (Note the use of an anonymous class.)

Iterator iterator(){
    return new Iterator(){
        ListNode current = head;
        boolean hasNext()
            { return current!=null; }
        Object next(){
            Object o = current.datum;
            current = current.next;
            return o;
            }
        };
}


example  Iterator for CircularLinkedList (without remove):

public Iterator iterator()
{
  return new Iterator(){
    ListNode current = tail;
    boolean tailVisited = (tail==null);

    public boolean hasNext(){
      return !tailVisited;
    }
    public Object next(){
      current = current.next;
      tailVisited = (current==tail);
      return current.datum;
    }
    public void remove(){
    }
  };
}


example  Iterator for CircularLinkedList (including remove):

public Iterator iterator()
{
  return new Iterator(){
    ListNode secondPrior = null;
    ListNode prior = tail;
    ListNode current = tail==null?null:tail.next;
    boolean tailVisited = (tail==null);

    public boolean hasNext(){
      return !tailVisited;
    }
    public Object next(){
      tailVisited = (current==tail);
      Object o = current.datum;
      if(prior!=null)
        secondPrior = prior;
      prior = current;
      current = current.next;
      return o;
    }
    public void remove(){
      if(prior==tail)
        if(prior==current)
          tail=null;
        else
          tail=secondPrior;
      secondPrior.next = current;
      prior = null;
      --nitems;
    }
  };
}


Implementation of a circular, doubly-linked list:


public class DoublyLinkedList implements SimpleList
{
    int nitems;

    ListNode sentinel;

    public class ListNode {
        Object datum;
        ListNode prior,next;

        ListNode(Object datum){
            this.datum = datum;
            prior = next = null;
        }

        ListNode(Object datum, ListNode prior, ListNode next){
            this.datum = datum;
            this.prior = prior;
            this.next = next;
        }

        public Object getDatum(){
            return datum;
        }

        public ListNode getNext(){
            return next;
        }

        public ListNode getPrior(){
         return prior;
      }
    }


DoublyLinkedList()
{
    nitems = 0;
    sentinel = new ListNode(null);
    sentinel.next = sentinel.prior = sentinel;
}

private ListNode getnth(int index)
{
    ListNode target = sentinel.next;

    if(index<0 || index>=nitems)
        throw new IndexOutOfBoundsException();
    for(int k=0; k<index; k++)
        target = target.next;
    return target;
}

public void clear()
{
    nitems = 0;
    sentinel.next = sentinel.prior = sentinel;
}

public int size()
{
    return nitems;
}

public boolean isEmpty()
{
    return nitems==0;
}

public boolean add(Object item)
{
    ListNode newNode = new ListNode(item,sentinel.prior,sentinel);
    sentinel.prior = newNode;
    newNode.prior.next = newNode;
    ++nitems;
    return true;
}

public Object get(int index)
{
    return getnth(index).datum;
}

public Object set(int index, Object obj)
{
    ListNode node = getnth(index);
    Object temp = node.datum;
    node.datum = obj;
    return temp;
}

public Object remove(int index)
{
    Object target;

    if(index<0 || index>=nitems)
        throw new IndexOutOfBoundsException();
    else {
        ListNode node = getnth(index);
        target = node.datum;
        node.prior.next = node.next;
        node.next.prior = node.prior;
        }
    --nitems;
    return target;
}

public void add(int index, Object item)
{
    if(index<0 || index>nitems)
        throw new IndexOutOfBoundsException();
    else {
        ListNode node = getnth(index);
        ListNode newNode = new ListNode(item,node.prior,node);
        node.prior = newNode;
        newNode.prior.next = newNode;
        ++nitems;
        }
}

public Iterator iterator()
{
  return new Iterator(){
    ListNode current = sentinel.next;

    public boolean hasNext(){
      return current!=sentinel;
    }
    public Object next(){
      Object o = current.datum;
      current = current.next;
      return o;
    }
    public void remove(){
      ListNode prior = current.prior;
      prior.prior.next = current;
      current.prior = prior.prior;
      --nitems;
    }
  };
}

}

 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: