Source of StringLinkedListWithIteratorDemo.java


  1: //StringLinkedListWithIteratorDemo.java

  3: //Public methods in the StringLinkedListWithIterator class:
  4: //addANodeToStart()
  5: //resetIteration()
  6: //moreToIterate()
  7: //goToNext()
  8: //getDataAtCurrent()
  9: //setDataAtCurrent()
 10: //insertNodeAfterCurrent()
 11: //deleteCurrentNode()
 12: //showList()
 13: //length()
 14: //onList()
 15: //toArray()

 17: import java.util.Scanner;

 19: public class StringLinkedListWithIteratorDemo
 20: {
 21:     public static void main(String[] args)
 22:     {
 23:         StringLinkedListWithIterator list = new StringLinkedListWithIterator();
 24:         list.addANodeToStart("One");
 25:         list.addANodeToStart("Two");
 26:         list.addANodeToStart("Three");
 27:         list.addANodeToStart("Four");
 28:         list.addANodeToStart("Five");
 29:         System.out.println("List has " + list.length() + " entries.");
 30:         System.out.println("Showing current list contents:");
 31:         list.showList();
 32:         pause();

 34:         System.out.println();
 35:         if (list.onList("Three"))
 36:             System.out.println("Three is on list.");
 37:         else
 38:             System.out.println("Three is NOT on list.");

 40:         try
 41:         {
 42:             list.resetIteration();
 43:             System.out.println("Current node data: " +
 44:                 list.getDataAtCurrent());
 45:                 
 46:             list.goToNext();
 47:             list.goToNext();
 48:             System.out.println("Current node data: " +
 49:                 list.getDataAtCurrent());
 50:             list.deleteCurrentNode();
 51:             if (list.onList("Three"))
 52:                 System.out.println("Three is on list.");
 53:             else
 54:                 System.out.println("Three is NOT on list.");
 55:             System.out.println("Current node data: " +
 56:                 list.getDataAtCurrent());
 57:             System.out.println("Showing current list contents:");
 58:             list.showList();
 59:             pause();

 61:             list.resetIteration();
 62:             while (list.moreToIterate())
 63:             {
 64:                 System.out.println(list.getDataAtCurrent());
 65:                 list.goToNext();
 66:             }
 67:             System.out.println(list.getDataAtCurrent());
 68:         }
 69:         catch (LinkedListException e)
 70:         {
 71:             System.out.println(e.getMessage());
 72:         }
 73:         System.out.println("Carrying on ... ");
 74:         pause();
 75:         list.resetIteration();
 76:         list.goToNext();
 77:         list.insertNodeAfterCurrent("ten");
 78:         list.showList();
 79:         pause();
 80:         }

 82:     private static void pause()
 83:     {
 84:         Scanner keyboard = new Scanner(System.in);
 85:         System.out.print("Press Enter to continue ... ");
 86:         keyboard.nextLine();
 87:     }
 88: }