(C++ ) I\'m making a DietList with 7 DietNodes. I chose to do this in the DietLi
ID: 3915906 • Letter: #
Question
(C++ )
I'm making a DietList with 7 DietNodes. I chose to do this in the DietLists constructor (filling with necessary data later). However, in the debugger it seems pHead = pTail no matter the length of the list. (Note: pMem always points to the same address after each call, so I think the issue is that pMem goes out of scope, this is supported by the fact that the DietNode and DietPlan destructors are called after each exit from addToEnd()).
Below are pictures of some applicable code:
Class declaration:
DietList constructor:
?class Dietlist { ////////////linked list with diet plans public: DietlistO; void addToEnd(DietNode *pMem, DietNode **pHead, DietNode **pTail); DietNode getDietpHead); DietNode getDietpTail); private: DietNode *mpHead NULL; DietNode *mpTail = NULL; 1:Explanation / Answer
Since you are already have the head and tail of the list, you don't need to pass that to the addToEnd() function.
Update your code as follows (keep everything the way I have shown below...constructor, addToEnd() signature and implementation).
Hope it solved your issues. Please do rate the answer if it helped. Thank you.
.h file
-------
class DietList{
public:
DietList();
void addToEnd(DietNode *pMem);
DietNode *getDietpHead();
DietNode *getDietpTail();
private:
DietNode *mpHead = NULL;
DietNode *mpTail = NULL;
};
.cpp file
---------
//constructor
DietList::DietList()
{
for(int i = 0; i < 7; i++)
{
DietNode *mem = new DietNode;
addToEnd(mem);
}
}
//addToEnd()
void DietList::addToEnd(DietNode *pMem)
{
if(mpHead == NULL) //list empty?
{
mpHead = pMem;
mpTail = pMem;
}
else
{
mpTail->setpNext(pMem);
pMem->setpPrev(mpTail);
mpTail = pMem;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.