Question
In C++... please show that it works //Mylist class is below #include #include using namespace std; class MyList { private: static const int MAX=10; int
In C++... please show that it works
//Mylist class is below
#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) const;
void display() const;
};
MyList::MyList()
{
cap=MAX;
csz=0;
}
MyList::MyList(int ucap)
{
if (ucap>MAX || ucap
cap=MAX;
else
cap=ucap;
csz=0;
}
MyList::~MyList()
{
cout
cout
}
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) const { for (int i=0;i if (a[i]==val) return true; return false; } void MyList::display() const { cout cout cout for (int i=0;i cout cout } int main() { MyList l1; MyList l2(5); for (int i=0;i { l1.add(i); l2.add(i); } l1.display(); l2.display(); for (int i=0;i { l1.remove(i); l2.remove(i); } cout l1.display(); l2.display(); }
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