Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

1 2015-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 Thomas Kuehne 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 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

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

5 5 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., L17, slide #21 } WQRET head Alternatively, delegate to LinkedNode: head.insertItemAfterPos(item, n); Alternatively, delegate to LinkedNode: head.insertItemAfterPos(item, n);

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

7 7  ( → 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!

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

9 9 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 back Queue front

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

11 11 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 "2015-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