CSCI A202 - Introduction to Programming (II)

Lecture 23: One Way Lists

Code and sample interactive session.


tucotuco.cs.indiana.edu% ls
One.java Three.java Two.java
tucotuco.cs.indiana.edu% cat One.java
import java.io.*; import java.util.*; class One { public static void main(String[] args) { try { BufferedReader b = new BufferedReader ( new InputStreamReader ( System.in)); String cmd; OneWayList list = null; while (true) { System.out.print("input> "); StringTokenizer s = new StringTokenizer(b.readLine()); if (s.hasMoreTokens()) { cmd = s.nextToken(); } else { continue; } if (cmd.equals("new")) { list = new OneWayList(); System.out.println(list); } else if (cmd.equals("isEmpty")) { System.out.println(list.isEmpty()); System.out.println(list); } else if (cmd.equals("atEnd")) { System.out.println(list.atEnd()); System.out.println(list); } else if (cmd.equals("size")) { System.out.println(list.size()); System.out.println(list); } else if (cmd.equals("first")) { list.first(); System.out.println(list); } else if (cmd.equals("next")) { list.next(); System.out.println(list); } else if (cmd.equals("access")) { list.access(); System.out.println(list); } else if (cmd.equals("replace")) { String elem = s.nextToken(); list.replace(elem); System.out.println(list); } else if (cmd.equals("insert")) { String elem = s.nextToken(); list.insert(elem); System.out.println(list); } else if (cmd.equals("remove")) { System.out.println(list.remove()); System.out.println(list); } else if (cmd.equals("quit")) { System.out.println("Good-bye!"); System.exit(0); } else { System.out.println("Unknown command: " + cmd); System.out.println(list); } } } catch (Exception e) { System.out.println("E: " + e.toString()); } } } class OneWayList { List current; List previous; List first; int size; boolean isEmpty() { return first == null; } boolean atEnd() { return current == null; } int size() { return size; } void first() { current = first; previous = null; } void next() { previous = current; current = current.cdr; } Object access() { return current.car; } void replace(Object obj) { current.car = obj; } void insert(Object obj) { size += 1; if (previous == null && current == null) { current = new List(obj, null); first = current; } else if (current == null) { previous.cdr = new List(obj, null); current = previous.cdr; } else if (previous == null) { current = new List(obj, current); first = current; } else { previous.cdr = new List(obj, current); // needed: current = previous.cdr; } } Object remove() { Object value = null; if (previous != null && current != null) { previous.cdr = current.cdr; value = current.car; current = current.cdr; size -= 1; } else if (previous == null) { first = current.cdr; value = current.car; current = current.cdr; size -= 1; } return value; } public String toString() { if (first == null) { return "null"; } else { return first.show(current); } } } class List { Object car; // call this first (or value ) List cdr; // call this second (or nextElement) List(Object car, List cdr) { this.car = car; this.cdr = cdr; } public String show(List current) { if (cdr == null) { if (this == current) { return "[" + car + "]"; } else { return (String)car; } } else { if (this == current) { return "[" + car + "] " + cdr.show(null); } else { return car + " " + cdr.show(current); } } } }
tucotuco.cs.indiana.edu%
And a sample interactive session:
tucotuco.cs.indiana.edu% javac One.java

tucotuco.cs.indiana.edu% java One
input> new null input> insert one [one] input> insert two [two] one input> next two [one] input> insert three two three [one] input> size 3 two three [one] input> first [two] three one input> next two [three] one input> replace six two [six] one input> next two six [one] input> remove one two six input> atEnd true two six input> first [two] six input> size 2 [two] six input> quit Good-bye!
tucotuco.cs.indiana.edu%
This will be the supporting code for most of the lecture.