Question Details Here is the question I am having trouble with. The question ask
ID: 3620914 • Letter: Q
Question
Question DetailsHere is the question I am having trouble with. The question asks me to modify an existing program that I list below.
(a) Develop a new function member called addItem() to add an item to the list. This function should keep all items in the List class in the ascending order after adding the new item. Note that several duplicated items are allowed in the List class. For the successful addition, the function should return true. If the function failed to add a new item, it should return false. The following presents the prototype of the function member.bool addItem(ElementType item);
(b) Develop a new function member called deleteItem() to remove an item from the list. If the item to be removed has duplications, remove all of them. For the successful deletion, your function should return true. Otherwise, it should return false.
bool deleteItem(ElementType item);
(c) Develop a new function member called findLast() that returns the position (= index) of a specified item in the array. If the item has duplications, the function should return the last index of them. If the item doesn’t exist, it should return -1.
int findLast(ElementType item);
The program has a header file, cpp program file and a cpp tester file, but I will just list the cpp program file which needs to be altered.
-------------------------------------------------------------------------------------------------------
#include
using namespace std;
#include "List.h"
//--- Definition of class constructor
List::List()
: mySize(0)
{}
//--- Definition of empty()
bool List::empty() const
{
return mySize == 0;
}
//--- Definition of display()
void List::display(ostream & out) const
{
for (int i = 0; i < mySize; i++)
out << myArray[i] << " ";
}
//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
aList.display(out);
return out;
}
//--- Definition of insert()
void List::insert(ElementType item, int pos)
{
if (mySize == CAPACITY)
{
cerr << "*** No space for list element -- terminating "
"execution *** ";
exit(1);
}
if (pos < 0 || pos > mySize)
{
cerr << "*** Illegal location to insert -- " << pos
<< ". List unchanged. *** ";
return;
}
// First shift array elements right to make room for item
for(int i = mySize; i > pos; i--)
myArray[i] = myArray[i - 1];
// Now insert item at position pos and increase list size
myArray[pos] = item;
mySize++;
}
//--- Definition of erase()
void List::erase(int pos)
{
if (mySize == 0)
{
cerr << "*** List is empty *** ";
return;
}
if (pos < 0 || pos >= mySize)
{
cerr << "Illegal location to delete -- " << pos
<< ". List unchanged. *** ";
return;
}
// Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myArray[i] = myArray[i + 1];
// Decrease list size
mySize--;
}
Explanation / Answer
/* modify an existing program that I list below. (a) Develop a new function member called addItem() to add an item to the list. This function should keep all items in the List class in the ascending order after adding the new item. Note that several duplicated items are allowed in the List class. For the successful addition, the function should return true. If the function failed to add a new item, it should return false. The following presents the prototype of the function member.bool addItem(ElementType item); (b) Develop a new function member called deleteItem() to remove an item from the list. If the item to be removed has duplications, remove all of them. For the successful deletion, your function should return true. Otherwise, it should return false. bool deleteItem(ElementType item); (c) Develop a new function member called findLast() that returns the position (= index) of a specified item in the array. If the item has duplications, the function should return the last index of them. If the item doesn’t exist, it should return -1. int findLast(ElementType item); The program has a header file, cpp program file and a cpp tester file, but I will just list the cpp program file which needs to be altered. ------------------------------------------------------------------------------------------------------ */ #include using namespace std; #include "List.h" //--- Definition of class constructor List::List() : mySize(0) {} //--- Definition of empty() bool List::empty() const { return mySize == 0; } //--- Definition of display() void List::display(ostream & out) const { for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.