Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
55 slides
-
Memory Upload • Method remove() of LinkedList class • Counting
from 0 or 1 • Method toString() of LinkedList class -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
Remove() Method • Java’s garbage collection automatically removes the 1st
node 3 head size data next B D data next null 1 position list -
position list Remove() Method • Decrement size of list size--
; 2 head size data next B D data next null 1 -
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 -
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) -
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 -
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 -
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 -
toString() Method • Jump to toString() method definition public String
toString(){ 3 head size A data next data next B C data next null list -
csvFormat list toString() Method • Initialize empty String String csvFormat
= new String(""); 3 head size A data next data next B C data next null " " -
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 -
• 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 -
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 -
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 -
• 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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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) -
Memory Defragmenter • LinkedList class remove() method • My LinkedList
starts at 1, but Java API’s LinkedList starts at 0 • LinkedList class toString() method -
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