Question
Code: package homework; import algs13.Queue; import stdlib.StdIn; import stdlib.StdOut; /** * Complete the 5 methods marked ToDo * You must not change the declaration of
Code:
package homework;
import algs13.Queue; import stdlib.StdIn; import stdlib.StdOut;
/** * Complete the 5 methods marked ToDo * You must not change the declaration of any method. */
/** * The LinkedListST class represents an (unordered) symbol table of * generic key-value pairs. It supports put, get, and delete methods (already implemented) */ public class LinkedListST
// a helper linked list data type private class Node { private Key key; private Value val; private Node next;
public Node(Key key, Value val, Node next) { this.key = key; this.val = val; this.next = next; } }
/** * Initializes an empty symbol table. */ public LinkedListST() { first = null; }
/** * Returns the value associated with the given key in this symbol table. */ public Value get(Key key) { if (key == null) throw new NullPointerException("argument to get() is null"); for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) return x.val; } return null; }
/** * Inserts the specified key-value pair into the symbol table, overwriting the old * value with the new value if the symbol table already contains the specified key. * Deletes the specified key (and its associated value) from this symbol table * if the specified value is null. */ public void put(Key key, Value val) { if (key == null) throw new NullPointerException("first argument to put() is null"); if (val == null) { delete(key); return; }
for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); }
/** * Removes the specified key and its associated value from this symbol table * (if the key is in this symbol table). */ public void delete(Key key) { if (key == null) throw new NullPointerException("argument to delete() is null"); first = delete(first, key); }
// delete key in linked list beginning at Node x // warning: function call stack too large if table is large private Node delete(Node x, Key key) { if (x == null) return null; if (key.equals(x.key)) { return x.next; } x.next = delete(x.next, key); return x; }
/** * size returns the number of key-value pairs in the symbol table. * it returns 0 if the symbol table is empty. */ public int size () { int N = 0; for(Node temp = first; temp != null; temp = temp.next) { N++; } return N; }
/** * inverse returns the inverse of this symbol table. * if the symbol table contains duplicate values, you can use any of the keys for the inverse */ public LinkedListST
// looping through each node, once for (Node temp = first; temp != null; temp = temp.next) { // fetching key and value Value v = temp.val; Key k = temp.key; // adding to inverseST with v being the key and k being the value inverseST.put(v, k); }
// returning inversed st return inverseST;
} Tests:
package homework;
import stdlib.StdIn; import stdlib.StdOut;
public class hw1Driver { public static void main(String[] args) {
// the simple testing code from the textbook pg 370 // you may delete/comment this out if you wish LinkedListST
public static void allSizeTests() { sizeTest("",0); // test size on an empty ST (symbol table) sizeTest("abcde",5); // test size on a non-empty ST sizeTest("abcdefgh", 8); // test size on a non-empty ST sizeTest("a", 1); // test size on an ST of 1 element } public static void allInverseTests() { inverseTest("", ""); } // sample testing function. // param vals: all substrings of length 1 are added to the ST // param answer: the correct value of the ST for the input:vals public static void sizeTest( String vals, int answer ) { // create and populate the table from the input string vals LinkedListST
public static void inverseTest(String vals, String answer) { // create and populate the table from the input string vals LinkedListST
Question: I don't understand how to write the test for my inverse function. Help please?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started