Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture16.pdf

Avatar for William Albritton William Albritton
September 14, 2014
73

 lecture16.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

Transcript

  1. Memory Upload • Method remove() of LinkedList class • Counting

    from 0 or 1 • Method toString() of LinkedList class
  2. Algorithm for Remove 1. If list is empty, throw ListException

    2. If position is less than 1, or larger than list size, throw ListException 3. If the position is 1, change the address in head to the 2nd node 4. If the position is greater than 1 a. Point Node variable previous to 1st node b. Point Node variable current to 2nd node
  3. Algorithm for Remove c. Loop position-2 number of times i.

    Point previous to current node ii. Point current to next node d. Point the previous node to node after current node (“skip” over current node, thus removing the node) 5. Decrease size of list by 1
  4. Remove() Method • Strings "A", "B", "C", and "D" have

    already been added to a list list.remove(3); list 4 head size A data next data next B C data next D data next null
  5. Remove() Method • Jump to remove() method definition public void

    remove(Integer position){ position list 4 head size A data next data next B C data next D data next null 3
  6. Remove() Method • Point previous to 1st node in list

    Node<T> previous = head; position previous list 4 head size A data next data next B C data next D data next null 3
  7. Remove() Method • Point current to 2nd node in list

    Node<T> current = head.getNext(); position current previous list 4 head size A data next data next B C data next D data next null 3
  8. Remove() Method • Start for loop (initialize variable i) for(int

    i=2;i<position;i++){ position current previous i list 4 head size A data next data next B C data next D data next null 3 2
  9. Remove() Method • Start for loop (is i < position?)

    for(int i=2;i<position;i++){ position current previous i list 4 head size A data next data next B C data next D data next null 3 2
  10. Remove() Method • 1st loop: assign previous to current previous

    = current; position current previous i list 4 head size A data next data next B C data next D data next null 3 2
  11. Remove() Method • 1st loop: assign current to next node

    current = current.getNext(); position current previous i list 4 head size A data next data next B C data next D data next null 3 2
  12. Remove() Method Remove() Method • Finish 1st loop of for

    loop (add 1 to i) for(int i=2;i<position;i++){ position current previous i list 4 head size A data next data next B C data next D data next null 3 3
  13. Remove() Method Remove() Method Remove() Method • Start 2nd loop

    (is i < position?) for(int i=2;i<position;i++){ position current previous i list 4 head size A data next data next B C data next D data next null 3 3
  14. Remove() Method Remove() Method Remove() Method • Finish for loop

    (i popped off stack) for(int i=2;i<position;i++){ position current previous list 4 head size A data next data next B C data next D data next null 3
  15. Remove() Method Remove() Method Remove() Method • Assign previous next

    to current next previous.setNext(current.getNext()); position current previous list 4 head size A data next data next B C data next D data next null 3
  16. Remove() Method Remove() Method Remove() Method • Previous & current

    popped off stack position list 4 head size A data next data next B C data next D data next null 3
  17. Remove() Method Remove() Method Remove() Method • Java’s garbage collection

    automatically removes 3rd node position list 4 head size A data next data next B D data next null 3
  18. Remove() Method Remove() Method Remove() Method • Decrease size of

    list by 1 size- -; position list 3 head size A data next data next B D data next null 3
  19. Remove() Method Remove() Method Remove() Method • At the end

    of method declaration parameter position popped off stack list 3 head size A data next data next B D data next null
  20. Remove() Method Remove() Method Remove() Method • Program control returns

    to line after method call list.remove(3); list.remove(1); list 3 head size A data next data next B D data next null
  21. Remove() Method Remove() Method Remove() Method • The next line

    has method call that removes from front of linked list list.remove(1); list 3 head size A data next data next B D data next null
  22. Remove() Method Remove() Method Remove() Method • Jump to remove()

    method definition public void remove(integer position){ position list 3 head size A data next data next B D data next null 1
  23. Remove() Method Remove() Method Remove() Method • Since this if

    statement is true… if(position.equals(1)){... 3 head size A data next data next B D data next null 1 position list
  24. Remove() Method • Skip over the 1st node to remove

    it head = head.getNext(); position list 3 head size A data next data next B D data next null 1
  25. Remove() Method • Java’s garbage collection automatically removes the 1st

    node 3 head size data next B D data next null 1 position list
  26. position list Remove() Method • Decrement size of list size--

    ; 2 head size data next B D data next null 1
  27. Remove() Method • Return to main() method, so variable position

    is popped off stack list.remove(1); ... 2 head size data next B D data next null list
  28. Big-O Break • Like the other methods that manipulate a

    linked list, method remove() must also loop through the nodes to remove a node • In worst case, remove() loops n-2 times to remove the last node in the linked list, so Big-O is O(n-2) = O(n)
  29. A Note on Counting • The Java API has class

    LinkedList, which starts counting from zero (0) • However, my LinkedList class starts counting from one (1) • For me, counting from one (1) makes more sense, since people are using the linked list class • So if you use the Java API LinkedList class, be aware of the difference with my code for the LinkedList class
  30. Algorithm for Display 1. Initialize empty string csvFormat 2. Initialize

    integer position to 1 3. Loop through all the nodes a. Add position and toString() method of node’s data to end of string csvFormat b. Increment position 4. Return string csvFormat
  31. toString() Method • List of Strings "A", "B", and "C"

    System.out.println(list.toString()); 3 head size A data next data next B C data next null list
  32. toString() Method • Jump to toString() method definition public String

    toString(){ 3 head size A data next data next B C data next null list
  33. csvFormat list toString() Method • Initialize empty String String csvFormat

    = new String(""); 3 head size A data next data next B C data next null " "
  34. csvFormat list • Initialize Integer position to 1 Integer position

    = new Integer(1); toString() Method 3 head size A data next data next B C data next null " " 1 position
  35. • 1st loop: start for loop for(Node<T> current = head;

    current != null; current = current.getNext()){ csvFormat list toString() Method 3 head size A data next data next B C data next null " " 1 position current
  36. for(Node<T> current = head; current != null; current = current.getNext()){

    • 1st loop: is variable current not null? csvFormat list toString() Method 3 head size A data next data next B C data next null " " 1 position current
  37. csvFormat = csvFormat + position + ", " + current.toString()

    + "\n"; • 1st loop: add to end of csvFormat csvFormat list toString() Method 3 head size A data next data next B C data next null 1 position current 1, A
  38. • 1st loop: increment integer position position++; csvFormat list toString()

    Method 3 head size A data next data next B C data next null 2 position current 1, A
  39. toString() Method • End 1st loop: point current to next

    for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2
  40. toString() Method • Begin 2nd loop: is current not null?

    for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2
  41. toString() Method • 2nd loop: add to end of csvFormat

    csvFormat = csvFormat + position + ", " + current.toString() + "\n”; csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 2
  42. toString() Method • 2nd loop: increment position position++; csvFormat current

    position list 3 head size A data next data next B C data next null 1, A 2, B 2
  43. toString() Method • End 2nd loop: point current next node

    for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3
  44. toString() Method • Start 3rd loop: is current not null?

    for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3
  45. toString() Method • 3rd loop: add to end of csvFormat

    csvFormat = csvFormat + position + ", " + current.toString() + "\n"; csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 3
  46. toString() Method • 3rd loop: increment position position++; csvFormat current

    position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 4
  47. toString() Method • End 3rd loop: point current to next

    for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 4 null
  48. toString() Method • Start 4th loop: is current not null?

    for(Node<T> current = head; current != null; current = current.getNext()){ csvFormat current position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 4 null
  49. toString() Method • End for loop, so current popped off

    stack csvFormat position list 3 head size A data next data next B C data next null 1, A 2, B 3, C 4
  50. toString() Method • Return csvFormat to main() method return csvFormat;

    csvFormat position list toString() Method 3 head size A data next data next B C data next null 1, A 2, B 3, C 4
  51. toString() Method • Print csvFormat object to screen System.out.println(list.toString()); System.out

    list 3 head size A data next data next B C data next null 1, A 2, B 3, C
  52. Big-O Break • Even without looking at the code, we

    can see that the toString() method displays all n objects in the list • Therefore, Big-O is O(n)
  53. Memory Defragmenter • LinkedList class remove() method • My LinkedList

    starts at 1, but Java API’s LinkedList starts at 0 • LinkedList class toString() method
  54. Task Manager • Before the next class, you need to:

    1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture