Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

1 2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John Lewis, 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 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

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

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

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.get(); node = node.next(); return ans; } public void remove () { throw new UnsupportedOperationException(); }


Download ppt "2013-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