Question
For C++ and please show it works. What is required is commented out (//): #include #include using namespace std; class MyList { private: static const
For C++ and please show it works. What is required is commented out (//):
#include
#include
using namespace std;
class MyList
{
private:
static const int MAX=10;
int a[MAX];
int csz;
int cap;
public:
MyList();
MyList(int);
~MyList();
bool add(int);
bool remove(int);
bool is_present(int);
void display();
//////////////
//Implement the following functions.
//You may have to change this class design
//slightly (e.g., add new class function and/or class data members)
//in order to implement some of these functions.
//(1) 10 points
bool add_light(int val);
//The add_light function is a variation of the add function that
//will not accept duplicates. Returns false if the
//input (val) is a duplicate in the list
//or if the list is full, true otherwise.
//(2) 15 points
bool remove_safe(int val);
//The remove_safe operation behaves the following way:
//when an element (val) is removed,
//it is removed as usual from the list but
//is also copied to a "trash can" (think Recycle Bin),
//instead of forgetting it completely.
//Returns false if the trash can is full
//or if the input is not in the list, true otherwise.
//(3) 15 points
bool restore(int val);
//This function restores the input element (val)
//from the trash can back to the list.
//Returns false if the element is not present
//in the trash can or if the list is full,
//true otherwise.
//(4) 10 points
int display_count() const;
//This functions returns the number of
//times the display() function was called
//on this list object.
};
MyList::MyList()
{
cap=MAX;
csz=0;
}
MyList::MyList(int ucap)
{
if (ucap>MAX || ucap<=0)
cap=MAX;
else
cap=ucap;
csz=0;
}
MyList::~MyList()
{
cout << "Destroying a list object. ";
cout << "Done. ";
}
bool MyList::add(int val)
{
if (csz==cap)
return false;
a[csz]=val;
csz++;
return true;
}
bool MyList::remove(int val)
{
if (!is_present(val))
return false;
int found=0;
for (int i=0;i if (a[i]==val) found=i; a[found]=a[csz-1]; csz--; } bool MyList::is_present(int val) { for (int i=0;i if (a[i]==val) return true; return false; } void MyList::display() { cout << "Capacity: " << cap << endl; cout << "Current number of elements: " << csz << endl; cout << "Contents: "; for (int i=0;i cout << a[i] << " "; cout << endl; } int main() { //Test (1) add_light MyList l1(20); //capacity 20 for (int i=0;i<10;i++) l1.add_light(i); for (int i=0;i<10;i++) l1.add_light(i); l1.display(); //should print 0 to 9 //Test (2) remove_safe for (int i=0;i<5;i++) l1.remove_safe(i); l1.display(); //should print 5 to 9 //Test (3) restore for (int i=0;i<5;i++) l1.restore(i); l1.display(); //should print 0 to 9 //Test (4) display_count l1.display(); cout << l1.display_count() << endl; //should print 4 }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started