Presentation is loading. Please wait.

Presentation is loading. Please wait.

2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.

Similar presentations


Presentation on theme: "2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John."— Presentation transcript:

1 2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John Lewis, and Thomas Kuehne, VUW COMP 103 Marcus Frean Using Linked Structures

2 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Linked nodes support building linked lists various useful list operations can be added to LinkedNode  But… what about removing the first node in a list? should clients always need to check for “null”? how to avoid expensive searching for nodes? TODAY  Explicitly representing a linked list  Implementing other useful Containers with LinkedNode

3 3 Iterating through a linked list LinkedNode rest = squares; while (rest != null) { UI.println(rest.getValue()); rest = rest.next(); }...............or.................. for (LinkedNode rest = squares; rest != null; rest = rest.next()) { UI.println(rest.getValue()); } or we could do it with recursion.... 49 1 squares rest /

4 4 public void printList(LinkedNode list’) { if (list’ == null) return; UI.println(list’.getValue()); printList(list’.next()); } list list’ printing a linked list (recursive version) /** Print the values in the list starting at a node */ public void printList(LinkedNode list) { if (list == null) return; UI.println(list.getValue()); printList(list.next()); } “dog”“cat” “cow” recursive call ends the recursion

5 5 /** Print the values in the list starting at a node */ public void printList(LinkedNode list ) { if (list == null) return; UI.println(list.getValue()); printList(list.next()); } printing a linked list (recursive version) printList( list ) "cat" "dog" "cow" “dog” “cat” “cow” animals printList( animals ); printList( list’ ) printList( list’’ ) printList( list’’’ ) call return

6 6 recursive vs iterative versions......... /** Print the values in the list starting at a node */ public void printList(LinkedNode list) { if (list == null) return; UI.println(list.getValue()); printList(list.next()); } public void printList(LinkedNode list) { for (LinkedNode rest=list; rest != null; rest=rest.next() ) UI.println(rest.getValue()); } “dog”“cat” “cow” list recursive call base case

7 7 Exercise: return the value in the LAST node /** Return the value in the last node of the list starting at a node */ public E lastValue (LinkedNode list) { if (list == null) return null; // in case the whole list is empty if (list.next() == null) return list.getValue(); return lastValue( list.next() ); } or public E lastValue (LinkedNode list) { if (list == null) return null; // in case the whole list is empty LinkedNode rest=list; while (rest.next() != null) rest = rest.next(); return rest.getValue(); } Note the “eliminate special cases first ” style recursive iterative

8 8 Exercise: return the value in the LAST node  Evaluation of a recursive method that returns something lastValue( ) “dog” “cat” “cow” animals lastValue( ) call “cow”

9 9 Inserting (recursive) /** Inserts the value ‘item’ after position n in the list. Assumes list is not empty, and n >=1 */ public void insertItemAfterPos(E item, int n, LinkedNode list) { if (n == 1 ) list.setNext(new LinkedNode (item, list.next()); else insertItemAfterPos(item, n-1, list.next()); }  eg. insert X after position 2 in mylist insertItemAfterPos(“X”, 2, mylist); W Q RET mylist X can we improve this?

10 10 Inserting (iterative alternative) /** Inserts the value ‘item’ after position n in the list. Assumes list is not empty, and n >=1 */ public void insertItemAfterPos(E item, int n, LinkedNode list) { int pos = 0; LinkedNode rest = list; // rest points to the first node while (pos < n-1) { rest = rest.next(); pos++; } rest.setNext(new LinkedNode (item, rest.next()); } WQRET mylist what further assumption is being made?

11 11 Removing (recursive) /** Remove the value from the list Assumes list is not empty, and value not in first node */ public void remove (E item, LinkedNode list) { if (list.next() == null) return; // we are at the end of the list if ( list.next().getValue().equals(item) ) list.setNext( list.next().next() ); else remove(item, list.next()); }  eg. remove R from mylist  eg. remove Q from mylist – ouch: how can I alter mylist ?? ! WQRET mylist

12 12 Removing (iterative alternative) public void remove (E item, LinkedNode list) { LinkedNode rest=list; while (rest.next() != null && !rest.next().getValue().equals(item)) rest = rest.next(); if (rest.next() != null) rest.setNext(rest.next().next()); } need to check the reason for loop termination WQRET mylist

13 13 Navigating in linked structures  Can this be tricky ? Yes!  we hope you can read the code and see it makes sense  writing it (correctly) can be quite challenging  the ordering of assignments is vital (in order to not lose information)  just step along and stop when there aren’t any more (e.g. print all of a list)  look for a node and stop there (e.g. insertItemAfterPos)  look for a node and stop at one before that (e.g. insert, remove)  temporary variables are your friend  drawing diagrams helps a lot QUICK TIP: There are three typical patterns


Download ppt "2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John."

Similar presentations


Ads by Google