Question: It won't let me post all of the code for you to be able to edit. Only images. Any suggestions? Please hurry! Use the prompt
It won't let me post all of the code for you to be able to edit. Only images. Any suggestions?
Please hurry! Use the prompt below and the files provided as well to do what is asked. Thank you!


Bag5.h




Bag5.template




Main.cpp



Node2.h







![Bag [1,2,3,4,5, 6, 7] x=2 prints 2 3 4 Bag [1,2,3,4,5,6,7] x=2](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66efb2e31cce8_93066efb2e27637d.jpg)
Node2.template
![prints 2 3 4 5 6 7 Bag [1,2,3,4,5,6,7] x=2 prints Bag](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66efb2e3ea80f_93166efb2e351a81.jpg)


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. 2 #de fine BAGSH - 4 /FILE: bag5.h (part of the namespace main_ savitch_chapter6) 5 // TEMPLATE CLASS PROVIDED: 6 1 bagcItem> (a collection of items each item may appear multiple times) 8 // TYPEDEFS for the bag- template class: 9 bagkItem>: :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 (xy) 15/1 16 // bag
- ::size type 17 //This is the data type of any variable that keeps track of how many items are in a bag 19// 20 / bagcItem>::itbrator and bag: :const iterator 21 1/ Forward iterators for a bag or a const bag. 23 CONSTRUCTOR for the bagcItem> class: 24 bag ( ) 2511 Postcondition: The bag is empty. 27 28 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) 31 // 32 bool erase one (const Item& target) 33// 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 return value indicates that one copy was removed: false indica nothing was removed. 35 tes that 38 void insert (const Item& entry) 39// Postcondition: A new copy of entry has been inserted into the bag. 41 // void operator +=(const.bag& addend) Postcondition: Each item in addend has been added to this bag 44// CONSTANT MEMBER FUNCTIONS for the bag
- class: 45 / size type count (const Item& target) const 46 Postcondition: Return value is number of times target is in the bag. 48 Item grab) const Precondition: size() > 0. Postcondition: The return value is a randomly selecte d item Erom the bag. 52 size type size() const 53// 54 7 Postcondition: Return value is the total number of items in the bag. 55 / STANDARD ITERATOR MEMBER FUNCTIONS (provide a forward iterator) 56// iterator begin) 57 const iterator begin() const 58 /1 iterator end ) 59 const iterator end const 61 // NONMEMBER FUNCTIONS for the bagcitem> class: 62 // templateclass Item bag
- operator +(const bag
- & bl, const bagsItem>& b2) Postcondition: The bag returned is the union of bl and b2 657 66 // VALUE SEMANTICS for the bag
- : 70I If there is insufficient dynamic memory, then the following functions throw bad alloc: The constructors, insert, operator +, operator t, and the 72 assignment operator 73 74 include // Provides NULL and size.t and NULL 75 #include "node2.h" // Provides node class 76 template 77 class bag 78 ( 79 public: 80 81 typedef std::size t size type: 82 83 typedef node iterator
- iterator: TYPEDEES typedef Item value type: typedef const node_iterator
- const iterator 85 86 87 1 CONSTRUCTORS and DESTRUCTOR bag() bag (const bag& source) -bag 89 90 91 92 93 94 95 // MODIFICATON MEMBER FUNCTIONS 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) 96 97 98 100 101 102 103 // CONST MEMBER FUNCTIONS size type count (const Items target) const Item grab( const: size_type size) const ( return many nodes) / FUNCTIONS TO PROVIDE ITERATORS 103 / FUNCTIONS TO PROVIDE ITERATORS 104 iterator begin( 105 106 const iterator begin) const 107 108 iterator end() 109 110 return iterator (head ptr):) t return const_iterator (head ptr): t return iterator(: / Uses default constructor const iterator end const return const iterator( Uses default constructor 112 113 private: 114 115 116 node
- thead ptr: size_type many_nodes // Head pointer for the list of items // Number of nodes on the list 118 / NONMEMBER functions for the bag 119 template 120 bag
- operator (const bag
- & bl, const bag
- & b2) 122 121 123 / The implementation of a template class must be included in its header fil 124 #include "bag5.template" 125 126 127 fendif // BAGS H 128 1 // FILE: bags.template 2 // CLASS mplemented: bag (see bags.h for documentation) II NOTE: 4 I Since bag is a template class, this file is included in node2.h 5 11 INVARIANT for the bag class: // 1. The items in the bag are stored on a linked st; 11 2. The head pointer of the list is stored in the member variable head ptr 8 3. The total number of items in the list is stored in the member variable 9 11 10 11 #includecassert> // Provides assert 12 #include stdlb> // Provides NULL, rand 13 #include-node2.h" // Provides node 14 15 16 template 17 bag
- : :bag) 18 Library facilities used: cstdlib many_nodes 19 20 21 head _ptr NULL many-nodes e; 23 24 template 25 bagcItem>::bag(const bagcItem& source) 26 I/ Library facilities used: node2.h 27 ( 28 node
- tail ptr: I/ Needed for argument of list copy 29 se 31 32 list copy (source.head_ptr, head ptr, tail ptr): many nodes source.many_nodes; 34 template 35 bagItem>:-bag() 36 Library facilities used: node2.h 37 list clear(head_ptr): many nodes e 38 39 40 41 42 template 43 typename bagcItem>::size type bagcItems::count (const Iten target) const 44 // Library facilities used: cstdlb, node2.h 45 46 47const nodecItem> "cursor; 48 49 5e 51 size_type answer: answer-8 cursor list search(head_ptr, target); uhile (cursor l= NULL) 52 while (cursor NULL) 51 52 53 /1 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 /I occurrence of the target. +ansiwer cursor cursor-link() cursor list-search(cursor, target); 56 57 58 59 return answer: 62 62 63 template : :size type bag
- : :erase(const Item& target) 65 1 Library facilities used: cstdlib, node2.h 67 68 69 70 size-type answer e; nodecItem> "target ptr: target-ptr list-search(head_ptr, while (target-ptr l= NULL) target); 72 73 74 75 76 /I Each time that target_ptr is not NULL, we have another occurrence /1 of target. We remove this target using the same technique that /I was used in erase one. ++answer -many-node s ; 78 79 80 target_ptr->set data head_ptr-data target-ptrtarget-ptr->link( ); target_ptr - list_search(target_ptr, target): list head remove(head_ptr): 82 83 return answer 85 86 template 87 bool bagcItem>: :erase_ one(const Item& target) 88 I Library facilities used: cstdlib, node2.h 89 8nodecItem> target ptr; 91 92 93 94 95 target_ptr-set data( head_ptr-data()) 96 97 98 target_ptr-list-search(head-ptr, target); if (target ptr NULL) return false; // target isn't in the bag, so no work to do list head_remove(head_ptr): --many_nodes; return true; 1ee 101 template: :grab const 0% 102 Item bagcitem>: :grabl const 103 / Library facilities used: cassert, cstdlib, node2.h 104 ( size type i; const nodecItem> "cursor 105 1e6 107 assert (size() > e); i . (std:: rand( ) % size( )) + 1; cursor return cursor->data( 188 109 11e list-locate(head-ptr, i); = 112 113 114 template 115 void bagcItem>: :insert(const Ite& entry) 116 // Library facilities used: node2.h list head insert(head_ptr, entry): ++many_nodes; 118 119 120 121 122 template 123 void bag<:operator bagcitem addend i library facilities used: node2h nodecitem> "copy_head _ptr: 127 128 129 138 131 132 133 134 135 136 ) 137 138 template 139 void bagcItem>: :operator -(const bageIte & source) 140 // Library facilities used: node2.h 141 142 nodecItem tail_ptr; I/ Needed for argument to list_copy 143 144if (this &source) 145 146 nodecItem "copy.tail ptr: if (addend.many_nodes > e) 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; return; 147 148 149 list_clear (head ptr); many-nodes e; 5elist_copy(source.head_ptr, head_ptr, tail_ptr); 151 man.nodes source.many-nodes; 152 ) 153 mplate X 53 54 template 55 bag
- & bi, const bag
- answer; answer +# b1; answer +# b2; return answer; 59 60 61 62 63 54 65 1 # include 3 #include 4 #include 11 void get_ items (bag
- & collection, SizeType n, MessageType description) 12// Postcondition: The string description has been written as a prompt to th 13 // screen. Then n items have been read from cin and added to the collection 14 // Library facilities used: iostream, bag4.h 15 ( 16 17 18 19 20 cout key after the final entry: " for (i l; i > user input; collection.insert (user_input) cout actors2: set result: 34 35 35 36 37 38 39 actors2.insert ("larry"); 40 actors2.insert ("curly"): 41 42 for (role actors1.begin() role- actors1.end): trole) set result; set::iterator role: actorsl.insert ("moe") actorsi.insert ("curly") 43 cout *head-ptr= new node start (head ptr) cout finish: node iterator position; 67 68 69 for (position-start; position! finish; ++position) 69 or (position start: position !-finish: ttposition) 70 71 72 cout bag of int: 76 bagcstring> bag of string 78 bag_of_int.insert (3) 79bag of string.insert ("hel1o") 80 bag of string.insert ("goodbye") 81 bag of string.insert ("auf wiedersehen") 82 bag of string.insert ("goodbye") 83 bag of string.insert ("he11o") 84 bag of atring.insert("goodbye) 85 86 cout :: iterator 92 93 94 95 return EXIT SUCCESS: 96 1 cursor bag-of-string , begin(); cursor ! bag-of-string . end(); ++cursor) - cout*cursor 8 // The node iterator is a forward iterators with two constructors: 9 1/ (1) A constructor (with a nodecItem parameter) that attaches the iterator 10// to the specified node in a linked list, and (2) a default constructor that 11// creates a special iterator that marks the position that is beyond the end of a 12 /7 linked list. There is also a const node iterator for use with 13 // const node
- * 15 TYPEDEF for the node
- template class: 16 Each node of the list contains a piece of data and a pointer to the 17 next node. The type of the data (nodecItem>: value type) is the Item type 18/ from the template parameter. The type may be any of the built-in CHt classes 19 int, char,..) or a class with a default const ructor, an assignment 20 operator, and' a test for equality ( y) 21 // NOTE: 22 / Many compilers require the use of the new keyword typename before using 23 / the expression node class: 28 node ( const Item& init data Item), node init_linkNULL 31 11) 32// Postcondition: The node contains the specified data and link. NOTE: The default value for the init data is obtained from the default constructor of the Item. In the ANSI/ISO standard, this notation is also allowed for the built-in types, providing a default value of Eor the init data is ob tained rom defauit 35 //is also allowed for the built-in types, providing a default value of zero. The init link has a default value of NULL 38// NOTE about two versions of some functions: 39The data function returns a reference to the data field of a node and 40 the link function returns a copy of the link field of a node. 41 / Each of these functions comes in two versions: a const version and a 42 non-const version. If the function is activated by a const node, then the 43 compiler choses the const version (and the return value is const) 44 If the function is activated by a non-const node, then the compiler choses 45 // the non-const version and the return value will be non-const). 46 // EXAMPLES: 47 const nodecint> *c 48 c-link() activates the const version of link returning const node* 49 a->data( activates the const version of data returning const Itema 50: //i ->data ( ) = 42; is forbidden 51nodeint> p 52 p->link) activates the non-const version of link returning node* 53 p-data) activates the non-const version of data returning Items 54. // p->data( ) 42; actually changes the data in p's node 56 // MEMBER FUNCTIONS for the node
- class: 57/ const Itemi data() const 77 void list clear (node
- 83 void list_copy 84 (const node
- 91 void 1ist head insert (node
- & head ptr) 97 // 98 9911Precondition: head ptr is the head pointer of a linked list, with at 100 // 101 Postcondition: The head node has been removed and returned to the heap 102head ptr is now the head pointer of the new, shorter linked list. least one node. 103 // 10411 template 105 void list insert (node
- previous ptr, const Item& entry) 106Precondition: previous ptr points to a node in a linked list 107// 108// 109// 110 // template 111 size t list length(const node
- * head ptr) Postcondition: A new node containing the given entry has been added after the node that previous ptr points to. Precondition: head ptr is the head pointer of a linked list. Postcondition: The value returned is the number of nodes in the linked list. 116 117 118 template NodePtr list_locate (NodePtr head _ptr, SizeType position) The NodePtr may be either node
- * or const node
- * 120// 121/ 122// 123// 124 // 125 // 126 // templateclass Item 127 void list_remove (node
- previous ptr) Precondition: head ptr is the head pointer of a linked list, and position>0. Postcondition: The return value is a pointer that points to the node at the specified position in the list. (The head node is position 1, the next node is position 2, and so on). If there is no such position, then the null pointer is returned. Precondition: previous ptr points to a node in a linked list, and this is not the tail node of the list. Postcondition: The node after previous ptr has been removed from the linked list. 129// 130 7/ 131 // 132 // 133 template t or const node
- + Precondition: head ptr is the head pointer of a linked list. Postcondition: The return value is a pointer that points to the first 138 // Postcondition: The return value is a pointer that points to the first node containing the specified target in its data member. If there is no such node, the null pointer is returned. 138 7/ 139 7/ 140 7/ 141 // 142 // DYNAMC MEMORY usage by the toolkit: 143If there is insufficient dynamic memory, then the following functions throw 144/ bad alloc: the constructor, 1ist head insert, list insert, list_copy. 145 146 #include // Provides NULL and size_t 147 tinclude Provides iterator and forward iterator tag 148 149 template 150 class node 151 152 public: 153 154 155 156 157 158 159 160 161 162 163 164 165 166 private: 167 168 169 170 // FUNCTIONS to manipulate a linked list: 171 template 172 void list_clear (nodecItem>*& head ptr) TYPEDEF typedef Item value_type: /CONSTRUCTOR node (const Items init_ data-Item, node init link-NULL) data field init data: link field init link: ) // MODIFCATION MEMBER FUNCTIONS Item& data() return data field: ) node link) return link field: ) void set_data (const Item& new_data) ( data field new data: void set_link (node new link) link field new link: ) // CONST MEMBER FUNCTIONS const Item& data() const (return data field: ) const node link( const return link field: ) Item data field: node link field 172 void list_clear (node
- t& head_ptr): 173 174 template 175 void list copy 176 (const nodecItem> source_ptr, node
- *& head ptr, node 179 void list_head_ insert (node
- void list head_remove (nodecItem>*s head ptr) 183 184 template 185 void list insert (node
- 188 std::size t list_length (const nodecItem>* head ptr) : 189 190 template 191 NodePtr list_locate (NodePtr head ptr, SizeType position) i 192 193 template 199 void 1ist remove (hodecttem* previous ptr) i 197 NodePtr list_search (NodePtr head ptr, const Items target) 200 // A node_iterator of can change the underlying linked list through the 203 // through the operator, so it may be used with a const node. 196 template 198 199 //FORWARD ITERATORS to step through the nodes of a linked list 201 // operator, so it may not be used with a const node. The 202 // node const iterator cannot change the underlying linked list 204 // WARNING: 205// This classes use std::iterator as its base class: 206 // older compilers that do not support the std::iterator class can 207 / / delete everything after the word iterator in the second line 208 209 template 210 class node iterator public atd::iterato 211 212 public: r<: forward iterator tag item> node_iterator (node
- data node_iterators operator/ Prefix ) current - current-link() return *this: node_iterator operator +(int) oER 223 224 225 226 227 228 229 230 231 232 node_iterator original (current) current current->link( return original: bool operator -(const node iterator other) conat ( return current other.current. bool operator !-(const riode iterator other) const ( return current other.current nodecItem> current: 233 private: 234 235 1 236 237 template 230 class const node iterator publie stdititeratorkstdi i forward iterator tag, const Item> 239 240 public: 241 const node iterator (const node
- initial NULL) current initial: ) 243 244 const Items operatorconst return current->data) const node_iterators operator )// Prefix+ 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 private: 262 const nodecItem+current: 263 264 265 current - current-link return *this: const_node_iterator operator +(int) Postfix + const node_iterator original (current): current current->link return original: bool operator (const const node_iterator other) const ( return current n other.current: bool operator !-(const const_node_ iterator other) const ( return current 1- other.current: 266 tinclude "node2.template" 267 268 269 fendif/ NODE2 H 270 1 / FILE: node2.template // MPLEMENTS: The functions of the node template class and the 3 II linked list toolkit (see node2.h for documentation). 5 1/ NOTE: 6 11 since node is a template class, this file is included in node2.h 711 Therefore, we should not put any using directives in this file. 9 // INVARIANT for the node class: 1e 1 The data of a node is stored in data_field, and the link in link_field. 12 include 11 Provides assert 13 #include // Provides NULL and size_t 14 15 16 template 17 void list clear(nodecItem>& head_ptr) 18 I Library facilities used: cstdlib 19 ( 20 while (head-ptr != NULL) 1 21 list head_remove(head_ptr); 23 24 template 25 void list_copy( 26 27 28 29) se / Library facilities used: cstdlib 31 32 const nodecItem source ptr nodecItem>&head ptr nodectem" tail-ptr head-ptr tail-ptr NULL; NULL; = 34 35 36 37 38 // Handle the case of the empty list if (source ptrNULL) return; 39 / Make the head node for the newly created list, and put data in it 40 list head_insert (head_ptr, source_ptr->data()) 41 42 43 tail ptr head_ptr // Copy rest of the nodes one at a time, adding at the tail of new list source_ptr source_ptr->link) while (source-ptr != NULL) 45 46 47 48 49 5e 51 52 list_insert(tail_ptr, source_ptr-data()): tail-ptr tail-ptr->link( ); source_ptrsource ptr-xlink 53 template 54 void list head insert(nodecItem>& head_ptr, const Item& entry) 56 57 58 59 template 6e void list head_remove(node
- & head_ptr) 61 ( 62 nodecItem> "remove_ptr; 63 head_ptr new nodecItem>(entry, head ptr) remove_ptr head_ptr 65 head ptr head_ptr-link delete remove_ptr; 67 68 69 template 7e void list insert(node
- insert_ptr: 73 74 75 76 insert-ptr new nodeltem> (entry, previous_ptr-link( previous_ptr->set link(insert ptr); )); 78 template cclass Item> . 79 std: :size_t list_length(const nodecItem>" head_ptr) 80 /I Library facilities used: cstdlib 81 82 const nodecItem "cursor; std::size_t answer 83 84 85 86 87 answer e; for (cursor . head-ptr; cursor != NULL; cursorcursor->link( )) ++answer; 89 90 91 return answer; 92 template link( ); = return cursor: 184 h 105 106 template kclass Item> 107 void list_remove(node
- * previous_ptr) 108 ( 109 118 nodecItem> remove_ptr; remove_ptr previous_ptr->link(); delete remove ptr 112 previous_ptr->set_link (remove_ptr->link( ): 113 114 ) 115 116 template 117 NodePtr list_search (NodePtr head_ptr, const Item& target) 118 I/ Library facilities used: cstdlib 119 f 120 121 122 for (cursor = head-ptr; cursor != NULL; cursor cursor->link( )) 123 124 125 126 127 128 129 130 NodePtr cursor; if (target == cursor->data( )) return cursor; return NULL