A Bag Implementation that Links Data

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
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.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
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.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
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.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
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.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
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.
Linked Lists Single Linked Lists Double Linked Lists.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Link-Based Implementations
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 Linked Lists.
Bag Implementations that Use Arrays
Queues Chapter 4.
A Bag Implementation that Links Data
Chapter 4 Linked Lists
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Adapted from Pearson Education, Inc.
Chapter 4 Link Based Implementations
Stack Implementations
Chapter 4 Linked Lists.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recitation 5 CS0445 Data Structures
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Adapted from Pearson Education, Inc.
List Implementations Chapter 9.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks: Implemented using Linked Lists
Chapter 4 Linked Lists.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Array-Based Implementations
Queues CSC212.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
ADT Queue (Array Implementation)
Data Structures & Algorithms
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Lecture 6: Linked Data Structures
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Queues: Implemented using Linked Lists
Bag Implementations that Use Arrays
Linked Lists Chapter 5 (continued)
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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 Adapted from Pearson Education, Inc.

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.

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.

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

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.

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.

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.

Method add Adapted from Pearson Education, Inc.

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

Method getFrequencyOf Adapted from Pearson Education, Inc.

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

Method contains Adapted from Pearson Education, Inc.

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

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.

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.

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

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

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.

Method clear Adapted from Pearson Education, Inc.

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.

End Chapter 3 Adapted from Pearson Education, Inc.