Example: LinkedSet<T>

Slides:



Advertisements
Similar presentations
Linked Lists Linear collections.
Advertisements

The List ADT Textbook Sections
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
CHAPTER 7 Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 7 Iterators Modified. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using.
COMP 103 Linked Stack and Linked Queue.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Linked Structures, LinkedStack
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.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Iteration Abstraction SWE Software Construction Fall 2009.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
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.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Iterators. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using iterators to solve.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
An Array-Based Implementation of the ADT List
Linked Data Structures
Iterators.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
The List ADT.
Chapter 4 Linked Structures.
EECE 310: Software Engineering
Storage Strategies: Dynamic Linking
ADT’s, Collections/Generics and Iterators
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Trees Tree nomenclature Implementation strategies Traversals
Chapter 16 Iterators.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Iteration Abstraction
Chapter 7 Iterators.
Java Software Structures: John Lewis & Joseph Chase
Linked Lists, Stacks and Queues Textbook Sections
null, true, and false are also reserved.
ArraySet Methods and ArrayIterator
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Linked Lists [AJ 15].
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Iteration Abstraction
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
List Implementation via Arrays
Lists.
Stacks, Queues, and Deques
Ordered Structures Wellesley College CS230 Lecture 10
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Example: LinkedSet<T> LinkedSet<T> Class LinkedSet Attributes/Constructor Linked Set Methods LinkedSet iterator method LinkedIterator<T> class Reading: L&C 3rd: 15.4 2nd: 4.1-4.5 1

LinkedSet<T> Class The LinkedSet<T> class implements the same SetADT<T> interface as ArraySet<T> using a singly linked list instead of an array Externally, your code is not much different when using the LinkedSet<T> class instead of the ArraySet<T> class: SetADT<String> mySet = new ArraySet<String>(); OR = new LinkedSet<String>(); Internally, method code of the LinkedSet<T> class is very different due to the difference in the underlying data structure being used. 2

LinkedSet<T> Class Again: We assume that SetADT<T> interface extends Iterable. 3

LinkedSet Attributes/Constructor The class definition starts with: public class LinkedSet<T> implements SetADT<T> { private static Random rand = new Random(); private int count; private LinearNode<T> contents; public LinkedSet() // default constructor count = 0; contents = null; // NOTE: not an array } 4

LinkedSet Attributes/Constructor We don’t need to define a default capacity or pass an initial capacity to the constructor We don’t need to instantiate an array object or any LinearNode objects in the constructor – just set contents to null 5

LinkedSet Methods Note that because we are not using a fixed size data structure such as an array, we don’t need a private expandCapacity() method. 6

LinkedSet Methods add – O(n) public void add (T element) { if (!contains(element)) LinearNode<T> node = new LinearNode<T>(element); node.setNext(contents); contents = node; count++; } 7

LinkedSet Methods contains – O(n) public boolean contains (T target) { LinearNode<T> next = contents; while (next != null) { if (next.getElement().equals(target) return true; next = next.getNext(); } return false; 8

LinkedSet Methods removeRandom – O(n) not O(1) like ArraySet public T removeRandom() throws EmptySetException { LinearNode<T> current; T result = null; if (isEmpty) throw new EmptySetException(); int choice = rand.nextInt(count) + 1; 9

LinkedSet Methods removeRandom (Continued) if (choice == 1) { // remove from beginning result = contents.getElement(); contents = contents.getNext(); } else { // remove from middle or end current = contents; for (int skip = 2; skip < choice; skip++) current = current.getNext(); result = current.getNext().getElement(); current.setNext(current.getNext().getNext()); count--; return result; } // bypassed LinearNode becomes garbage 10

LinkedSet iterator Method iterator – O(1) public Iterator<T> iterator { return new LinkedIterator<T>(contents) } We will study the LinkedIterator class to understand how it is implemented 11

LinkedIterator<T> Class We may have several collection classes like the LinkedSet class that use an underlying singly linked linear data structure Again, we would like to reuse one Iterator class for all of these collection classes So we write a general purpose Iterator class for use with linked data structures 12

LinkedIterator<T> Class The iterator method of the LinkedSet class instantiates and returns a reference to a LinkedIterator object to its caller The LinkedIterator constructor needs to get a reference to the first LinearNode object in the specific linked structure to be iterated 13

LinkedIterator<T> Class Class / Attribute Definitions and Constructor public class LinkedIterator<T> implements Iterator<T> { private LinearNode<T> current; // current position public LinkedIterator(LinearNode<T> current) this.current = current; } 14

LinkedIterator Methods hasNext – O(1) public boolean hasNext() { return current != null; } next – O(1) public T next() if (!hasNext()) throw new NoSuchElementException(); T result = current.getElement(); current = current.getNext(); return result; } // old LinearNode does not become garbage (Why?) 15

LinkedIterator Methods remove – O(1) We don’t need to implement real code for the remove method, but there is no return value that we can use to indicate that it is not implemented If we don’t implement it, we indicate that the code is not implemented by throwing an exception public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } 16

LinkedIterator Methods Again, if we implement the remove method, notice that we don’t specify the element that is to be removed and we do not return a reference to the element being removed It is assumed that the calling code has been iterating on condition hasNext() and calling next() and already has a reference The last element returned by next() is the element that will be removed 17

LinkedIterator Method Analysis All three LinkedIterator methods are O(1) However, they are usually called inside an external while loop or “for-each” loop Hence, the process of “iterating” through a collection using an Iterator is O(n) 18

LinkedIterator Class in Text There is an anomaly in the definition of the LinkedIterator class in the textbook The class has a count attribute and the constructor has a parameter to set count But that attribute is not used anywhere in the code of the other methods The code can and does always identify the end of the list by the null terminator value Why was count included? 19

LinkedSet Alternative If the count attribute was of no use in the LinkedIterator class, is it also of no use in the LinkedSet class? It is possible to implement the code of the LinkedSet class without a count attribute What is adversely affected by removing it? Size method would become O(n) instead of O(1) RemoveRandom method would need to use size method to calculate its choice of node to remove, but it is already O(n) 20