CH 03 A Bag ImplementationThatLinksData

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 41

A Bag Implementation

that Links Data


Chapter 3

Data Structures and Abstractions with Java, 4e


Frank Carrano

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.
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

This occurrence of T
is optional

LISTING 3-6 The class LinkedBag when


Node is in the same package
© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.
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.

You might also like