Presentation is loading. Please wait.

Presentation is loading. Please wait.

2014-T2 Lecture 19 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 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John."— Presentation transcript:

1 2014-T2 Lecture 19 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 more about linked structures

2 2 RECAP-TODAY RECAP  we have been looking at using a LinkedNode class  we looked at retrieving the value in the last linkedNode in a chain, and got as far as writing methods (both recursive and iterative) for inserting a new item at an arbitrary point in the chain... TODAY  we continue where we left off on Tuesday: removing an element from a chain  But LinkedNode can’t do everything we desire in a List...  Explicitly representing a linked list  Implementing other useful Containers with LinkedNode

3 3 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?

4 4 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?

5 5 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

6 6 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

7 7 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

8 8 LinkedList  A linked list should support operations such as “isEmpty()” and “removeFirst()”  Linked nodes cannot support these goals directly  Solution: Use a LinkedList class as a “wrapper” class  LinkedList uses LinkedNode much like ArrayList uses a Java array  LinkedList delegates many operations to LinkedNode  Special cases dealt with by LinkedList A B A B Internal data structure: Linked Node Internal data structure: Array My Program _______ ________ ______ My Program _______ ________ ______ LinkedList ArrayList uses

9 9 Making a LinkedList class class LinkedList { private LinkedNode head = null; public void printList( ) { for (LinkedNode rest = head; rest != null; rest = rest.next() ) System.out.printf("%s, ", rest.get()); } public void printList( ) { if (head != null) head.printAll(); } … } Underlying data structure Direct implementation Delegation to LinkedNode behaviour head LinkedList LinkedNode

10 10 Inserting /** Inserts the value at position n in the list (counting from 0) Assumes list is not empty, and 0 <= n < length */ public void insert (E item, int n) { if ( n == 0 ) { head = new LinkedNode (item, head); } else insertItemAfterPos(item, n, head); // c.f. Slide #9 or #10 } WQRET head Alternatively, delegate to LinkedNode: head.insertItemAfterPos(item, n); Alternatively, delegate to LinkedNode: head.insertItemAfterPos(item, n);

11 11 Types of Lists: ArrayLists, LinkedLists > Collection > Collection > List > List > AbstractList > AbstractList > ArrayList > ArrayList > AbstractSequentialList > AbstractSequentialList > LinkedList > LinkedList underlying data structure: ARRAY underlying data structure: LINKED NODE Maintaining order & growing is expensive Indexing is expensive

12 12  ( → Stack )  ( → Queue )  ( → CursorList ) Cost of Indexing?!  Random access of linked nodes is costly  What to do about it?  Solution 1: Do not allow it  Solution 2: Restrict it  Solution 3: Support it to some extent It’s a feature, not a bug!

13 13 No Random Access: e.g., Stack  Which end of the list to use for the top element?  need for efficiently adding and removing elements top Stack

14 14 Restricted Access: e.g., Queue  Which end of the list to use for adding elements?  How to efficiently remove elements?  expensive to remove from the end of a list, even if you have a direct reference to the last element  easy to remove from the front (so we should add to the back) back Queue front

15 15 Some Support: e.g., Cursor List  Maintaining a current manipulation point  not as flexible as random access  sometimes sufficient  Is this a full replacement for an iterator? head CursorList cursor

16 16 Iterator (external Cursor) public Iterator iterator() { return new LinkedNodeIterator(data); } private class LinkedNodeIterator implements Iterator { private LinkedNode node; // node containing next item public LinkedNodeIterator (LinkedNode node) { this.node = node; } public boolean hasNext () { return (node != null); } public E next () { if (node == null) throw new NoSuchElementException(); E ans = node.getValue(); node = node.next(); return ans; } public void remove () { throw new UnsupportedOperationException(); }


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

Similar presentations


Ads by Google