Bumping my object on a linked list

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2005
Posts: 3
Reputation: Keyonts is an unknown quantity at this point 
Solved Threads: 0
Keyonts Keyonts is offline Offline
Newbie Poster

Bumping my object on a linked list

 
0
  #1
Nov 28th, 2005
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.


  1. void bumpPlane (plane*& pstart)
  2. {
  3. int flight;
  4. cout << " Enter the flight number you would like to move ahead" << endl;
  5. cin >> flight;
  6. plane* ptarget = NULL;
  7. plane* pcurrent;
  8. pcurrent = pstart;
  9. plane* pbefore = NULL;
  10.  
  11. if(pstart == NULL)
  12. cout << "No planes on the queue" << endl;
  13.  
  14.  
  15. while (pcurrent != NULL)
  16. {
  17. if(pcurrent->flightnum == flight)
  18. ptarget = pcurrent;
  19.  
  20. else
  21. {
  22. pbefore = pcurrent;
  23. pcurrent = pcurrent->pnext;
  24.  
  25. }
  26. }
  27. if (ptarget = NULL)
  28. {
  29. cout << "Could not find flight number in the queue" << endl;
  30. return;
  31. }
  32.  
  33. plane* psave = NULL;
  34.  
  35. psave = pcurrent->pnext;
  36.  
  37. pcurrent->pnext = pbefore;
  38.  
  39. pbefore = psave;
  40.  
  41. }



ps. My first post here, though im a lurker and like to browse around here to pump me up to do my homework.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Bumping my object on a linked list

 
0
  #2
Nov 28th, 2005
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? 

}
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 3
Reputation: Keyonts is an unknown quantity at this point 
Solved Threads: 0
Keyonts Keyonts is offline Offline
Newbie Poster

Re: Bumping my object on a linked list

 
0
  #3
Nov 29th, 2005
[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.

  1. void bumpPlane (plane*& pstart)
  2. {
  3. int flight;
  4. cout << " Enter the flight number you would like to move ahead" << endl;
  5. cin >> flight;
  6. plane* ptarget = NULL;
  7. plane* pcurrent;
  8. pcurrent = pstart;
  9. plane* pbefore = pstart;
  10. plane* pbefore2 = pstart;
  11. plane* pafter = pstart;
  12.  
  13. if(pstart == NULL)
  14. cout << "No planes on the queue" << endl;
  15.  
  16.  
  17. while (pcurrent != NULL)
  18. {
  19. if(pcurrent->flightnum == flight)
  20. {
  21. ptarget = pcurrent;
  22. pafter = pcurrent->pnext;
  23. cout << "target found" << endl;
  24.  
  25. pcurrent = NULL;
  26. }
  27. else
  28. {
  29. pbefore2 = pbefore;
  30. pbefore = pcurrent;
  31. pcurrent = pcurrent->pnext;
  32. cout << "target not found, moving pointers"<< endl;
  33.  
  34.  
  35.  
  36.  
  37. }
  38. }
  39. if (ptarget = NULL)
  40. {
  41. cout << "Could not find flight number in the queue" << endl;
  42. return;
  43. cout << "ok inside next iff" << endl;
  44. }
  45.  
  46.  
  47. plane* psave = NULL;
  48.  
  49. psave = pbefore2->pnext;
  50.  
  51. pbefore2->pnext = pbefore->pnext;
  52.  
  53. pbefore->pnext = pafter;
  54. ptarget->pnext = psave;
  55.  
  56. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Bumping my object on a linked list

 
0
  #4
Nov 29th, 2005
Try this
  1. plane* psave = NULL;
  2. psave = pbefore2->pnext;
  3.  
  4. pbefore2->pnext = pbefore->pnext;
  5. psave->pnext = ptarget->pnext;
  6.  
  7. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1796 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC