//HW4: Implement a generic bag using template //Due: April 1 (Sunday) at 11:59pm //Note that for class linked_list and bag, you need to also include destructor, copy constructor, move constructor, left ref operator=, and right ref operator=
#include #include #include using namespace std;
template class ThreeD { public: T ht; T wid; T dep; ThreeD() { ht = wid = dep = 0; } ThreeD(T i) { ht = wid = dep = i; } ThreeD(T a, T b, T c) { ht = a; wid = b; dep = c; } //Add additional code if needed };
template class node { public: T value; node * next; node * previous; node() { next = previous = nullptr; } node(T v) { value = v; next = previous = nullptr; }
//Add additional code if needed
}; template class linked_list { //doubly linked list as in HW2 public: //You need to implement this part. };
template class item { public: X value; item *next; item *previous; item(X v) { value = v; next = nullptr; previous = nullptr; } };
template class bag { public: item *last; item *first; int num_items; int size() { return num_items; } bag() { last = nullptr; first = nullptr; num_items = 0; } //default constructor for bag class void push_back(X v); //insert an item with value v to the back of the bag void push_front(X v); //insert an item with value v to the front of the bag void pop_back(); //delete the last item in the bag void pop_front(); //delete the first item in the bag X &operator[](int i); //Access bag item with index. X front() { //it returns the value of the first item in the bag. //In real applicaitons, we need to check if the bag is empty. We are skipping this here. return first->value; } X back() { //it returns the value of the last item in the bag //In real applicaitons, we need to check if the bag is empty. We are skipping this here.
return last->value; } void clear(); //Delete all items in the bag item * find(X x);//find returns address of item with value equal to x or nullptr if match not found void erase(item * p); //erase the item pointed by p; void erase(int i); //erase the item with index equal to i;
//You also need to implement cout < }; //your program needs to support simple data type (int, double, string), template-based doubly linked list (as in HW2), template-based ThreeD, vector, list, map, etc.
int main() { bag bag_d; bag_d.push_back(5.5); bag_d.push_back(6.6); bag_d.push_front(4.4); bag_d.push_front(3.3); bag_d.pop_front(); bag_d.pop_back(); cout << bag_d << endl;;
bag bag_i; bag_i.push_back(5); bag_i.push_back(6); bag_i.push_front(4); bag_i.push_front(3); bag_i.pop_front(); bag_i.pop_back(); cout << bag_i < ThreeD td3(3), td4(4), td5(5), td6(6), td7(100, 200, 300); bag> bag_3D; bag_3D.push_back(td5); bag_3D.push_back(td6); bag_3D.push_front(td4); bag_3D.push_front(td3); bag_3D.pop_front(); bag_3D.pop_back(); cout << bag_3D << endl;; cout << bag_3D.front() << bag_3D.back(); cout << bag_3D[0] << " " << bag_3D[1] << endl; bag_3D[1] = td7; cout << bag_3D[0] << " " << bag_3D[1] << endl; bag_3D.clear(); cout << bag_3D << endl;; cout << bag_3D.size() < linked_listls_1; ls_1.push_front("David"); ls_1.push_front("John"); ls_1.push_front("Pat"); ls_1.push_front("Ben"); ls_1.push_front("Jeff"); cout << ls_1 << endl;
linked_listls_2; ls_2.push_front("Wendy"); ls_2.push_front("Mary"); ls_2.push_front("Nancy"); ls_2.push_front("Jennifer"); cout << ls_2 << endl; bag> bag_string; bag_string.push_back(ls_1); bag_string.push_back(ls_2);
cout << bag_string << endl; cout << bag_string[1] << endl; ThreeD t10(3.2, 7.4, 8.9), t11(5.6, 7.7, 2.987), t12(4.6, 7.5, 3.1416), t13(55.6, 66.8, 333.45); linked_list> LTD1; LTD1.push_front(t10);
LTD1.push_front(t11); linked_list> LTD2; LTD2.push_front(t13); LTD2.push_front(t12); LTD2.push_front(t10); LTD2.push_front(t11);
bag > > BLTD; BLTD.push_back(LTD1); BLTD.push_back(LTD2); cout << BLTD < item>> * p2;
p2 = BLTD.find(LTD1); BLTD.erase(p2); cout << BLTD << endl; BLTD.push_back(LTD1); cout << BLTD << endl; BLTD.erase(0); cout << BLTD << endl;
getchar(); getchar(); return 0;
}