A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
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.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Linked Bag Chapter 3.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Link Based Implementations
Link-Based Implementations
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Bag Implementations that Use Arrays
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 4 Link Based Implementations
Chapter 3 Array-Based Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Hashing as a Dictionary Implementation
List Implementations Chapter 9
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations Chapter 9.
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Array-Based Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
Presentation transcript:

A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents Linked Data  Forming a Chain by Adding to Its Beginning A Linked Implementation of the ADT Bag  The Private Class Node  An Outline of the Class LinkedBag  Defining Some Core Methods  Testing the Core Methods  The Method getFrequencyOf  The Method contains Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents Removing an Item from a Linked Chain  The Methods remove and clear A Class Node That Has Set and Get Methods The Pros and Cons of Using a Chain to Implement the ADT Bag Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Describe linked organization of data Describe how to add new node to beginning of chain of linked nodes Describe how to remove first node in chain of linked nodes Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Describe how to locate particular piece of data within chain of linked nodes Implement ADT bag by using chain of linked nodes Describe differences between array-based and linked implementations of ADT bag Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-1 A chain of five desks Linked Data Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-2 One desk in the room Forming a Chain by Adding to Its Beginning Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-3 Two linked desks, with the newest desk first Forming a Chain by Adding to Its Beginning Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-4 Three linked desks, with the newest desk first Forming a Chain by Adding to Its Beginning Copyright ©2012 by Pearson Education, Inc. All rights reserved

A Linked Implementation of the ADT Bag The private class Node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-5 Two linked nodes that each reference object data The Private Class Node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Class LinkedBag View source code of Node class, Listing 3- 1Listing 3- 1 Note outline of class LinkedBag (Listing 3-2) which uses class NodeListing 3-2 Note: Code listing files must be in same folder as PowerPoint files for links to work Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-6 (a) An empty chain and a new node; (b) after adding a new node to a chain that was empty Copyright ©2012 by Pearson Education, Inc. All rights reserved

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 Copyright ©2012 by Pearson Education, Inc. All rights reserved

The Method add Copyright ©2012 by Pearson Education, Inc. All rights reserved

The method toArray Copyright ©2012 by Pearson Education, Inc. All rights reserved

Testing Core Methods Initial test methods  add  toArray Stub methods (for now)  getCurrentSize  isFull  isEmpty View source code, Listing 3-3Listing 3-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Method getFrequencyOf Copyright ©2012 by Pearson Education, Inc. All rights reserved

Method contains Copyright ©2012 by Pearson Education, Inc. All rights reserved

Removing an Item from a Linked Chain Case 1  Your item is first in the chain of items. Figure 3-8 A chain of desks just prior to removing its first desk Copyright ©2012 by Pearson Education, Inc. All rights reserved

Steps Required for Case 1 1.Locate first desk by asking the instructor for address. 2.Give address written on first desk to instructor. This is address of second desk in chain. 3.Return the first desk to the hallway. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-9 A chain of desks just after removing its first desk (after first two steps) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Removing an Item from a Linked Chain Case 2  Your item is not first in the chain of items. Steps required for Case 2 1. Move student in the first desk to former desk. 2. Remove first desk using steps described for Case 1. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 3-10 A chain of nodes (a) just prior to removing the first node; (b) just after removing the first node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Method remove Copyright ©2012 by Pearson Education, Inc. All rights reserved

Removing a Given Entry Required steps 1. Replace entry in node N with entry in first node. 2. Remove first node from chain. Must find reference to specified value Copyright ©2012 by Pearson Education, Inc. All rights reserved

Method getReferenceTo Copyright ©2012 by Pearson Education, Inc. All rights reserved

Method remove for Given Value Method clear Copyright ©2012 by Pearson Education, Inc. All rights reserved

Putting It Together Class Node that has Set and Get methods  View source code, Listing 3-4Listing 3-4 Class Node with package access  Source code, Listing 3-5Listing 3-5 Class LinkedBag with Node in same package  Note source code, Listing 3-6Listing 3-6 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Pros and Cons of Using a Chain Bag can grow and shrink in size as necessary Possible to remove and recycle nodes Copying values for array enlargement not needed Removing specific value entry requires search of array or chain Chain requires more memory than array of same length Copyright ©2012 by Pearson Education, Inc. All rights reserved

End Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved