1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | /* This is really a linked list class for a linked list of Strings. * No loops allowed in these parts! */ public class Recursion { // Class for the links in the linked list. // No getters and setters necessary because the fields are public. private class Node { public String data; public Node next; public Node(String data) { this.data = data; next = null; } public Node(String data, Node next) { this.data = data; this.next = next; } } // Pointer to the first Node private Node first; public Recursion() { first = null; } // Builds a linked list from the given array. // I've written this one for you - you're welcome! // Feel free to use it for your own debugging. public void build(String[] contents) { first = buildHelper(contents, 0); } // (No need to change this method.) private Node buildHelper(String[] contents, int index) { if (index >= contents.length) { return null; } else { return new Node(conents[index], buildHelper(contents, index+1)); } } // Prints the list in order. // We did this in class! public void print() { printHelper(first); } // (No need to change this method.) private void printHelper(Node cur) { if (cur != null) { System.out.println(cur.data); printHelper(cur.next); } } // Inserts a new element into the second position // (i.e., index 1). public void addSecond(String element) { } // Prints the list in reverse order, last node first. // I've started a helper method for you below. public void printInReverseOrder() { } private void printReverseHelper(Node cur) { } // Gets the longest word in the list. // If more than one word is tied for the longest, // you should return the FIRST one. public String longest() { return null; // Change this line!!! } // Helper method private String longestHelper(Node cur) { return null; // Change this line!!! } // Gets the contents of the i-th node, counting from 0. // Remember you have to write this recursively. // Would you like to add a helper method??? public String get(int index) throws IndexOutOfBoundsException { return null; // Change this line!!! } // Duplicates every element, so like // ["a" "b" "c"] would become // ["a" "a" "b" "b" "c" "c"] // A helper method would be lovely, wouldn't it? public void duplicate() { } // Removes all occurrences of the given string // Don't forget your helper method! public void remove(String rem) { } } |