Home Study tools
My courses
My books My folder Career Life Study Pack
Find solutions for your homework Search
Your subscription will expire on Dec 25, 2021. Turn on auto-renew to keep accessing solutions.
Turn on auto-renew
home / study / engineering / computer science / computer architecture / computer architecture solutions manuals / data structures and other objects using c++ / 4th edition / chapter 5 / problem 19p
Data Structures and Other Objects Using C++ Post a question
(4th Edition)
Answers from our experts for your tough
homework questions
Enter question
Chapter 5, Problem 19PP Bookmark Show all steps: ON
Step-by-step solution
Continue to post
20 questions remaining
Step 1 of 16
Refer to the specifications and design of node class given in FIGURE 5.10 for the complete code
from the textbook. My Textbook Solutions
Refer to the implementation of Linked List toolkit given from page 232 to 253 from the textbook.
Refer to the specifications, design and implementation of bag class using linked list given in the
FIGURE 5.11 and FIGURE 5.14 for the complete code from the textbook. Add a
textboo
Data Auditing &
Comment Structures... Assurance...
4th Edition 6th Edition
View all solutions
Step 2 of 16
Programming Plan:
• Modify the node class to contain element of type pair.
• Include the utility header in node class which contain the definition for the pair class.
• In the bag class modify the following functions to include functionality for the pair class:
• count(): modify the function to test for the second member of the pair class to check for a
match.
Great Clips® Near You
• erase(): modify this to match for the second member of the pair class and then if a match is
found then erase this node. Great Clips
Frisco 10AM–5PM
• erase_one(): modify this to match for the second member of the pair class and then if a match
is found then erase this node.
• grab(): modify the function to return the second member of the pair class object.
• insert(): modify this function to insert a pair object as the data of the node.
• operator+=: modify to add the second member of the addend to this bag.
• print(): define this method to print both the first as well as second member of the data.
• Define a main function in [Link] file.
• In this function create a bag object.
• Insert elements in the bag using the insert function.
• Call different function to test the functionality.
Comment
Home Study tools
My courses
My books My folder Career Life Study Pack
Step 3 of 16
Program:
node1.h file:
// FILE: node1.h
// add specifications from the textbook
// .
// .
// .
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include // Provides size_t and NULL
#include
namespace main_savitch_5
class node
public:
// TYPEDEF
typedef std::pair<size_t, double>
value_type;
// add code from the textbook
// .
// .
// .
};
// FUNCTIONS for the linked list toolkit
// add code from the textbook
// .
// .
// .
#endif
Comment
Step 4 of 16
[Link] file:
// FILE: [Link]
#include "node1.h"
#include
#include // Provides assert
#include // Provides NULL and size_t
using namespace std;
namespace main_savitch_5
// add code from the textbook
// .
// .
// . Home Study tools
My courses
My books My folder Career Life Study Pack
Comment
Step 5 of 16
bag3.h file:
// FILE: bag3.h
// add specifications from the textbook
// .
// .
// .
#ifndef MAIN_SAVITCH_BAG3_H
#define MAIN_SAVITCH_BAG3_H
#include // Provides size_t and NULL
#include "node1.h" // Provides node class
namespace main_savitch_5
class bag
public:
// TYPEDEFS
typedef std::size_t size_type;
typedef double value_type;
// add code from the textbook
// .
// .
// .
};
// add code from the textbook
// .
// .
// .
#endif
Comment
Step 6 of 16
[Link] file:
// FILE: [Link]
// add specifications from the textbook
// .
// .
// .
#include // Provides assert
#include // Provides NULL, rand, size_t
#include
#includeHome Study tools
My courses
My books My folder Career Life Study Pack
"[Link]"
#include "bag3.h"
using namespace std;
namespace main_savitch_5
// add code from the textbook
// .
// .
// .
// function to count the number of an item
bag::size_type bag::count(const value_type&
target) const
// define a counter
size_type value_count = 0;
// define a node to traverse the bag
node *cursor;
// check if the target is present
for (cursor = head_ptr; cursor != NULL;
cursor = cursor->link( ))
if (target == cursor->data( ).second)
// retrieve the count if present
value_count = cursor->data().first;
break;
// return the count
return value_count;
// function to erase all occurrence of the target
bag::size_type bag::erase(const value_type&
target)
// counter to store number of values removed
size_type value_count = 0;
// check if list is empty
if(size() == 0)
return value_count;
// nodes to traverse the bag
node *previous_node = NULL;
node *cursor;
// find the location of the node to remove
Comment
Step 7 of 16
for (cursor = head_ptr; cursor != NULL;
cursor Home Study tools
My courses
My books My folder Career Life Study Pack
= cursor->link( ))
if (target == cursor->data( ).second)
value_count = cursor->data().first;
break;
previous_node = cursor;
// check if the node is found or not
// check if the node is first node
if(previous_node == NULL)
if(cursor->link() == NULL)
head_ptr = NULL;
else
head_ptr = head_ptr->link();
else
previous_node->set_link(cursor->link());
// decrement the number of elements removed
many_nodes -= value_count;
// return the count
return value_count;
Comment
Step 8 of 16
// function to erase single occurrence of the target
bool bag::erase_one(const value_type& target)
// counter to store number of values removed
size_type value_count = 0;
// check if list is empty
if(size() == 0)
return value_count;
bool is_found = false;
node *previous_node = NULL;
node *cursor;
// traverse the bag
for (cursor = head_ptr; cursor != NULL;
cursor = cursor->link( ))
if (target == cursor->data( ).second)
value_count = cursor->data().first;
is_found = true;
break;Home Study tools
My courses
My books My folder Career Life Study Pack
previous_node = cursor;
// check if the node is found or not
if(previous_node == NULL && head_ptr->data()
.second == target && head_ptr->data()
.first > 1)
head_ptr->set_data(make_pair(
head_ptr->data().first - 1,
head_ptr->data().second));
else if(previous_node == NULL && head_ptr->data()
.second == target && head_ptr->data()
.first == 1)
if(cursor->link() == NULL)
head_ptr = NULL;
else
head_ptr = head_ptr->link();
else if(cursor != NULL && cursor->
data().first > 1)
cursor->set_data(make_pair(cursor->data()
.first - 1, cursor->data().second));
else if( is_found)
previous_node->set_link(cursor->link());
else
return false;
// decrement the element count
--many_nodes;
return true;
Comment
Step 9 of 16
// function to grab a random element from the bag
bag::value_type bag::grab( ) const
size_type idx;
// declare a node to traverse
node *cursor;
Home Study tools
My courses
My books My folder Career Life Study Pack
// make sure that bag is not empty
assert(size( ) > 0);
// generate a random index
idx = (rand( ) % list_length(head_ptr)) + 1;
// retrieve the node
cursor = list_locate(head_ptr, idx);
// return the value
return cursor->data( ).second;
// function to insert a value in the bag
void bag::insert(const value_type& entry)
// Library facilities used: node1.h
// check if list is empty
if(head_ptr == NULL)
// add one element
list_head_insert(head_ptr, make_pair(1,
entry));
++many_nodes;
return;
node *previous = NULL;
node *cursor = head_ptr;
bool isPresent = false;
// find the location to insert the new element
while(cursor != NULL && cursor->data().second
< entry)
previous = cursor;
cursor = cursor->link();
// insert the element
if(cursor != NULL && cursor->data().second ==
entry)
cursor->set_data(make_pair(cursor->data()
.first + 1, cursor->data().second));
else if(previous == NULL)
list_head_insert(head_ptr, make_pair(1,
entry));
else
list_insert(previous, make_pair(1, entry));
}
// increment number of elements
++many_nodes;
Home Study tools
My courses
My books My folder Career Life Study Pack
// operator to add two bag objects
void bag::operator +=(const bag& addend)
node* current = addend.head_ptr;
// traverse all nodes
while(current != NULL)
// insert total number
for(int idx = 1; idx <= current->data()
.first; idx++)
insert(current->data().second);
current = current->link();
// definition for overloaded = operator
void bag::operator =(const bag& source)
// add code from the textbook
// .
// .
// .
Comment
Step 10 of 16
bag operator +(const bag& b1, const bag& b2)
// add code from the textbook
// .
// .
// .
// function to print the items present in the bag
void bag::print()
// node to traverse the bag
node *cursor;
// display all elements
for( cursor = head_ptr; cursor != NULL;
cursor = cursor->link())
cout << "(" << cursor->data().first
<< ", " << cursor->data().second
<< ")"<< endl;
} Home Study tools
My courses
My books My folder Career Life Study Pack
Comment
Step 11 of 16
[Link] file:
#include
#include
#include "[Link]"
using namespace std;
using namespace main_savitch_5;
int main()
// create a bag object
bag bag1;
// insert elements in the bag
[Link](10.125);
[Link](10.125);
[Link](10.125);
[Link](10.123);
[Link](10.123);
[Link](1.5);
[Link](100.5);
[Link](10.124);
[Link](10.123);
// display the functionality of grab function
cout << "50 random values from the bag are:"
<< endl;
for(int idx = 0; idx < 50; idx++)
cout << [Link]() << endl;
// display the bag status
cout << "\nSize of bag is -> " << [Link]()
<< endl;
cout << "Number of 10.125 is -> " << [Link]
(10.125) << endl;
cout << "Number of 10.123 is -> " << [Link]
(10.123) << endl;
cout << "Number of 1.5 is -> " << [Link](1.5)
<< endl;
cout << "Number of 100.5 is -> " << [Link](100.5)
<< endl;
cout << "Number of 1000.5 is -> " << [Link]
(1000.5) << endl;
cout << "\nElements in bag are:"<<endl;
[Link]();
cout <<Home Study tools
My courses
My books My folder Career Life Study Pack
"\nBag after removing all occurrence "
"of 10.125:" <<endl;
[Link](10.125);
[Link]();
cout << "Size of bag is -> " << [Link]()
<< endl;
cout << "\nBag after removing all occurrence "
"of 10.123:" <<endl;
[Link](10.123);
[Link]();
// display the bag status
cout << "\nSize of bag is -> " << [Link]()
<< endl;
cout << "Number of 10.125 is -> " << [Link]
(10.125) << endl;
cout << "Number of 10.123 is -> " << [Link]
(10.123) << endl;
cout << "Number of 1.5 is -> " << [Link](1.5)
<< endl;
cout << "Number of 100.5 is -> " << [Link](100.5)
Comment
Step 12 of 16
<< endl;
cout << "Number of 1000.5 is -> " << [Link]
(1000.5) << endl;
cout << "\nInserting more elements..."<<endl;
[Link](10.125);
[Link](10.125);
[Link](10.125);
[Link](10.125);
[Link](10.125);
[Link](10.125);
cout << "\nElements in bag are:"<<endl;
[Link]();
cout << "\nBag after removing one occurrence "
"of 10.125:" <<endl;
bag1.erase_one(10.125);
[Link]();
// display the bag status
cout << "\nSize of bag is -> " << [Link]()
<< endl;
cout << "Number of 10.125 is -> " << [Link]
(10.125) << endl;
cout << "Number of 10.123 is -> " << [Link]
(10.123) << endl;
cout << "Number of 1.5 is -> " << [Link](1.5)
<< endl;
cout <<Home Study tools
My courses
"Number of 100.5 is -> " << [Link](100.5)
My books My folder Career Life Study Pack
<< endl;
cout << "Number of 1000.5 is -> " << [Link]
(1000.5) << endl;
cout << "\nBag after removing one occurrence "
"of 10.123:" <<endl;
bag1.erase_one(10.123);
[Link]();
// display the bag status
cout << "\nSize of bag is -> " << [Link]()
<< endl;
cout << "Number of 10.125 is -> " << [Link]
(10.125) << endl;
cout << "Number of 10.123 is -> " << [Link]
(10.123) << endl;
cout << "Number of 1.5 is -> " << [Link](1.5)
<< endl;
cout << "Number of 100.5 is -> " << [Link](100.5)
<< endl;
cout << "Number of 1000.5 is -> " << [Link]
(1000.5) << endl;
cout << "\nBag after removing one occurrence "
"of 10.124:" <<endl;
bag1.erase_one(10.124);
[Link]();
// display the bag status
cout << "\nSize of bag is -> " << [Link]()
<< endl;
cout << "Number of 10.125 is -> " << [Link]
(10.125) << endl;
cout << "Number of 10.123 is -> " << [Link]
(10.123) << endl;
cout << "Number of 1.5 is -> " << [Link](1.5)
<< endl;
cout << "Number of 100.5 is -> " << [Link](100.5)
<< endl;
cout << "Number of 1000.5 is -> " << [Link]
(1000.5) << endl;
cout << "\nBag after removing one occurrence "
"of 100.5:" <<endl;
bag1.erase_one(100.5);
[Link]();
// display the bag status
cout << "\nSize of bag is -> " << [Link]()
<< endl;
cout << "Number of 10.125 is -> " << [Link]
(10.125) << endl;
cout << "Number of 10.123 is -> " << [Link]
(10.123) << endl;
cout << "Number of 1.5 is -> " << [Link](1.5)
<< endl;Home Study tools
My courses
My books My folder Career Life Study Pack
cout << "Number of 100.5 is -> " << [Link](100.5)
<< endl;
cout << "Number of 1000.5 is -> " << [Link]
(1000.5) << endl;
bag bag2;
[Link](25.1);
[Link](25.1);
[Link](25.1);
[Link](25.1);
[Link](250.1);
[Link](125.1);
[Link](1.5);
[Link](1000.1);
cout << "\nElements in bag1 are:"<<endl;
[Link]();
Comment
Step 13 of 16
cout << "\nElements in bag2 are:"<<endl;
[Link]();
cout << "\nAdding bag1 and bag2..." << endl;
bag1 += bag2;
cout << "\nElements in bag1 are:"<<endl;
[Link]();
cout << "\nAdding bag1 and bag2..." << endl;
bag bag3 = bag1 + bag2;
cout << "\nElements in bag3 are:"<<endl;
[Link]();
bag bag4 = bag3;
cout << "\nElements in bag4 are:"<<endl;
[Link]();
cout << "\nAssigning a different bag to "
"bag4..." << endl;
bag4 = bag2;
cout << "\nElements in bag4 are:"<<endl;
[Link]();
Comment
Step 14 of 16
Sample output:
50 random values from the bag are:
10.123
10.124
100.5
1.5 Home Study tools
My courses
My books My folder Career Life Study Pack
100.5
100.5
10.125
10.125
10.124
100.5
1.5
1.5
10.123
10.124
10.123
10.123
1.5
10.124
10.124
10.123
10.123
100.5
10.124
10.125
10.124
10.124
10.123
10.123
10.125
1.5
10.124
10.123
10.123
10.125
100.5
10.124
10.124
100.5
1.5
100.5
10.125
10.123
10.124
10.125
10.125
100.5
10.123
10.123
10.125
10.125
Size of bag is -> 9
Number of 10.125 is -> 3
Number of 10.123 is -> 3
NumberHome Study tools
of 1.5 is -> 1
My courses
My books My folder Career Life Study Pack
Number of 100.5 is -> 1
Number of 1000.5 is -> 0
Elements in bag are:
(1, 1.5)
(3, 10.123)
(1, 10.124)
(3, 10.125)
(1, 100.5)
Bag after removing all occurrence of 10.125:
(1, 1.5)
(3, 10.123)
(1, 10.124)
(1, 100.5)
Size of bag is -> 6
Comment
Step 15 of 16
Bag after removing all occurrence of 10.123:
(1, 1.5)
(1, 10.124)
(1, 100.5)
Size of bag is -> 3
Number of 10.125 is -> 0
Number of 10.123 is -> 0
Number of 1.5 is -> 1
Number of 100.5 is -> 1
Number of 1000.5 is -> 0
Inserting more elements...
Elements in bag are:
(1, 1.5)
(1, 10.124)
(6, 10.125)
(1, 100.5)
Bag after removing one occurrence of 10.125:
(1, 1.5)
(1, 10.124)
(5, 10.125)
(1, 100.5)
Size of bag is -> 8
Number of 10.125 is -> 5
Number of 10.123 is -> 0
Number of 1.5 is -> 1
Number of 100.5 is -> 1
Number of 1000.5 is -> 0
Bag after removing one occurrence of 10.123:
(1, 1.5)
(1, 10.124)
(5, 10.125)
Home Study tools
My courses
My books My folder Career Life Study Pack
(1, 100.5)
Size of bag is -> 8
Number of 10.125 is -> 5
Number of 10.123 is -> 0
Number of 1.5 is -> 1
Number of 100.5 is -> 1
Number of 1000.5 is -> 0
Bag after removing one occurrence of 10.124:
(1, 1.5)
(5, 10.125)
(1, 100.5)
Size of bag is -> 7
Number of 10.125 is -> 5
Number of 10.123 is -> 0
Number of 1.5 is -> 1
Number of 100.5 is -> 1
Number of 1000.5 is -> 0
Bag after removing one occurrence of 100.5:
(1, 1.5)
(5, 10.125)
Size of bag is -> 6
Number of 10.125 is -> 5
Number of 10.123 is -> 0
Number of 1.5 is -> 1
Number of 100.5 is -> 0
Number of 1000.5 is -> 0
Elements in bag1 are:
(1, 1.5)
(5, 10.125)
Elements in bag2 are:
(1, 1.5)
(4, 25.1)
(1, 125.1)
(1, 250.1)
(1, 1000.1)
Adding bag1 and bag2...
Elements in bag1 are:
(2, 1.5)
(5, 10.125)
(4, 25.1)
(1, 125.1)
(1, 250.1)
(1, 1000.1)
Adding bag1 and bag2...
Elements in bag3 are:
(3, 1.5)
(5, 10.125)
(8, 25.1)
(2, 125.1)
(2, 250.1)
Home Study tools
My courses
My books My folder Career Life Study Pack
(2, 1000.1)
Comment
Step 16 of 16
Elements in bag4 are:
(3, 1.5)
(5, 10.125)
(8, 25.1)
(2, 125.1)
(2, 250.1)
(2, 1000.1)
Assigning a different bag to bag4...
Elements in bag4 are:
(1, 1.5)
(4, 25.1)
(1, 125.1)
(1, 250.1)
(1, 1000.1)
Comment
Was this solution helpful? 0 0
Recommended solutions for you in Chapter 5
Chapter 5, Solution 18PP Chapter 5, Solution 15PP
Refer to the specification and design of card class Refer to the specification and design of sequence
from Chapter 2 Project 4. Refer to the specification class provided under section 5.4 from the textbook.
and design of deck...
See solution
See solution
COMPANY
LEGAL & POLICIES
CHEGG PRODUCTS AND SERVICES
CHEGG NETWORK
Home Study tools
My courses
My books My folder Career Life Study Pack
CUSTOMER SERVICE
© 2003-2021 Chegg Inc. All rights reserved.