I'm new to C++ and i'm trying to write a method to add a node to the end of a linked list but i'm getting an infinite loop and i'm not sure why.
#include "Car.h"
Car::Car()
{
this->pName = NULL;
this->pNext = NULL;
}
Car::Car(const char *name)
{
this->pName = name;
this->pNext = NULL;
}
#include "Garage.h"
Garage::Garage()
{
this->pHead = NULL;
}
void Garage::AddToFront(Car *r)
{
if (this->pHead != NULL)
{
Car *tmp = this->pHead;
this->pHead = r;
r->pNext = tmp;
}
this->pHead = r;
}
void Garage::Print(const char *string)
{
Trace::out("Garage(%s): \n",string);
Car tmp = *pHead;
while (tmp.pNext != NULL)
{
Trace::out("\tCar(%s)--->%s \n", tmp.pName, tmp.pNext->pName);
tmp = *tmp.pNext;
}
Trace::out("\tCar(%s)--->%s \n", tmp.pName, "(null)");
Trace::out("\n");
}
void Garage::Remove(Car *r)
{
Car *tmp = this->pHead;
Car *prev = NULL;
if (pHead == r)
{
pHead = r->pNext;
return;
}
while (tmp != NULL && tmp != r)
{
prev = tmp;
tmp = tmp->pNext;
}
prev->pNext = tmp->pNext;
}
void Garage::AddToEnd(Car *r)
{
if (pHead == NULL)
{
pHead = r;
return;
}
else
{
Car *tmp = pHead;
while (tmp->pNext != NULL)
{
tmp = tmp->pNext;
};
tmp->pNext = r;
return;
}
}
int main()
{
// -------------------------------------------------
// Do not change the main() function in any way
// -------------------------------------------------
// Output needs to match PDF exactly
//--------------------------------------------------
Car A("Jetta");
Car B("Accord");
Car C("Forte");
Car D("Outback");
Car E("Escape");
Garage G;
G.AddToFront(&A);
G.AddToFront(&B);
G.AddToFront(&C);
G.AddToFront(&D);
G.AddToFront(&E);
G.Print("Add to Front");
G.Remove(&A);
G.Remove(&C);
G.Remove(&E);
G.Print("Remove A,C,E");
G.AddToEnd(&A);
G.Print("After A");
G.AddToEnd(&C);
G.Print("After C");
G.AddToEnd(&E);
G.Print("Add to End");
I've tried debugging and printing my list but I can't figure out what to do. It looks like i've made my list circular but i'm not sure how I did this and how to fix it. I can post my header files too if that would be helpful to debugging this.
The linked list in the OP is a brittle structure that depends on the the allocation of
Carnodes in functionmain. Functions in structGaragedo not copyCarnodes when they are inserted into theGaragelist. Instead, theGaragelist simply stores pointers to theCarnodes that are allocated in functionmain.When you remove
Car* rfrom theGaragelist,r->pNextcontinues to point into theGaragelist.When that same car is added back at the end of the list, you create a loop in the
Garagelist.One way to fix both problems is to set
r->pNexttonullptrwhenCar* ris removed from theGaragelist.Here is my version of function
Remove. It calls helper functionFindPrev, which, in all but a few special cases, returns a pointer to the node that precedesCar* rin theGaragelist.FindPrevreturnsnullptrwhen:Garagelist is empty.Car* ris not found in theGaragelist.Car* ris found at the head of theGaragelist.risnullptr.As a precaution, function
AddToEndshould setr->pNexttonullptr, as well. My version includes a check to see whetherCar* risnullptr.Incidentally, function
AddToFrontcan be simplified. Once again, I have added a check to verify thatris notnullptr. You need that in all the functions that acceptras a parameter.Output: