// Supplementary to "Software Design ...", John A Robinson, Newnes, 2004, // // // Java Program to implement ADT table of pairs // using unordered array // // import java.io.*; import java.util.*; // util is for StringTokenizer class ipair { public int key; public int other_stuff; }; class table_of_pairs { int table_size; ipair ptable[]; int num_in_table; public table_of_pairs(int size) { num_in_table = 0; table_size = size; // Set up an array of the requested size ptable = new ipair[table_size+1]; // Now, being Java, we have to go through and construct the entries for (int i = 0; i < table_size+1; i++) ptable[i] = new ipair(); }; public int add_to_table(int key, int other) { remove_from_table(key); // Remove any existing entry with this key value if (num_in_table == table_size) return(-1); // table is full ptable[num_in_table].key = key; // Add new item at the end of the list ptable[num_in_table++].other_stuff = other; return(0); } public int remove_from_table(int member) { int offset = 0; ptable[num_in_table].key = member; // sentinel while (ptable[offset].key != member) offset++;// Keep going until find something with this key value if (offset == num_in_table) // The one I found was the sentinel - return(-1); // - so member is not in table // If member is in table, copy down final element in its place (to overwrite it) ptable[offset] = ptable[num_in_table - 1]; num_in_table--; return(0); } public int get_other_stuff(int member, int other[]) { // Passing data back in other[0] int offset = 0; ptable[num_in_table].key = member; // sentinel ptable[num_in_table].other_stuff = 0; // set up, so that if key is not in table, this zero // value of other_stuff will get put into other while(ptable[offset].key != member) offset++; other[0] = ptable[offset].other_stuff; if(offset != num_in_table) // returns 0 if the one found was the sentinel return 1; return 0; } public int number_in() { return(num_in_table); }; public void print_table() { int offset; for (offset = 0; offset < num_in_table; offset++) System.out.print(ptable[offset].key + "," + ptable[offset].other_stuff + " "); System.out.println(); } }; // // The main program here is just a simple test loop // The commands q, a, r, c, p are available, to quit, add element, // remove element, check for element, and print table repectively. // public class table_unorderedarray { public static void main(String args[]) throws java.io.IOException { char c; int key; int other[] = new int[1]; // Other is a one element array to allow pass-by-reference table_of_pairs mytable = new table_of_pairs(8); // Here saying that 8 is max num of elements I will use in // any run of this test program (A bit restrictive). BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); // Using BufferedReader to allow readLine into a String while(true) { System.out.print(": "); // Prompt String temp = cin.readLine(); // Split input string into tokens StringTokenizer tokens = new StringTokenizer(temp); if (!tokens.hasMoreTokens()) continue; temp = tokens.nextToken(); switch(temp.charAt(0)) { case 'q': return; case 'a': key = Integer.parseInt(tokens.nextToken()); other[0] = Integer.parseInt(tokens.nextToken()); mytable.add_to_table(key, other[0]); break; case 'r': key = Integer.parseInt(tokens.nextToken()); mytable.remove_from_table(key); break; case 'c': key = Integer.parseInt(tokens.nextToken()); if (mytable.get_other_stuff(key, other) != 0) System.out.println("Item labelled " + key + " has value " + other[0] + " in mytable"); else System.out.println("There is no item labelled " + key + " in mytable"); break; case 'p': mytable.print_table(); break; default: System.out.println("Use q, a, r, c or p only"); break; } System.out.println(mytable.number_in() + " items in table"); } } }