2015-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
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists Linear collections.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
COMP 103 Linked Stack and Linked Queue.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Lists Based on content from: Java Foundations, 3rd Edition.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
COMP 103 Hashing 2013-T2 Lecture 28 Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay.
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
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()
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
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.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
LinkedList Many slides from Horstmann modified by Dr V.
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.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
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.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
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.
“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.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
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.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
COMP 103 Linked Structures
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Implementing ArrayList Part 1
Binary Search Trees (I)
THURSDAY, OCTOBER 17 IN LAB
More Data Structures (Part 1)
Presentation transcript:

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