Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Bag Implementation that Links Data

Similar presentations


Presentation on theme: "A Bag Implementation that Links Data"— Presentation transcript:

1 A Bag Implementation that Links Data
Chapter 3 Adapted from Pearson Education, Inc.

2 Contents The private inner class Node Creating a Linked Chain
Removing Items from a Linked Chain The Pros and Cons of Using a Chain to Implement the ADT Bag Adapted from Pearson Education, Inc.

3 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 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 Adapted from Pearson Education, Inc.

4 Class Node The private class Node Adapted from Pearson Education, Inc.

5 A Linked Implementation of the ADT Bag
public class LinkedBag < T > implements BagInterface < T > { private Node firstNode; // reference to first node private int numberOfEntries; public LinkedBag () { firstNode = null; numberOfEntries = 0; } // end default constructor // Implementations of the public methods // of BagInterface go here. private class Node // private inner class { private T data; // entry in bag private Node next; // link to next node private Node (T dataPortion) { this (dataPortion, null); } private Node (T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } // end constructor } // end Node } // end LinkedBag Adapted from Pearson Education, Inc.

6 Adding first item to an empty chain
Figure 3-6 An empty chain and a new node; after adding a new node to the empty chain Adapted from Pearson Education, Inc.

7 Adding a node at the beginning of a chain
Figure 3-7 A chain and a new node; The chain after adding the new node Adapted from Pearson Education, Inc.

8 Method add Adapted from Pearson Education, Inc.

9 Method toArray ( currentNode != null )
Adapted from Pearson Education, Inc.

10 Method getFrequencyOf
Adapted from Pearson Education, Inc.

11 Method getFrequencyOf
( currentNode != null ) We can get rid of the integer variable counter Adapted from Pearson Education, Inc.

12 Method contains Adapted from Pearson Education, Inc.

13 Method contains return true;
We can get rid of the boolean variable found false; Adapted from Pearson Education, Inc.

14 Removing an Item from a Linked Chain
Case 1 Your item is first in the chain of items. Steps required Locate first item by checking firstNode. Check next of first item and make it the new firstNode. Get rid of the first item. firstNode next Adapted from Pearson Education, Inc.

15 Removing an Item from a Linked Chain
Case 2 Desired item is NOT first in the chain of items. Steps required: Find reference to Desired item Move data from the first item in place of desired item. Remove first item using steps described for Case 1. xyz abc klm Desired item First item xyz abc klm Desired item Adapted from Pearson Education, Inc.

16 Method remove …(Case 1) Adapted from Pearson Education, Inc.

17 Method remove …(Case 2) Adapted from Pearson Education, Inc.

18 Method remove …(Case 2) Do we need the boolean variable result?
return currentNode; Do we need the boolean variable result? Did we forget anything before returning? Adapted from Pearson Education, Inc.

19 Method clear Adapted from Pearson Education, Inc.

20 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 Why? Adapted from Pearson Education, Inc.

21 End Chapter 3 Adapted from Pearson Education, Inc.


Download ppt "A Bag Implementation that Links Data"

Similar presentations


Ads by Google