class LinkedList
+----+----+----+----+----+----+----+----+ | 17 | 20 | -2 | 5 | 10 | 33 | 6 | ...| +----+----+----+----+----+----+----+----+ | | | | | | | . . . V V V V V V V . . . +----+----+----+----+----+----+----+----+----+ | 17 | 20 | -2 | 5 | 10 | 33 | 6 | ...| NEW| +----+----+----+----+----+----+----+----+----+
int[] ia = new int[]; //
.-. .-. .-. .-. .-. | | | | | | | | | | . . . V | V | V | V | V | . . . +----+----+----+----+----+----+----+----+----+ | 17 | 20 | -2 | 5 | 10 | 33 | 6 | ...| ...| +----+----+----+----+----+----+----+----+----+
ArrayList
.-. .-. .-. .-. .-. .-. .-. | | | | | | | | | | | | | | . . . | V | V | V | V | V | V | V . . . +----+----+----+----+----+----+----+----+----+ | | | | | | | | ...| ...| +----+----+----+----+----+----+----+----+----+
40 | .-. .-. .-. .-. V | | | | | | | | . . . | V | V | V | V . . . +----+----+----+----+----+----+----+----+----+ | ...| ...| 33 | 44 | 55 | | | ...| ...| +----+----+----+----+----+----+----+----+----+
class LinkedList +-------------------------------------------------------------------+ | | | first class ~Node class ~Node class ~Node | | +-------+ +-------+ +-------+ +-------+ | | | *--+------>| 33 | +-->| 22 | +-->| 55 | | | +-------+ +-------+ | +-------+ | +-------+ | | | *--+---+ | *--+---+ | / | | | +-------+ +-------+ +-------+ | | | +-------------------------------------------------------------------+
linkList
public void displayList() { System.out.println("List (first-->last): "); System.out.println(" first=" + (first == null ? "null" : first.toString0())); Link current = first; // start at beginning of list while(current != null) // until end of list, { System.out.print(" "); current.displayLink(); // print data System.out.println(); current = current.next; // move to next link } System.out.println(""); }
public Link find(int key) { Link temp = null; // have reference to link // CODE FROM |displayList()|: Link current = first; // start at beginning of list while(current != null) // until end of list, { if ( current.iData == key ) { temp = current; break; } current = current.next; // move to next link } return temp; // return found link }
public boolean contains(int key) { return find(key) != null; }
public void addLast(int id, double dd) { Link newLink = new Link(id, dd); // |newLink.next| is |null| from constructor if ( first == null ) { first = newLink; return; } Link current = first; // start at beginning of list while(current.next != null) // until end of list, { current = current.next; // move to next link } current.next = newLink; }
public Link delete(int key) // delete link with given key { // Link current = first; // search for link Link previous = first; while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; }
linkList2