I have a simple plane object with public datamembers of a flight number and a pointer. I am successfully adding "planes" and deleting "planes," however when I try to move one up a spot, though it compiles well, I seem to get memory crashes.

Here is my bump function that passes the lead pointer for my linked list. I put cout statements in the function to see if i could pin point where it was crashing and it seems to be in my while loop. I'm sort of a newb really so im sure im doing something fairly obvious that is incorrect.

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)
		cout << "No planes on the queue" << endl;


	while (pcurrent != NULL)
	{
		if(pcurrent->flightnum == flight)
			ptarget = pcurrent;
			
		else
		{
			pbefore = pcurrent;
			pcurrent = pcurrent->pnext;
		
		}
	}
	if (ptarget = NULL)
	{
		cout << "Could not find flight number in the queue" << endl;
		return;
	}

	plane* psave = NULL;
	
	psave = pcurrent->pnext;
	
	pcurrent->pnext = pbefore;
	
	pbefore = psave;
	
}

ps. My first post here, though im a lurker and like to browse around here to pump me up to do my homework.

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? 

}

[edit] - I think instead of swirling around a ton of pointers im just going to swap the data members in the objects not the objects themselves.

Actualy, losing 'b' is exactly my problem. What I would like it to do if for excample 'c' was the target, would be.

a--->c--->b.

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 = pstart;
	plane* pbefore2 = pstart;
	plane* pafter = pstart;
	
	if(pstart == NULL)
		cout << "No planes on the queue" << endl;

	
	while (pcurrent != NULL)
	{
		if(pcurrent->flightnum == flight)
		{	
			ptarget = pcurrent;
			pafter = pcurrent->pnext;
			cout << "target found" << endl;

			pcurrent = NULL;
		}
		else
		{
			pbefore2 = pbefore;
			pbefore = pcurrent;
			pcurrent = pcurrent->pnext;
			cout << "target not found, moving pointers"<< endl;




		}
	}
	if (ptarget = NULL)
	{
		cout << "Could not find flight number in the queue" << endl;
		return;
		cout << "ok inside next iff" << endl;
	}

	
	plane* psave = NULL;

	psave = pbefore2->pnext;
	
	pbefore2->pnext = pbefore->pnext;
	
	pbefore->pnext = pafter;
	ptarget->pnext = psave;
		
}

This program crashes from the very last line in the program. (Even if I move it up a line or two.)

If I remove the last line and run the program I loose the object before my target.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.