Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//Implement functions of class linked_list and class bag //All member functions listed in the two class needs implementation //note that some are already done //Need

//Implement functions of class linked_list and class bag

//All member functions listed in the two class needs implementation

//note that some are already done

//Need to implement the overloaded operator<< for all classes, including vector

//See the sample output for formats when implementing operator<< for all classes

#include

#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; }

bool operator==(ThreeD &t) { return (ht == t.ht && wid = t.wid && dep == t.dep); }

bool operator!=(ThreeD &t) { return (ht != t.ht || wid != t.wid || dep != t.dep); }

template friend ostream & operator<<(ostream &s, const ThreeD &t);

};

template class node {

public:

T value;

node * next;

node * previous;

node() { next = nullptr; previous = nullptr; }

node(T v) { value = v; next = nullptr; previous = nullptr; }

};

template class linked_list {

public:

int num_nodes;

node * head;10/15

node * tail;

linked_list() { num_nodes = 0; head = tail = nullptr; }

linked_list(const initializer_list &V);

void push_front(T t);

void push_back(T t);

bool operator==(linked_list &L);

linked_list(const linked_list &L);

~linked_list();

void operator=(const linked_list &L);

template friend ostream & operator<<(ostream &s, const linked_list &L);

};

template void linked_list::push_front(T t) {

node * p = new node(t);

if (head == nullptr) { head = tail = p; num_nodes++; }

else {

p->next = head;

head->previous = p;

head = p;

num_nodes++;

}

}

template void linked_list::push_back(T t)

{

node * p = new node(t);

if (head == nullptr) { head = tail = p; num_nodes++; }

else {

p->previous = tail;

tail->next = p;

tail = p;

num_nodes++;

}

}

template

class item {

public:

X value;

item *next;

item *previous;

item(X v) { value = v; next = nullptr; previous = nullptr; }

bool operator==(item I) { return value == I.value; }

template friend ostream & operator<<(ostream &s, const item &I);

};

template

class bag {

public:

item *last;

item *first;

int num_items;

int size() { return num_items; }

//void show_bag();//Print all items in the bag

bag() { last = nullptr; first = nullptr; num_items = 0; } //default constructor for bag class

bag(const initializer_list &V);

~bag();//destructor

bag(const bag & B);//copy constructor

void operator=(const bag & B); //operator= ; left value reference

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.

//if (num_items == 0) cout << "Error! The bag is empty! ";

//else

return first->value;

}

X back() { //it returns the value of the last item in the bag

/*

if (num_items == 0)

{

cout << "Error! The bag is empty! ";

}

else

*/

return last->value;

}

void clear(); //Delete all items in the bag

item * find(X I);

void erase(int index);

void erase(item * p);

template friend ostream & operator<<(ostream &s, const bag &B);

};

template void bag::push_front(X v) {

item *p;

p = new item(v);

if (num_items == 0) { first = last = p; }

else {

first->previous = p;

p->next = first;

first = p;

}

num_items++;

}

template void bag::push_back(X v) {

item *p = new item < X >(v);

if (num_items == 0) { first = last = p; }

else {

p->previous = last;

last->next = p;

last = p;

}

num_items++;

}

template void bag::pop_back() {

item *p;

if (num_items == 0) cout << "Error! The bag is empty! ";

else {

p = last->previous;

delete last;

last = p;

num_items--;

if (num_items == 0) first = nullptr;

else last->next = nullptr;

}

}

template void bag::pop_front() {

item *p;

if (num_items == 0) { return; }

else {

p = first->next;

delete first;

first = p;

num_items--;

if (num_items == 0) last = nullptr;

else first->previous = nullptr;

}

}

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 << endl;

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() << endl;

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 << endl;

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;

vector > V1 = { { 1,2,3 },{ 4,5,6 },{ 7,8,9 } };

cout << V1 << endl;

//bag> V2 = { {1,2,3}, {4,5,6}, {7,8,9} };

vector>> V2 = { { { 1,2,3 },{ 4,5,6 },{ 7,8,9 } },{ { 20,30,40 },{ 11,22, 33 } } };

cout << V2 << endl;

vector>> V3 = { { { 1, 2, 3 },{ 4, 5 } },{ { 6,7 },{ 8, 9, 10 } } };

cout << V3 << endl;

bag B10 = { 1,2,3,4,5 };

bag B11 = B10, B12;

B12 = B10;

cout << B10 << endl;

B10.first->value = 1000;

cout << B10 << endl;

cout << B11 << endl;

cout << B12 << endl;

bag>>> B13 = { { { { 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 } } } };

cout << B13 << endl;

bag>>> B14 = { { { { 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 } } } };

cout << B14 << endl;

bag> * p10 = new bag>({ { 1,2,3,4 },{ 5,6,7 } });

cout << *p10 << endl;

delete p10;

bag>>> B15 = { { { { 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 } } } };

cout << B15 << endl;

B15.erase(1);

cout << B15 << endl;

getchar();

getchar();

return 0;

}

//The following is a screenshot of a sample output

/*

4.4 5.5

4 5

( 4, 4, 4 ) ( 5, 5, 5 )

( 4, 4, 4 )( 5, 5, 5 )( 4, 4, 4 ) ( 5, 5, 5 )

( 4, 4, 4 ) ( 100, 200, 300 )

0

Jeff Ben Pat John David

Jennifer Nancy Mary Wendy

Jeff Ben Pat John David Jennifer Nancy Mary Wendy

Jennifer Nancy Mary Wendy

( 5.6, 7.7, 2.987 ) ( 3.2, 7.4, 8.9 ) ( 5.6, 7.7, 2.987 ) ( 3.2, 7.4, 8.9 ) ( 4.6, 7.5, 3.1416 ) ( 55.6, 66.8, 333.45 )

( 5.6, 7.7, 2.987 ) ( 3.2, 7.4, 8.9 ) ( 4.6, 7.5, 3.1416 ) ( 55.6, 66.8, 333.45 )

( 5.6, 7.7, 2.987 ) ( 3.2, 7.4, 8.9 ) ( 4.6, 7.5, 3.1416 ) ( 55.6, 66.8, 333.45 ) ( 5.6, 7.7, 2.987 ) ( 3.2, 7.4, 8.9 )

( 5.6, 7.7, 2.987 ) ( 3.2, 7.4, 8.9 )

[( 1, 2, 3 ), ( 4, 5, 6 ), ( 7, 8, 9 )]

[( 1, 2, 3 ) ( 4, 5, 6 ) ( 7, 8, 9 ) , ( 20, 30, 40 ) ( 11, 22, 33 ) ]

[1 2 3 4 5 , 6 7 8 9 10 ]

1 2 3 4 5

1000 2 3 4 5

1 2 3 4 5

1 2 3 4 5

[( 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 ) ]

( 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 )

1 2 3 4 5 6 7

[( 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 ) ]

[( 1, 2, 3 ) ( 4, 5, 6 ) , ( 7, 8, 9 ) ( 10, 11, 12 ) ( 13, 14, 15 ) ]

*/

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

Students also viewed these Databases questions

Question

1. How might volunteering help the employer and the employee?

Answered: 1 week ago