A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 12: Data Structures
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Data Structures and Abstractions with Java, 4e Frank Carrano
Data Structures and Abstractions with Java, 4e Frank Carrano
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Linked Bag Chapter 3.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Link-Based Implementations
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 12: Data Structures
Bag Implementations that Use Arrays
A Bag Implementation that Links Data
A Bag Implementation that Links Data
Chapter 4 Link Based Implementations
Chapter 14: Queue and Priority Queue Implementations
Iterators (short version)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations Chapter 9.
Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Bag Implementations that Use Arrays
A List Implementation That Links Data
List Implementations that Use Arrays
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

What Is an Iterator? An object that traverses a collection of data During iteration, each data item is considered once − Possible to modify item as accessed Should implement as a distinct class that interacts with the ADT © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Problems with Array Implementation Array has fixed size May become full Alternatively may have wasted space Resizing is possible but requires overhead of time © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Analogy Empty classroom Numbered desks stored in hallway − Number on back of desk is the “address” Number on desktop references another desk in chain of desks Desks are linked by the numbers © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Analogy FIGURE 3-1 A chain of five desks © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Forming a Chain by Adding to Its Beginning FIGURE 3-2 One desk in the room © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Forming a Chain by Adding to Its Beginning FIGURE 3-3 Two linked desks, with the newest desk first © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Forming a Chain by Adding to Its Beginning FIGURE 3-4 Three linked desks, with the newest desk first. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Forming a Chain by Adding to Its Beginning Pseudocode details the steps taken to form a chain of desks © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Private Class Node LISTING 3-1 The private inner class Node © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Private Class Node FIGURE 3-5 Two linked nodes that each reference object data © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

An Outline of the Class LinkedBag LISTING 3-2 An outline of the class LinkedBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

An Outline of the Class LinkedBag LISTING 3-2 An outline of the class LinkedBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Beginning a Chain of Nodes FIGURE 3-6 (a) An empty chain and a new node; (b) after adding a new node to a chain that was empty © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Beginning a Chain of Nodes FIGURE 3-7 A chain of nodes (a) just prior to adding a node at the beginning; (b) just after adding a node at the beginning © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Beginning a Chain of Nodes The method add © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Method toArray The method toArray returns an array of the entries currently in a bag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Test Program LISTING 3-3 A sample program that tests some methods in the class LinkedBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Test Program LISTING 3-3 A sample program that tests some methods in the class LinkedBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Test Program LISTING 3-3 A sample program that tests some methods in the class LinkedBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Method getFrequencyOf Counts the number of times a given entry appears © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Method contains Determine whether a bag contains a given entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Removing an Item from a Linked Chain Case 1: Your desk is first in the chain of desks. Case 2: Your desk is not first in the chain of desks. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Removing an Item from a Linked Chain Case 1 1.Locate first desk by asking instructor for its address. 2.Give address written on the first desk to instructor. This is address of second desk in chain. 3.Return first desk to hallway. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Removing an Item from a Linked Chain FIGURE 3-8 A chain of desks just prior to removing its first desk © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Removing an Item from a Linked Chain FIGURE 3-9 A chain of desks just after removing its first desk © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Removing an Item from a Linked Chain Case 2 1.Move the student in the first desk to your former desk. 2.Remove the first desk using the steps described for Case 1. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Removing an Item from a Linked Chain FIGURE 3-10 A chain of nodes (a) just prior to removing the first node; (b) just after removing the first node © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Method remove Note need for private method getReferenceTo © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Method remove Note use of method getReferenceTo © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Method clear As in previous implementation, uses isEmpty and remove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Class Node That Has Set and Get Methods LISTING 3-4 The inner class Node with set and get methods © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Class Node That Has Set and Get Methods LISTING 3-4 The inner class Node with set and get methods © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Class Node That Has Set and Get Methods LISTING 3-4 The inner class Node with set and get methods © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

A Class within A Package LISTING 3-5 The class Node with package access © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

A Class within A Package LISTING 3-5 The class Node with package access © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

A Class within A Package LISTING 3-5 The class Node with package access © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

When Node Is in Same Package LISTING 3-6 The class LinkedBag when Node is in the same package © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This occurrence of T is optional

Pros of Using a Chain Bag can grow and shrink in size as necessary. Remove and recycle nodes that are no longer needed Adding new entry to end of array or to beginning of chain both relatively simple Similar for removal © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Cons of Using a Chain Removing specific entry requires search of array or chain Chain requires more memory than array of same length © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

End Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.