Please Hurry! Use the prompt and file below to complete this. Thank you
bag5.h
#ifndef BAG5_H #define BAG5_H
// FILE: bag5.h (part of the namespace main_savitch_chapter6) // TEMPLATE CLASS PROVIDED: // bag- (a collection of items; each item may appear multiple times) // TYPEDEFS for the bag
- template class: // bag
- ::value_type // This is the Item type from the template parameter. // It is the data type of the items in the bag. It may be any of the C++ built-in types (int, char, etc.), or a class with a default constructor, a copy constructor, an assignment operator, and a test for equality (x == y). // bag
- ::size_type // This is the data type of any variable that keeps track of how many items are in a bag, bag
- ::iterator and bag
- ::const_iterator // Forward iterators for a bag or a const bag. // CONSTRUCTOR for the bag
- class: // bag( ) // Postcondition: The bag is empty. // MODIFICATION MEMBER FUNCTIONS for the bag
- class: // size_type erase(const Item& target) // Postcondition: All copies of target have been removed from the bag. // The return value is the number of copies removed (which could be zero). // bool erase_one(const Item& target) // Postcondition: If target was in the bag, then one copy of target has // been removed from the bag; otherwise the bag is unchanged. A true // return value indicates that one copy was removed; false indicates that // nothing was removed. // void insert(const Item& entry) // Postcondition: A new copy of entry has been inserted into the bag. // void operator +=(const bag& addend) // Postcondition: Each item in addend has been added to this bag. // CONSTANT MEMBER FUNCTIONS for the bag
- class: // size_type count(const Item& target) const // Postcondition: Return value is number of times target is in the bag. // Item grab( ) const // Precondition: size( ) > 0. // Postcondition: The return value is a randomly selected item from the bag. // size_type size( ) const // Postcondition: Return value is the total number of items in the bag. // STANDARD ITERATOR MEMBER FUNCTIONS (provide a forward iterator): // iterator begin( ) // const_iterator begin( ) const // iterator end( ) // const iterator end( ) const // NONMEMBER FUNCTIONS for the bag
- class: // template // bag
- operator +(const bag
- & b1, const bag
- & b2) // Postcondition: The bag returned is the union of b1 and b2. // VALUE SEMANTICS for the bag
- class: // Assignments and the copy constructor may be used with bag objects. // DYNAMIC MEMORY USAGE by the bag
- : // If there is insufficient dynamic memory, then the following functions throw // bad_alloc: The constructors, insert, operator +=, operator +, and the // assignment operator. #include // Provides NULL and size_t and NULL #include "node2.h" // Provides node class template class bag { public: // TYPEDEFS typedef std::size_t size_type; typedef Item value_type; typedef node_iterator
- iterator; typedef const_node_iterator
- const_iterator; bag( ); bag(const bag& source); ~bag( ); size_type erase(const Item& target); bool erase_one(const Item& target); void insert(const Item& entry); void operator +=(const bag& addend); void operator =(const bag& source); size_type count(const Item& target) const; Item grab( ) const; size_type size( ) const { return many_nodes; } iterator begin( ) { return iterator(head_ptr); } const_iterator begin( ) const { return const_iterator(head_ptr); } iterator end( ) { return iterator( ); } // Uses default constructor const_iterator end( ) const { return const_iterator( ); } // Uses default constructor
private: node- *head_ptr; // Head pointer for the list of items
size_type many_nodes; // Number of nodes on the list };
// NONMEMBER functions for the bag template bag- operator +(const bag
- & b1, const bag
- & b2); // The implementation of a template class must be included in its header file: #include "bag5.template" #endif // BAG5_H main.cpp
#include #include #include #include #include "node2.h" #include "bag5.h"
using namespace std;
// PROTOTYPE for a function used by this demonstration program template void get_items(bag- & collection, SizeType n, MessageType description) // Postcondition: The string description has been written as a prompt to the // screen. Then n items have been read from cin and added to the collection. // Library facilities used: iostream, bag4.h { Item user_input; // An item typed by the program's user SizeType i;
cout key after the final entry: "; for (i = 1; i > user_input; collection.insert(user_input); } cout
int main() { //demostrate how to use set template class set actors1; set actors2; set result; set::iterator role; actors1.insert("moe"); actors1.insert("curly"); actors2.insert("larry"); actors2.insert("curly"); for(role = actors1.begin(); role != actors1.end(); ++role) cout *head_ptr = new node(); list_head_insert(head_ptr, 42); list_head_insert(head_ptr, 13); list_head_insert(head_ptr, 67); node_iterator start(head_ptr); node_iterator finish; node_iterator position; for(position = start; position != finish; ++position) cout bag_of_int; bag bag_of_string;
bag_of_int.insert(3); bag_of_string.insert("hello"); bag_of_string.insert("goodbye"); bag_of_string.insert("auf wiedersehen");
bag_of_string.insert("goodbye"); bag_of_string.insert("hello");
bag_of_string.insert("goodbye");
cout ::iterator cursor = bag_of_string.begin(); cursor != bag_of_string.end(); ++cursor) coutbag5.template
// CLASS implemented: bag (see bag5.h for documentation) // Since bag is a template class, this file is included in node2.h. // INVARIANT for the bag class: // 1. The items in the bag are stored on a linked list; // 2. The head pointer of the list is stored in the member variable head_ptr; // 3. The total number of items in the list is stored in the member variable // many_nodes.
#include #include #include "node2.h"
template bag- ::bag( ) // Library facilities used: cstdlib { head_ptr = NULL; many_nodes = 0; } template bag
- ::bag(const bag
- & source) // Library facilities used: node2.h { node
- *tail_ptr; // Needed for argument of list_copy
list_copy(source.head_ptr, head_ptr, tail_ptr); many_nodes = source.many_nodes; } template bag- ::~bag( ) // Library facilities used: node2.h { list_clear(head_ptr);
many_nodes = 0; } template typename bag- ::size_type bag
- ::count(const Item& target) const // Library facilities used: cstdlib, node2.h { size_type answer; const node
- *cursor; answer = 0; cursor = list_search(head_ptr, target); while (cursor != NULL) { ++answer; cursor = cursor->link( ); cursor = list_search(cursor, target); } return answer; }
template typename bag- ::size_type bag
- ::erase(const Item& target) // Library facilities used: cstdlib, node2.h { size_type answer = 0; node
- *target_ptr;
target_ptr = list_search(head_ptr, target); while (target_ptr != NULL) {
++answer; --many_nodes; target_ptr->set_data( head_ptr->data( ) ); target_ptr = target_ptr->link( ); target_ptr = list_search(target_ptr, target); list_head_remove(head_ptr); } return answer; } template bool bag- ::erase_one(const Item& target) // Library facilities used: cstdlib, node2.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; } template Item bag- ::grab( ) const // Library facilities used: cassert, cstdlib, node2.h { size_type i; const node
- *cursor;
assert(size( ) > 0); i = (std::rand( ) % size( )) + 1; cursor = list_locate(head_ptr, i);
return cursor->data( ); } template void bag- ::insert(const Item& entry) // Library facilities used: node2.h { list_head_insert(head_ptr, entry); ++many_nodes; } template void bag
- ::operator +=(const bag
- & addend) // Library facilities used: node2.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; } } template void bag
- ::operator =(const bag
- & source) // Library facilities used: node2.h { node
- *tail_ptr;
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; }
template bag- operator +(const bag
- & b1, const bag
- & b2){ bag
- answer;
answer += b1;
answer += b2;
return answer; }
Objectives The purpose of this assignment is to reinforce linked-list version of the bag template class with an iterator in C++. You need to use HW6_Templates zipped files posted on the course website Requirements 1. Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be cornpared for equality using ==. Use the following header for the function: void print_value range (const Item&x, const Item& y) print_value_range can be interpreted in a number of ways, but use the following points. This should make the implementation a little easier. Print the Items from x to y including the start but not including the end. .If there is no element in the bag that has value x, print nothing Otherwise, if there is no element in the bag, after x, that has the value y, then print from x to the end of the list Print the values on one line separated by space. Put an end of line after the values are all printed. Here are some examples: Bag [1,2,3,4,5, 6, 7] x=2 prints 2 3 4 Bag [1,2,3,4,5,6,7] x=2 prints 2 3 4 5 6 7 Bag [1,2,3,4,5,6,7] x=2 prints Bag [1,2,3,4,5,6,7) prints (nothing) 2 3 45 6 7 2. Remove repetitions Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using. Use the following header for the function: void remove_ repetitions() B da ent/25816925/View?ou 1690440 2. Remove repetitions Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using- Use the following header for the function: void remove_repetitions () Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag e If the next Item has data equal to the data in p, remove the next Item o o Otherwise move q to the next Item in the bag 3. Write test program to test the above two member functions