//variable node for the dynamic list - could be in a separate file class VariableNode { char name; int value; VariableNode next; public VariableNode (char varname, int value) { this.name = varname; this.value = value; next = null; } } //constructor create a new list - i.e. empty linked list public class VList { VariableNode head; public VList () { head = null; } //insert function public void insert (char name, int val) { //first go through list to //see if node is present VariableNode iterator = head; boolean found = false; //assume that it is not found //loop though list while(iterator != null) { if(iterator.name == name) { iterator.value = val; //if you find it - reset value found = true; //and set a flag to say that it was found break; } iterator = iterator.next; //advance through the list } //If it was not in the list - make a new node if(!found) { VariableNode temp = new VariableNode(name, val); temp.next = head; head = temp; } } //extra method not required, but nice - just checks to see //if the character was already there public boolean isPresent (char name) { VariableNode iterator = head; while (iterator != null) { if (iterator.name == name) break; iterator = iterator.next; } return (iterator == null); } //method to set new value public void setValue (char name, int val) { VariableNode iterator = head; while (iterator != null) { if (iterator.name == name) break; iterator = iterator.next; } iterator.value = val; } //get value - lookup method to retrive value of a variable public int getValue (char name) { VariableNode iterator = head; //default return in case it is not there //this sets all variables not in list to 0 int ret = 0; //loop though to find value //when you find it set ret to the value stored //if it is not found ret stays the same while (iterator != null) { if (iterator.name == name) ret = iterator.value; iterator = iterator.next; } return ret; } //debug function that just prints the list public void print () { VariableNode iterator = head; while (iterator != null) { System.out.println ("variable " + iterator.name + " has value=" + iterator.value); iterator = iterator.next; } } }