/* AryListViaLink1.java ** Instances of this class have the same basic capabilities as instances ** of the java.util.ArrayList class, but ONLY the most basic capabilities. ** That is, an instance of this class represents a list (or sequence, if ** you prefer) whose elements are referred to by their positions (numbered ** starting at zero) and in which element insertions, removals, and ** replacements can be effected at any position. ** ** Authors: R. McCloskey and < STUDENTS' NAMES > ** Known defects: ... */ public class AryListViaLink1 { // instance variable // ----------------- private Link1 dummy; // reference to dummy node // constructors // ------------ /* Initializes this AryListViaLink1 object by placing into it the ** elements in the given array. ** ** post: this.get(k) == ary[k] for all k in [0..ary.length), ** this.lengthOf() == ary.length */ public AryListViaLink1(T[] ary) { dummy = new Link1(null,null); Link1 pntr = dummy; for (int i=0; i != ary.length; i++) { pntr.next = new Link1(ary[i]); pntr = pntr.next; } } // Note: The following is equivalent to the body of the for-loop above: // pntr.next = new Link1(); // creates a new node with null values // pntr = pntr.next; // advances pntr to point to the new node // pntr.item = ary[i]; // places the desired item into the new node /* Initializes this AryListViaLink1 object to have no elements in it. ** post: this.lengthOf() == 0 */ public AryListViaLink1() { dummy = new Link1(null,null); } // observers // --------- /* Returns the length (i.e., # of elements in) this list. */ public int lengthOf() { return 0; // STUB!! } /* Returns the element at position k of this list. ** pre: 0 <= k < this.lengthOf() */ public T get(int k) { return find(k).item; } /* Displays the elements in the specified sublist of this list, starting ** at position 'low' and going up to, but not including, position 'high'. ** (The displayed elements are separated by spaces.) ** ** pre: 0 <= low <= high <= this.lengthOf() */ public void display(int low, int high) { // STUB!! (lowest priority) } /* Displays all the elements of this list (separated by spaces). */ public void display() { Link1 pntr = dummy.next; // start pntr at 0-th position while (pntr != null) { System.out.print(pntr.item + " "); pntr = pntr.next; } } // Assuming that the stubbed version of display() is made to work // correctly, the code for this version could be this one-liner: // display(0, lengthOf()); // mutators // -------- /* Inserts a new element ('newItem') into the list at the ** specified position (k). ** ** pre: 0 <= k <= this.lengthOf() ** post: For all ik, get(i) yields what get(i-1) did before. */ public void insert(int k, T newItem) { // STUB!! } /* Inserts a new element ('newItem') at the end of this list. */ public void append(T newItem) { insert(lengthOf(), newItem); } /* Deletes the element at the specified position (k) of this list ** and returns (a reference to) the deleted element. ** ** pre: 0 <= k < this.lengthOf() ** post: For all i=k, get(i) yields what get(i+1) did before. */ public T remove(int k) { return null; // STUB!! } /* Replaces the element at the specified position (k) with the ** specified element (replItem). ** pre: 0 <= k < this.lengthOf() ** post: For all ik, get(i) yields same value as before. */ public void replace(int k, T replItem) { Link1 pntr = find(k); pntr.item = replItem; } /* Reverses this list. ** post: Letting N be this.lengthOf(): ** For all i in [0..N), get(i) yields what get(N-1-i) did before. */ public void tsil() { if (this.lengthOf() > 1) { Link1 pntr = dummy.next; // make pntr point to 0th list node Link1 pntrSucc = pntr.next; // make pntrSucc point to 1st list node // missing code here to make 0th node last // Suggested loop invariant: // Let k be the number of times that the loop has iterated. // Then each of the nodes originally in positions [0..k] points // to what had been its predecessor (with the 0th node pointing to // nobody). Meanwhile, pntr and pntrSucc point to what were // originally the nodes at positions k and k+1, respectively. // The nodes originally at positions k+1 and above are unchanged. while (pntrSucc != null) { // missing loop body } // missing code here to bring things to a close } } // private utilities // ----------------- /* Returns a reference to the Link1 node that holds the element at ** position k of this list (or to the dummy node if k == -1). ** (Note that positions are numbered beginning with zero.) ** pre: -1 <= k < this.lengthOf() */ private Link1 find(int k) { Link1 pntr = dummy; for (int i = -1; i != k; i++) { pntr = pntr.next; } return pntr; } // main() method, for testing purposes // ----------------------------------- public static void main(String[] args) { // Create a list containing a phrase. String[] strAry = new String[] { "The", "cat", "in", "the", "hat" }; AryListViaLink1 aList = new AryListViaLink1(strAry); System.out.println("Initial list is:"); aList.display(); System.out.println(); aList.insert(4, "big"); aList.insert(1, "stupid"); System.out.println("\nAfter inserting \"big\" at 3 and" + " \"stupid\" at 1:\n "); aList.display(); System.out.println(); aList.remove(5); System.out.println("\nAfter deleting 5th item:\n "); aList.display(); System.out.println(); aList.replace(1, "idiotic"); System.out.println("\nAfter replacing 1st item with \"idiotic\":\n"); aList.display(); System.out.println(); } }