Answe the following question using c++ ignore the wrriten stuff 9. (10 points) C
ID: 3740303 • Letter: A
Question
Answe the following question using c++
ignore the wrriten stuff
9. (10 points) Consider the following design for an ordered list class. Implement the remove function (assume other functions are implemented). 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; petroOrt bol Mylist :: remove lint om { if(! 14_prent (1) * 3; return false; for (int i=0; iz cat ; i++) if(y-ac:)) temp '5). othered ( Larsen ) lift - { a[i] = a[•*-; a'Chua /- SZ -- 5-a?se -); } for (int i=0; je cse; jrt) } a): tcrt; };Explanation / Answer
/*** As other functions are provided so i have provided the implementation of remove method. By keeping in mind that csz represents the current array size of the MyList and cap represents the capacity of the myList ****/
/**** Algorithm: First we will check whether the element which we need to remove is present in the MyList or not. If it is not present then we will return false because we can not delete the element which is not present.
If the element is present then we need to remove it . First iterate over the complete array to find the index of that element . After finding the element we need to remove it and maintain the order of MyList as well. So after finding the index of that element we will iterate through the list from that index to last element of the list and overrite the curent element with the next element. By doing this the complete array is shifted to left by one index and that elements get removed. and finally reduce the size of the list by one.
***/
#include<iostream>
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();
};
bool MyList::remove(int element){
if(!is_present(element)){
return false;
}
for(int i=0; i<csz; i++){
if(a[i]==element){
for(int j = i; j<csz-1; j++){
a[j] = a[j+1];
}
csz--;
break;
}
}
return true;
}