Question: Need help with linked list in dynamic class. Implement -= as a member function and - as a non member function (similar to += and

Need help with linked list in dynamic class.

Implement -= as a member function and - as a non member function (similar to += and +). Here are the two headers from bag.h:

o void operator -=(const bag &subtrahend);

o bag operator-(const bag & b1, const bag & b2);

//header file

#include // Provides size_t and NULL #include "node2.h" // Provides node class namespace main_savitch_5 { class bag { public: // TYPEDEFS typedef std::size_t size_type; typedef node::value_type value_type; // CONSTRUCTORS and DESTRUCTOR bag( ); bag(const bag& source); ~bag( ); // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void operator +=(const bag& addend); void operator =(const bag& source); void operator -=(const bag& subtrahend); // CONSTANT MEMBER FUNCTIONS size_type size( ) const { return many_nodes; } size_type count(const value_type& target) const; value_type grab( ) const; private: node *head_ptr; // List head pointer size_type many_nodes; // Number of nodes on the list };

// NONMEMBER FUNCTIONS for the bag class: bag operator +(const bag& b1, const bag& b2); bag operator -(const bag& b1, const bag& b2); } #endif

//cpp file

#include "Bag3.h"

#include // Provides assert #include // Provides NULL, rand, size_t #include "node2.h" // Provides node and the linked list functions using namespace std;

namespace main_savitch_5 {

bag::bag( ) // Library facilities used: cstdlib { head_ptr = NULL; many_nodes = 0; }

bag::bag(const bag& source) // Library facilities used: node1.h { node *tail_ptr; // Needed for argument of list_copy

list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; } bag::~bag( ) // Library facilities used: node1.h { list_clear(head_ptr); many_nodes = 0; }

bag::size_type bag::count(const value_type& target) const // Library facilities used: cstdlib, node1.h { size_type answer; const node *cursor; // Use const node* since we don't change the nodes. answer = 0; cursor = list_search(head_ptr, target); while (cursor != NULL) { // Each time that cursor is not NULL, we have another occurrence of // target, so we add one to answer, and move cursor to the next // occurrence of the target. ++answer; cursor = cursor->link( ); cursor = list_search(cursor, target); } return answer; }

bag::size_type bag::erase(const value_type& target) // Library facilities used: cstdlib, node1.h { size_type answer = 0; node *target_ptr;

target_ptr = list_search(head_ptr, target); while (target_ptr != NULL) { // Each time that target_ptr is not NULL, we have another occurrence // of target. We remove this target using the same technique that // was used in erase_one. target_ptr->set_data( head_ptr->data( ) ); target_ptr = target_ptr->link( ); target_ptr = list_search(target_ptr, target); list_head_remove(head_ptr); --many_nodes; ++answer; } return answer; } bool bag::erase_one(const value_type& target) // Library facilities used: cstdlib, node1.h { node *target_ptr; target_ptr = list_search(head_ptr, target); if (target_ptr == NULL) return false; // target isn't in the bag, so no work to do target_ptr->set_data( head_ptr->data( ) ); list_head_remove(head_ptr); --many_nodes; return true; }

bag::value_type bag::grab( ) const // Library facilities used: cassert, cstdlib, node1.h { size_type i; const node *cursor; // Use const node* since we don't change the nodes.

assert(size( ) > 0); i = (rand( ) % size( )) + 1; cursor = list_locate(head_ptr, i); return cursor->data( ); }

void bag::insert(const value_type& entry) // Library facilities used: node1.h { list_head_insert(head_ptr, entry); ++many_nodes; }

void bag::operator +=(const bag& addend) // Library facilities used: cstdlib, node1.h { node *copy_head_ptr; node *copy_tail_ptr; if (addend.many_nodes > 0) { list_copy(addend.head_ptr, copy_head_ptr, copy_tail_ptr); copy_tail_ptr->set_link( head_ptr ); head_ptr = copy_head_ptr; many_nodes += addend.many_nodes; } } void bag::operator =(const bag& source) // Library facilities used: node1.h { node *tail_ptr; // Needed for argument to list_copy

if (this == &source) return;

list_clear(head_ptr); many_nodes = 0; list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; }

bag operator +(const bag& b1, const bag& b2) { bag answer;

answer += b1; answer += b2; return answer; } void bag::operator -=(const bag& subtrahend) // Library facilities used: algorithm {

//code here } bag operator -(const bag& b1, const bag& b2){

//code here }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!