class State // Linked list of the current values of variables. { String name; // Variable's name. int value; // Its value. State next; // Next entry in list. State() { name = ""; } // First entry is a dummy. State(String n, int v, State s) { name = n; value = v; next = s; } // Do a lookup of variable named n, returning its value. int valueOf(String n) { for (State S = this; S != null; S = S.next) if (n.compareTo(S.name) == 0) return S.value; return 0; // Probably not a good idea. } // Change variable v to have value n, or add a new entry if necessary. State update(String n, int v) { for (State S = this; S != null; S = S.next) if (n.compareTo(S.name) == 0) { S.value = v; // New value for existing variable. return this; } return new State(n, v, this); // Add an entry to this state. } public String toString() { String result = ""; for (State S = this; S != null; S = S.next) result += S.name + " = " + S.value + "\n"; return result; } }