Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using The given .h and .cpp files add the given method in C++ language stated be

ID: 3738938 • Letter: U

Question

Using The given .h and .cpp files add the given method in C++ language stated below.

Add a findAndDelete method to the ItemList class. This method accepts a number that is to be deleted. It will return true if it can find the number in the list and delete it from the linked list and false otherwise. Show how this method is called and works. For example, if the list contains 100, 200 and 300, findAndDelete of 50 will return false and findAndDelete of 200, the list is left with 100 and 300.

Item.h

#ifndef ITEM_H

#define ITEM_H

class Item

{

private:

int m_num ;

Item *m_pNext ;

public:

Item(int num) ;

int getNum() ;

Item *getNext() ;

void setNext(Item *pItem) ;

};

#endif

item.cpp

#include <iostream>

#include "Item.h"

using namespace std;

Item::Item(int num) : m_num(num), m_pNext(nullptr)

{

}

int Item::getNum()

{

return m_num ;

}

Item * Item::getNext()

{

return m_pNext ;

}

void Item::setNext(Item *pItem)

{

m_pNext = pItem ;

}

ItemList.h

#ifndef ITEMLIST_H

#define ITEMLIST_H

#include <string>

#include "Item.h"

class ItemList

{

private:

Item *m_pHead;

public:

ItemList();

bool add(Item *pItem) ;

std::string toString() ;

} ;

#endif

Itemlist.cpp

#include "ItemList.h"

#include <string>

#include <sstream>

using namespace std;

ItemList::ItemList() : m_pHead(nullptr)

{

}

bool ItemList::add(Item *pItem)

{

if (m_pHead == nullptr)

m_pHead = pItem;

else

{

Item *pCurr = m_pHead;

while (pCurr->getNext() != nullptr)

pCurr = pCurr->getNext();

pCurr->setNext(pItem);

}

return true;

}

string ItemList::toString()

{

std::stringstream sout;

Item *pCurr = m_pHead;

while (pCurr != nullptr)

{

sout << pCurr->getNum() << " " ;

pCurr = pCurr->getNext();

}

sout << endl;

return sout.str();

}

Explanation / Answer

If you need any help, give me comment please...

Item.h

#ifndef ITEM_H

#define ITEM_H

class Item

{

private:

int m_num;

Item *m_pNext;

public:

Item(int num);

int getNum();

Item *getNext();

void setNext(Item *pItem);

};

#endif

Item.cpp

#include <iostream>

#include "Item.h"

using namespace std;

Item::Item(int num) : m_num(num), m_pNext(nullptr)

{

}

int Item::getNum()

{

return m_num;

}

Item *Item::getNext()

{

return m_pNext;

}

void Item::setNext(Item *pItem)

{

m_pNext = pItem;

}

ItemList.h

#ifndef ITEMLIST_H

#define ITEMLIST_H

#include <string>

#define nullptr NULL

#include "Item.cpp"

class ItemList

{

private:

Item *m_pHead;

public:

ItemList();

bool add(Item *pItem);

bool findAndDelete(int v);

std::string toString();

};

#endif

ItemList.cpp

#include "ItemList.h"

#include <string>

#include <sstream>

using namespace std;

ItemList::ItemList() : m_pHead(nullptr)

{

}

bool ItemList::add(Item *pItem)

{

if (m_pHead == nullptr)

m_pHead = pItem;

else

{

Item *pCurr = m_pHead;

while (pCurr->getNext() != nullptr)

pCurr = pCurr->getNext();

pCurr->setNext(pItem);

}

return true;

}

bool ItemList::findAndDelete(int v){

if (m_pHead == nullptr)

return false;

else

{

Item *pCurr = m_pHead, *prev = nullptr;

if(pCurr->getNum()==v){

m_pHead = pCurr->getNext();

delete pCurr;

}

else{

while (pCurr != nullptr){

if(pCurr->getNum()==v)

break;

prev = pCurr;

pCurr = pCurr->getNext();

}

if(pCurr==nullptr)

return false;

else{

prev->setNext(pCurr->getNext());

delete pCurr;

return true;

}

}

}

}

string ItemList::toString()

{

std::stringstream sout;

Item *pCurr = m_pHead;

while (pCurr != nullptr)

{

sout << pCurr->getNum() << " ";

pCurr = pCurr->getNext();

}

sout << endl;

return sout.str();

}

main.cpp

#include<iostream>

#include "ItemList.cpp"

using namespace std;

int main(){

ItemList *items = new ItemList();

items->add(new Item(100));

items->add(new Item(200));

items->add(new Item(300));

cout<<items->toString();

cout<<items->findAndDelete(50)<<endl;

cout<<items->findAndDelete(200)<<endl;

cout<<items->toString();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote