void bumpPlane (plane*& pstart)
{
int flight;
cout << " Enter the flight number you would like to move ahead" << endl;
cin >> flight;
plane* ptarget = NULL;
plane* pcurrent;
pcurrent = pstart;
plane* pbefore = NULL;
if(pstart == NULL)
{ // better to get used to using braces even if there is only one statement after the if condition
cout << "No planes on the queue" << endl;
return; // thought you might want to return if the list is empty
}
while (pcurrent != NULL)
{
if(pcurrent->flightnum == flight)
{
ptarget = pcurrent;
break; // thought you might want to break the loop if a match is found
}
else
{
pbefore = pcurrent;
pcurrent = pcurrent->pnext;
}
}
if (ptarget == NULL) // == not = ( this is one of the places where most newcomers get it wrong.
{
cout << "Could not find flight number in the queue" << endl;
return;
}
// there is no error in here. But is this what you want to do?
// At start
// ---->A-----> B------> C
plane* psave = NULL;
psave = pcurrent->pnext;
pcurrent->pnext = pbefore;
pbefore = psave;
// After the above code it becomes
// ---->A-------------->C
// |
// B
// You will not be able to reach B after this. Is that ok with your objective?
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Try this
plane* psave = NULL;
psave = pbefore2->pnext;
pbefore2->pnext = pbefore->pnext;
psave->pnext = ptarget->pnext;
ptarget->pnext = psave;
Correct the other mistakes I showed you in my previous reply and the one below.
if (ptarget = NULL)
{
cout << "Could not find flight number in the queue" << endl;
return;
cout << "ok inside next iff" << endl; //You wont reach this line also
}
Check what happens if the target is within the first 2 nodes of the list. Wont it crash in that case? Run it and see.
Also try to use the STL list next time if possible.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115