| | |
Bumping my object on a linked list
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2005
Posts: 3
Reputation:
Solved Threads: 0
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.
ps. My first post here, though im a lurker and like to browse around here to pump me up to do my homework.
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.
C++ Syntax (Toggle Plain Text)
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?
}•
•
Join Date: Nov 2005
Posts: 3
Reputation:
Solved Threads: 0
[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.
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.
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.
C++ Syntax (Toggle Plain Text)
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
Correct the other mistakes I showed you in my previous reply and the one below.
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.
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Linked List Retrieve Method (C)
- linked list small problem with Insert Function (C++)
- Linked List (C)
- Linked List & Objects (C++)
- How Can I Sort a Doubly Linked List Queue? (C)
- doubly linked list implementation (Java)
Other Threads in the C++ Forum
- Previous Thread: please some one check this code and help me
- Next Thread: Weird bug...
Views: 1796 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






