Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

See the files lbag.h and node.h below. In the lbag.h file, include the following member functions to overload += and == operators. The function signatures

See the files "lbag.h" and "node.h" below. In the lbag.h file, include the following member functions to overload += and == operators. The function signatures are provided below:

a. void operator +=(const LBag & other); In this function, other will be appended to the existing LBag object.

b. bool operator ==(const LBag & other); In this function, you will have to check if the elements of the existing LBag is same as that of other

Make the necessary changes, so that your main() should look like:

image text in transcribed

Files

lbag.h:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

node.h:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

int main() { LBag int> a; a. append(1); a.append(2); a.append(3); a.append(4); a.print(); // [1 2 3 4] LBag b(a); b.print(); // [1 2 3 4] cout C; C.append(5); C.append(6); C.append(7); c.append(8); c.print(); // [5 6 7 8] cout #include #include "node.h" template Eclass LBag { public: using value_type = T; using size_type = std::size_t; using iterator = Iterator; // pre: none // post: creates an empty lbag LBag(); // pre: none // post: creates an lbag that is a copy of given Ibag LBag(const LBag &); // copy constructor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 ~LBag(); void operator =(const LBag& other); size_type size() const; // finding the number of occurrence of the target size_type count(const T& target) const; // inserting an element at front void insert(const T& target); void append(const T& target); // inserting at end // printing the elements present in the LBag void print) const; void operator +=(const LBag &); bool find(const T& target); void find_replace(const T& target, const T& new_target); // removing one occurrence of the target bool erase_one(const T& target); // removing all occurrences of the target size_type erase(const T& target); 53 54 55 56 I for iterating over the elements of the bag II iterator begin(); iterator end(); private: Node* head_j }; // TODO template LBag operator +(const LBag& a, const LBag& b); template ELBag::LBag() { std::cout ELBag::-LBag() { std::cout Bag::LBag(const LBag & b) { Node* tail; list_copy(b.head_, head_, tail); } El overloading = operator to copy one bag into another // same as copy constructor template Evoid LBag::operator =(const LBag& other) { if (this == &other) return; Node* tail; list_clear(head_); list_copy (other.head_, head_, tail); template typename LBag::size_type LBag::size() const { return list_length(head_); } template stypename LBag::size_type LBag::count(const 1 & value) const { size_type ans = 0; E p->link() { for (Node* p = head_; p != nullptr; p = if (P->data() value) ++ans; == return ans; // inserting at front template Evoid LBag::insert(const 1 & value) { list_head_insert(head_, value); // inserting at the end template Evoid LBag::append(const T& target) { list_tail_insert(head_, target); template Evoid LBag::print() const { list_print(head); Il finding a specific element in the list template Ebool LBag::find(const T& target) { if (list_search(head_, target) != nullptr) { return true; 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 return false; // replacing the element found with the new value template Evoid LBag::find_replace(const T& target, const T& new_target) { Node* target_ptr list_search(head_, target); if (target_ptr == nullptr) return; // target isn't in the list, so no work to do target_ptr->set_data(new_target); // setting the new data template Ebool LBag::erase_one(const T& target) { Node* target_ptr list_search(head_, target); if (target_ptr nullptr) return false; // target isn't in the list, so no work to do target_ptr->set_data(head_ ->data()); list_head_remove (head_); return true; template typename LBag::size_type LBag::erase(const T& target) { typename LBag::size_type ans = 0; Node* target_ptr; target_ptr list_search(head_, target); -- while (target_ptr != nullptr) { // each time the target pointer is not a nullptr, we have another occurrence of the target // we remove this target using the same technique we used for erase_one target_ptr->set_data(head_->data()); target_ptr target_ptr->link(); target_ptr list_search(target_ptr,target); list_head_remove(head_); ++ans; return ans; IIII for iterating the elements of the LBag 166 167 168 169 179 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 template stypename LBag::iterator LBag::begin() { /eed typename for templated typedefs ONLY iterator i(head); return i; //return iterator(head) template typename LBag::iterator LBag::end(){ return nullptr; #endif /* lbag_h */ EV // node.hpp [// W9-linked-bag HNM0009 #ifndef node_h #define node_h #include #include #include template class Node { public: using value_type = 1; B // CONSTRUCTOR Node (const value_type& init_data, Nodes init_link) { data_ = init_data; link_ = init_link; // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_ = new_data; void set_link(Node* new_link) { link_ = new_link; 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 // Constant member function to retrieve the current data: value_type data() const { return data_; // Two slightly different member functions to retreive // the current link: const Node* link() const { return link_j Node* link() { return link_j private: value_type data_; Node* link_; //// Iterator class ////// template class Iterator { public: // constructor Iterator(Nodedata(); } - // returns THIS iterator, not a copy of it // this is the meaning of & Iterator& operator++() { // pre-increment current_ = current_->link(); return *this; Iterator operator++(int) { // post-increment Iterator temp = *this; current_ = current_->link(); return temp; } bool operator==(const Iterator other) const { return current_ == other.current_; } bool operator!=(const Iterator other) const { return current_ != other.current_; 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 184 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 private: Node* current_; [ }; MALTI FUNCTIONS for the linked list toolkit / 17 finding number of nodes in the list template size_t list_length(const Node* cursor; size_t answer; answer = @; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link ++answer; return answer; // adding at first template Avoid list_head_insert(Node*& head_ptr, const T& entry) { head_ptr = new Node(entry, head_ptr); 1/ insert after a node template avoid list_insert(Node *insert_ptr; insert_ptr = new Node(entry, previous_ptr->link(); previous_ptr->set_link(insert_ptr); // adding at last template Evoid list_tail_insert(Nodext>*& head_ptr, const T& entry) { Node(entry, nullptr); Node* current = head_ptr; if (current == nullptr) { list_head_insert(head_ptr, entry); } else { while (current->link() != nullptr) { current = current->link(); current->set_link (new_node); // removing the head template Evoid list_head_remove(Node*& head_ptr) { Node *remove_ptr; remove_ptr = head_ptr; head_ptr = head_ptr->link(); delete remove_ptr; 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 178 171 172 173 174 175 176 177 178 179 180 181 // removing node that is not the head template void list_remove(Node *remove_ptr; remove_ptr = previous_ptr->link(); previous_ptr->set_link( remove_ptr->link()); delete remove_ptr; // printing the nodes of the linked list template void list_print(Node* head_ptr) { std::cout * curr = head_ptr; curr != nullptr; cur curr = curr->link()) { std::cout data() Node* list_search(Nodext>* head_ptr, const T& target) { Node *cursor; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link( ) if (target == cursor->data() return cursor; return nullptr; // searching in the list 1/ you can either use this or the other search depending on how you want to implement your code //template 1/bool list_search(Node* head_ptr, const T& target) { Node *cursor; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link() if (target == cursor->data()) return true; return false; // searching in the list template const Node* head_ptr, const T& target) { const Node *cursor; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link) if (target == cursor->data() return cursor; return nullptr; 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 // Locating a particular node at a specified position template Node* head_ptr, size_t position) { Node *cursor; size_t i; assert (@ link(); return cursor; // Locating a particular node at a specified position template const Node* head_ptr, size_t position) { const Node *cursor; size_t i; assert (@ link(); return cursor; 233 // removing the node of the linked list template void list_clear(Node*& head_ptr) { while (head_ptr != nullptr) list_head_remove(head_ptr); 234 235 236 237 238 239 240 241 11 copying one linked into another template Evoid list_copy(const Node* source_ptr, Node*& head_ptr, Node*& tail_ptr) { head_ptr = nullptr; tail_ptr = nullptr; // Handle the case of the empty list. if (source_ptr == nullptr) return; // Make the head Node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data()); tail_ptr = head_ptr; 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 // Copy the rest of the Nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link(); while (source_ptr != nullptr) list_insert(tail_ptr, source_ptr->data()); tail_ptr = tail_ptr->link(); source_ptr = source_ptr->link(); #endif /* node_h */ int main() { LBag int> a; a. append(1); a.append(2); a.append(3); a.append(4); a.print(); // [1 2 3 4] LBag b(a); b.print(); // [1 2 3 4] cout C; C.append(5); C.append(6); C.append(7); c.append(8); c.print(); // [5 6 7 8] cout #include #include "node.h" template Eclass LBag { public: using value_type = T; using size_type = std::size_t; using iterator = Iterator; // pre: none // post: creates an empty lbag LBag(); // pre: none // post: creates an lbag that is a copy of given Ibag LBag(const LBag &); // copy constructor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 ~LBag(); void operator =(const LBag& other); size_type size() const; // finding the number of occurrence of the target size_type count(const T& target) const; // inserting an element at front void insert(const T& target); void append(const T& target); // inserting at end // printing the elements present in the LBag void print) const; void operator +=(const LBag &); bool find(const T& target); void find_replace(const T& target, const T& new_target); // removing one occurrence of the target bool erase_one(const T& target); // removing all occurrences of the target size_type erase(const T& target); 53 54 55 56 I for iterating over the elements of the bag II iterator begin(); iterator end(); private: Node* head_j }; // TODO template LBag operator +(const LBag& a, const LBag& b); template ELBag::LBag() { std::cout ELBag::-LBag() { std::cout Bag::LBag(const LBag & b) { Node* tail; list_copy(b.head_, head_, tail); } El overloading = operator to copy one bag into another // same as copy constructor template Evoid LBag::operator =(const LBag& other) { if (this == &other) return; Node* tail; list_clear(head_); list_copy (other.head_, head_, tail); template typename LBag::size_type LBag::size() const { return list_length(head_); } template stypename LBag::size_type LBag::count(const 1 & value) const { size_type ans = 0; E p->link() { for (Node* p = head_; p != nullptr; p = if (P->data() value) ++ans; == return ans; // inserting at front template Evoid LBag::insert(const 1 & value) { list_head_insert(head_, value); // inserting at the end template Evoid LBag::append(const T& target) { list_tail_insert(head_, target); template Evoid LBag::print() const { list_print(head); Il finding a specific element in the list template Ebool LBag::find(const T& target) { if (list_search(head_, target) != nullptr) { return true; 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 return false; // replacing the element found with the new value template Evoid LBag::find_replace(const T& target, const T& new_target) { Node* target_ptr list_search(head_, target); if (target_ptr == nullptr) return; // target isn't in the list, so no work to do target_ptr->set_data(new_target); // setting the new data template Ebool LBag::erase_one(const T& target) { Node* target_ptr list_search(head_, target); if (target_ptr nullptr) return false; // target isn't in the list, so no work to do target_ptr->set_data(head_ ->data()); list_head_remove (head_); return true; template typename LBag::size_type LBag::erase(const T& target) { typename LBag::size_type ans = 0; Node* target_ptr; target_ptr list_search(head_, target); -- while (target_ptr != nullptr) { // each time the target pointer is not a nullptr, we have another occurrence of the target // we remove this target using the same technique we used for erase_one target_ptr->set_data(head_->data()); target_ptr target_ptr->link(); target_ptr list_search(target_ptr,target); list_head_remove(head_); ++ans; return ans; IIII for iterating the elements of the LBag 166 167 168 169 179 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 template stypename LBag::iterator LBag::begin() { /eed typename for templated typedefs ONLY iterator i(head); return i; //return iterator(head) template typename LBag::iterator LBag::end(){ return nullptr; #endif /* lbag_h */ EV // node.hpp [// W9-linked-bag HNM0009 #ifndef node_h #define node_h #include #include #include template class Node { public: using value_type = 1; B // CONSTRUCTOR Node (const value_type& init_data, Nodes init_link) { data_ = init_data; link_ = init_link; // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_ = new_data; void set_link(Node* new_link) { link_ = new_link; 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 // Constant member function to retrieve the current data: value_type data() const { return data_; // Two slightly different member functions to retreive // the current link: const Node* link() const { return link_j Node* link() { return link_j private: value_type data_; Node* link_; //// Iterator class ////// template class Iterator { public: // constructor Iterator(Nodedata(); } - // returns THIS iterator, not a copy of it // this is the meaning of & Iterator& operator++() { // pre-increment current_ = current_->link(); return *this; Iterator operator++(int) { // post-increment Iterator temp = *this; current_ = current_->link(); return temp; } bool operator==(const Iterator other) const { return current_ == other.current_; } bool operator!=(const Iterator other) const { return current_ != other.current_; 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 184 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 private: Node* current_; [ }; MALTI FUNCTIONS for the linked list toolkit / 17 finding number of nodes in the list template size_t list_length(const Node* cursor; size_t answer; answer = @; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link ++answer; return answer; // adding at first template Avoid list_head_insert(Node*& head_ptr, const T& entry) { head_ptr = new Node(entry, head_ptr); 1/ insert after a node template avoid list_insert(Node *insert_ptr; insert_ptr = new Node(entry, previous_ptr->link(); previous_ptr->set_link(insert_ptr); // adding at last template Evoid list_tail_insert(Nodext>*& head_ptr, const T& entry) { Node(entry, nullptr); Node* current = head_ptr; if (current == nullptr) { list_head_insert(head_ptr, entry); } else { while (current->link() != nullptr) { current = current->link(); current->set_link (new_node); // removing the head template Evoid list_head_remove(Node*& head_ptr) { Node *remove_ptr; remove_ptr = head_ptr; head_ptr = head_ptr->link(); delete remove_ptr; 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 178 171 172 173 174 175 176 177 178 179 180 181 // removing node that is not the head template void list_remove(Node *remove_ptr; remove_ptr = previous_ptr->link(); previous_ptr->set_link( remove_ptr->link()); delete remove_ptr; // printing the nodes of the linked list template void list_print(Node* head_ptr) { std::cout * curr = head_ptr; curr != nullptr; cur curr = curr->link()) { std::cout data() Node* list_search(Nodext>* head_ptr, const T& target) { Node *cursor; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link( ) if (target == cursor->data() return cursor; return nullptr; // searching in the list 1/ you can either use this or the other search depending on how you want to implement your code //template 1/bool list_search(Node* head_ptr, const T& target) { Node *cursor; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link() if (target == cursor->data()) return true; return false; // searching in the list template const Node* head_ptr, const T& target) { const Node *cursor; for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link) if (target == cursor->data() return cursor; return nullptr; 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 // Locating a particular node at a specified position template Node* head_ptr, size_t position) { Node *cursor; size_t i; assert (@ link(); return cursor; // Locating a particular node at a specified position template const Node* head_ptr, size_t position) { const Node *cursor; size_t i; assert (@ link(); return cursor; 233 // removing the node of the linked list template void list_clear(Node*& head_ptr) { while (head_ptr != nullptr) list_head_remove(head_ptr); 234 235 236 237 238 239 240 241 11 copying one linked into another template Evoid list_copy(const Node* source_ptr, Node*& head_ptr, Node*& tail_ptr) { head_ptr = nullptr; tail_ptr = nullptr; // Handle the case of the empty list. if (source_ptr == nullptr) return; // Make the head Node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data()); tail_ptr = head_ptr; 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 // Copy the rest of the Nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link(); while (source_ptr != nullptr) list_insert(tail_ptr, source_ptr->data()); tail_ptr = tail_ptr->link(); source_ptr = source_ptr->link(); #endif /* node_h */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions