//default value of int value is unassigned if varName is only declared and not assigned public void add(string varName,int value = unassigned) { Variable variable = new Variable(varName, value); int index = hash(variable); LinkedList<Variable> root = chainlist[index]; LinkedListNode<Variable> current = root.First; //if the chainlist's current index has no value stored inside. if (current == null) { root.AddFirst(variable); } else { chainlist[index].AddLast(variable); } }
//needed for comparison public bool Equal(Variable other) { return this.varName.Equals(other.varName) && this.value == other.value; }
//HASH FUNCTIONS //the variable name and its associated value is stored together in the same location //thanks to the Variable class. //for the add function. public int hash(Variable variable) { //hold the calculations necessary to find the hash key of the value. int t = 0; //getting the ascii values of each character in the string in bytes string value = variable.varName; byte[] asciibytes = ASCIIEncoding.ASCII.GetBytes(value.ToString()); //using the formula given in the assignment //e.g. ABC = ((65 * 1) + (66 * 2) + (67 * 3))%TABLESIZE for (int i = 0; i < asciibytes.Length;i++ ) t += (int)asciibytes[i] * (i + 1); t = t % size; // Console.WriteLine("Variable is stored at location {0}", t); return t; }