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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
COMP 103 Linked Stack and Linked Queue.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 5 Linked Lists II
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CS 367 Introduction to Data Structures Lecture 5.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
slides adapted from Marty Stepp and Hélène Martin
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
COMP 103 Linked Structures
Linked Structures – Part 2
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Building Java Programs
Week 3 - Friday CS221.
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Programming Abstractions
Implementing ArrayList Part 1
Binary Search Trees (I)
Top Ten Words that Almost Rhyme with “Peas”
Building Java Programs
Programming Abstractions
CSC 143 Java Linked Lists.
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

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 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 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 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 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 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 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 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 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 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 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  ( → 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 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 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 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 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(); }