Question: need help with this c++ program please use linked lists do not use vectors The specialbag data type will be implemented as follows: Objects can

need help with this c++ program

please use linked lists do not use vectors

The specialbag data type will be implemented as follows:

Objects can be inserted into the magic bag using the SpecialBag::insert(item) member function. The specialbag can hold any number of items, Duplicate items are allowed.

Objects are removed from the magic bag using the SpecialBag::draw() member function. This function returns a random item and removes it from the bag. The returned item should be randomized. In other words, if there are 5 items in the bag, and only one of them is a 7, then there should be a 1 in 5 chance of drawing a 7 with the draw() function. If the bag is empty, the draw() function should throw an exception.

You can check if an object is in the bag using SpecialBag::peek(item), which should return 0 if there is no item in the bag matching the item parameter. If there are items matching the item parameter in the bag, the number (count) of matching items should be returned.

You can print a specialbag using the SpecialBag::print(ostream&) member function. This function should print to any ostream you pass to it (cout, cerr, etc.).

You should be able to assign the contents of a SpecialBag using the = operator. This should result in a copy of the magic bag that is not linked to the original bag. In other words, if a and b are magic bags, the line "a = b;" should result in bags a and b having the same contents. If I then draw elements from bag b, this should not change the contents of bag a.

You should begin by constructing a special bag that holds integers. For additional credit, you can create a template so that the special bag can hold any primitive data type (see below for grading details).

You can implement the magic bag using an array, a linked, list, or any other data structure that you feel is appropriate. You need to design and implement the underlying data structure yourself. You may not, for example, use C++ standard template library vectors for the underlying data structure.

You will be provided with a main program for testing your magic bag.

No points will be awarded for submissions that do not compile.

starting code *** SpecialBag.h****

#pragma once

#include

using namespace std;

template class SpecialBag

{

public:

SpecialBag() {

}

SpecialBag(const SpecialBag& other) {

}

SpecialBag& operator=(const SpecialBag& other) {

}

~SpecialBag() {

}

void insert(T item) {

}

T draw() {

}

int peek(T item) {

}

private:

friend ostream& operator<<(ostream& os, const SpecialBag& mb) {

}

};

*** end of SpecialBag.H**

test program****

#include "stdafx.h"

#include

#include "SpecialBag.h"

#define SB_CHECK_SIZE 5

#define NUM_MEM_TESTS 100000

using namespace std;

void memoryLeakTest();

int main() {

SpecialBag mb1;

//change this to a ridiculously large number to make sure it still works

for (int i = 0; i < SB_CHECK_SIZE; i++) {

sb1.insert(i);

sb1.insert(111);

}

cout << "Printing an integer bag:" << endl;

sb1.print(cout);

int d = sb1.draw();

cout << "What I drew from sb1:" << endl;

cout << d << endl << endl;

cout << "Should be missing " << d << endl;

sb1.print(cout);

SpecialBag mb3;

sb3 = mb1;

d = mb1.draw();

cout << "Should be missing " << d << endl;

sb1.print(cout);

cout << "sb3 Should not be missing " << d << endl;

sb3.print(cout);

cout << "Testing peek, how many 111s we got?" << endl;

cout << sb1.peek(111) << endl << endl;

cout << "Should not be missing ^" << endl;

sb1.print(cout);

cout << "Making a large character bag..." << endl;

SpecialBag mb4;

for (int i = 0; i < 1000; i++) {

sb4.insert('a');

}

cout << "Making a large bag of doubles..." << endl;

SpecialBag sb5;

for (int i = 0; i < 1000; i++) {

sb5.insert(1.0);

}

cout << "Start memory leak test:" << endl;

for (int i = 0; i < NUM_MEM_TESTS; i++) {

memoryLeakTest();

}

cout << "End memory leak test" << endl;

system("pause");

return 0;

}

void memoryLeakTest() {

SpecialBag mb7;

for (int i = 0; i < SB_CHECK_SIZE; i++){

sb7.insert(i);

}

SpecialBag mb9;

mb9 = mb7;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!