Source of TestIntegerNodeSequence.java


  1: //TestIntegerNodeSequence.java
  2: //Illustrates recursion applied to a sequence of linked nodes

  4: import java.util.Scanner;

  6: public class TestIntegerNodeSequence
  7: {
  8:     private static Scanner keyboard = new Scanner(System.in);
  9:     private static Scanner lineScanner = null;
 10:     private static Node head = null;

 12:     public static void main(String[] args)
 13:     {
 14:         System.out.println
 15:         (
 16:             "\nEnter four data values for the nodes "
 17:             + "on the following line:"
 18:         );
 19:         String line = keyboard.nextLine();
 20:         lineScanner = new Scanner(line);

 22:         buildSequenceExplicitly();
 23:         //buildSequenceIteratively();
 24:         //buildSequenceRecursively();

 26:         System.out.println
 27:         (
 28:             "\nHere, from the linked nodes, are the "
 29:             + "values read in, first in the order\nin which they "
 30:             + "appear in the sequence, then in that order reversed:"
 31:         );
 32:         displaySequenceValues(head);
 33:         System.out.println();
 34:         displaySequenceValuesInReverseOrder(head);

 36:         System.out.print("\nPress Enter to continue ... ");
 37:         keyboard.nextLine();
 38:     }

 40:     public static void buildSequenceExplicitly()
 41:     {
 42:         head = null;
 43:         if (lineScanner.hasNextInt())
 44:             head = new Node(lineScanner.nextInt(), head);
 45:         if (lineScanner.hasNextInt())
 46:             head = new Node(lineScanner.nextInt(), head);
 47:         if (lineScanner.hasNextInt())
 48:             head = new Node(lineScanner.nextInt(), head);
 49:         if (lineScanner.hasNextInt())
 50:             head = new Node(lineScanner.nextInt(), head);
 51:     }

 53:     public static void buildSequenceIteratively()
 54:     {
 55:         head = null;
 56:         while (lineScanner.hasNextInt())
 57:             head = new Node(lineScanner.nextInt(), head);
 58:     }

 60:     public static void buildSequenceRecursively()
 61:     {
 62:         if (lineScanner.hasNextInt())
 63:         {
 64:             int value = lineScanner.nextInt();
 65:             buildSequenceRecursively();
 66:             head = new Node(value, head);
 67:         }
 68:         else
 69:         {
 70:             head = null;
 71:         }
 72:     }

 74:     public static void displaySequenceValues(Node head)
 75:     {
 76:         if (head != null)
 77:         {
 78:             System.out.print(head.data + " ");
 79:             displaySequenceValues(head.next);
 80:         }
 81:     }

 83:     public static void displaySequenceValuesInReverseOrder(Node head)
 84:     {
 85:         if (head != null)
 86:         {
 87:             displaySequenceValuesInReverseOrder(head.next);
 88:             System.out.print(head.data + " ");
 89:         }
 90:     }

 92:     private static class Node
 93:     {
 94:         private int  data; //Data in the node
 95:         private Node next; //Link to next node

 97:         //Two constructors
 98:         public Node(int data)
 99:         {
100:             this(data, null);
101:         }
102:         public Node(int data, Node next)
103:         {
104:             this.data = data;
105:             this.next = next;
106:         }
107:     }
108: }