public Link search(int index) { System.out.println("\n\nBegin Search index: " + index + ", listCount " + listCount); if (index < 0 || index >= listCount) return null; // if the index wasn't found do not continue Link c = first; // begin at the beginning for (int i = 0; i < index; i++) { // loop until index c = c.getNext(); // get the next Link c.displayData(); // print the data } return c; }
public void printAll() { System.out.println("\nBegin Linkedlist First--->Last"); Link c = first; int count = 0; while (c.getNext() != null) { c.displayData(); c = c.getNext(); count++; } System.out.println("\nEnd Linkedlist\n"); }
public int searchIndex(String name) { Link c = first; int count = 0; while (c.getNext() != null) { if (((NAW) c.getData()).getName().equals(name)) { c.displayData(); return count; } c = c.getNext(); count++; } return -1; // not found }
public void remove(String name) { int index = this.searchIndex(name) - 1; // index minus 1, we only want the previous element if (index < 0) return; // if the index wasn't found do not continue Link c = first; // begin at the beginning for (int i = 0; i < index; i++) { // loop until index c = c.getNext(); // get the next Link c.displayData(); // print the data } c.setNext( c.getNext() .getNext()); // set the previous Link from the one that we where searching the next Link // so basically we skip one to forget listCount--; // count the listCount down }
public Link search(String name) { System.out.println("\n\nBegin Search name: " + name); Link c = first; int count = 0; while (c.getNext() != null) { if (((NAW) c.getData()).getName().equals(name)) { System.out.println("count : " + count); c.displayData(); return c; } c = c.getNext(); count++; } System.out.println("End Search\n\n"); return null; }