The interIterator.java Program

The interIterator.java Program

The interIterator.java program includes an interactive interface that permits the user to control the iterator directly. After you’ve started the program, you can perform the following actions by typing the appropriate letter:

• s—Show the list contents • r—Reset the iterator to the start of the list • n—Go to the next link • g—Get the contents of the current link • b—Insert before the current link • a—Insert a new link after the current link • d—Delete the current link

Listing 5.9 shows the complete interIterator.java program. LISTING 5.9 The interIterator.java Program

// interIterator.java // demonstrates iterators on a linked listListIterator // to run this program: C>java InterIterApp import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class Link

{ public long dData; // data item

236 CHAPTER 5 Linked Lists

LISTING 5.9 Continued public Link next; // next link in list

// -------------------------------------------------------------

public Link(long dd) // constructor

{ dData = dd; } // -------------------------------------------------------------

public void displayLink() // display ourself

{ System.out.print(dData + “ “); } } // end class Link //////////////////////////////////////////////////////////////// class LinkList

{ private Link first; // ref to first item on list

// -------------------------------------------------------------

public LinkList() // constructor

{ first = null; } // no items on list yet // ------------------------------------------------------------- public Link getFirst() // get value of first { return first; } // ------------------------------------------------------------- public void setFirst(Link f) // set first to new link { first = f; } // ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return first==null; } // ------------------------------------------------------------- public ListIterator getIterator() // return iterator { return new ListIterator(this); // initialized with } // this list

// ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning of list while(current != null) // until end of list,

{ current.displayLink(); // print data current = current.next; // move to next link }

System.out.println(“”); }

Iterators 237

LISTING 5.9 Continued // -------------------------------------------------------------

} // end class LinkList //////////////////////////////////////////////////////////////// class ListIterator

{ private Link current; // current link private Link previous; // previous link private LinkList ourList; // our linked list

//-------------------------------------------------------------- public ListIterator(LinkList list) // constructor { ourList = list; reset(); }

//-------------------------------------------------------------- public void reset() // start at ‘first’ { current = ourList.getFirst(); previous = null; }

//-------------------------------------------------------------- public boolean atEnd() // true if last link { return (current.next==null); } //-------------------------------------------------------------- public void nextLink() // go to next link { previous = current; current = current.next; }

//-------------------------------------------------------------- public Link getCurrent() // get current link { return current; } //-------------------------------------------------------------- public void insertAfter(long dd) // insert after { // current link Link newLink = new Link(dd);

if( ourList.isEmpty() ) // empty list { ourList.setFirst(newLink); current = newLink;

238 CHAPTER 5 Linked Lists

LISTING 5.9 Continued }

else // not empty

{ newLink.next = current.next; current.next = newLink; nextLink(); // point to new link }

} //-------------------------------------------------------------- public void insertBefore(long dd) // insert before { // current link Link newLink = new Link(dd);

if(previous == null) // beginning of list { // (or empty list) newLink.next = ourList.getFirst(); ourList.setFirst(newLink); reset(); }

else // not beginning { newLink.next = previous.next; previous.next = newLink; current = newLink; }

} //-------------------------------------------------------------- public long deleteCurrent() // delete item at current { long value = current.dData; if(previous == null) // beginning of list

{ ourList.setFirst(current.next); reset(); }

else // not beginning { previous.next = current.next; if( atEnd() )

reset(); else

Iterators 239

LISTING 5.9 Continued current = current.next;

} return value; }

//-------------------------------------------------------------- } // end class ListIterator //////////////////////////////////////////////////////////////// class InterIterApp

{ public static void main(String[] args) throws IOException

{ LinkList theList = new LinkList(); // new list ListIterator iter1 = theList.getIterator(); // new iter long value;

iter1.insertAfter(20); // insert items iter1.insertAfter(40); iter1.insertAfter(80); iter1.insertBefore(60);

while(true) { System.out.print(“Enter first letter of show, reset, “); System.out.print(“next, get, before, after, delete: “); System.out.flush(); int choice = getChar(); // get user’s option switch(choice)

{ case ‘s’: // show list

if( !theList.isEmpty() ) theList.displayList(); else System.out.println(“List is empty”); break; case ‘r’: // reset (to first) iter1.reset(); break;

case ‘n’: // advance to next item if( !theList.isEmpty() && !iter1.atEnd() ) iter1.nextLink(); else

240 CHAPTER 5 Linked Lists

LISTING 5.9 Continued System.out.println(“Can’t go to next link”);

break; case ‘g’: // get current item

if( !theList.isEmpty() ) { value = iter1.getCurrent().dData; System.out.println(“Returned “ + value); }

else

System.out.println(“List is empty”); break; case ‘b’: // insert before current System.out.print(“Enter value to insert: “); System.out.flush(); value = getInt(); iter1.insertBefore(value); break;

case ‘a’: // insert after current System.out.print(“Enter value to insert: “); System.out.flush(); value = getInt(); iter1.insertAfter(value); break;

case ‘d’: // delete current item

if( !theList.isEmpty() ) { value = iter1.deleteCurrent(); System.out.println(“Deleted “ + value); }

else

System.out.println(“Can’t delete”); break; default:

System.out.println(“Invalid entry”); } // end switch } // end while } // end main() //-------------------------------------------------------------- public static String getString() throws IOException

{ InputStreamReader isr = new InputStreamReader(System.in);

Iterators 241

LISTING 5.9 Continued BufferedReader br = new BufferedReader(isr);

String s = br.readLine(); return s; }

//------------------------------------------------------------- public static char getChar() throws IOException { String s = getString(); return s.charAt(0); }

//------------------------------------------------------------- public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); }

//------------------------------------------------------------- } // end class InterIterApp ////////////////////////////////////////////////////////////////

The main() routine inserts four items into the list, using an iterator and its insertAfter() method. Then it waits for the user to interact with it. In the following sample interaction, the user displays the list, resets the iterator to the beginning, goes forward two links, gets the current link’s key value (which is 60), inserts 100 before this, inserts 7 after the 100, and displays the list again:

Enter first letter of show, reset, next, get, before, after, delete: s 20 40 60 80

Enter first letter of show, reset, next, get, before, after, delete: r Enter first letter of show, reset, next, get, before, after, delete: n Enter first letter of show, reset, next, get, before, after, delete: n Enter first letter of show, reset, next, get, before, after, delete: g Returned 60 Enter first letter of

show, reset, next, get, before, after, delete: b Enter value to insert: 100

242 CHAPTER 5 Linked Lists

Enter first letter of show, reset, next, get, before, after, delete: a Enter value to insert: 7 Enter first letter of

show, reset, next, get, before, after, delete: s 20 40 100 7 60 80

Experimenting with the interIterator.java program will give you a feeling for how the iterator moves along the links and how it can insert and delete links anywhere in the list.