pgmerLA
asked on
linked list problem
Hi, I am trying to build an unorder linked list.
The program compile ok but then it keeps crashing.
can you please help me?
Thanks.
The program compile ok but then it keeps crashing.
can you please help me?
Thanks.
#include<iostream>
#include<cstdlib>
using namespace std;
struct NodeType{
int info;
NodeType* next;
};
int main()
{
NodeType *first, *last, *newNode;
first =NULL;
last=NULL;
int a[]={25,30,45,60,65,80,90};
for(int i=0;i<7;++i)
{
newNode= new NodeType;
newNode->info=a[i];
newNode->next=NULL;
if(first->next == NULL)
{first=newNode;
last=newNode;
}
else
{
last->next=newNode;
last=newNode;
}
}
return 0;
}
I'll look into it.
The short answer is that the program is crashing at this statement
first is still null. You can have a null pointer but you can't use it.
Does this make sense to you? If not, ask for more help.
if(first->next == NULL)
first is still null. You can have a null pointer but you can't use it.
Does this make sense to you? If not, ask for more help.
ASKER
How can I fix it to make the program work?
Unless I'm mistaken, fixing just that problem won't make the program work. I'll need some time to study what you did.
I figured it out. The question looks a lot like a homework assignment. Experts aren't allowed to simply give answers to homework. We are allowed to help.
First, I'd lose the variable called "last" and replace it with a variable called "current." I think this makes the program easier to understand.
Inside the for loop, add a test that compares i to 0. If i == 0, then you want to initialize first by setting it to newNode.
The key part that you had missing is what to do when i doesn't equal 0. In that case, you want to set the old current node's next pointer to newNode.
After you leave the if/then/else, you want to set current equal to newNode.
I also suggest you add a for loop at the end where you traverse the list and print so that you can verify that you really have a linked list.
Do you understand? If you don't, please ask questions but also get rid of the if/then/else in your code and replace it with a test of i against 0 and post the updated code along with your question. (Just getting a student to type the code himself has educational value.)
First, I'd lose the variable called "last" and replace it with a variable called "current." I think this makes the program easier to understand.
Inside the for loop, add a test that compares i to 0. If i == 0, then you want to initialize first by setting it to newNode.
The key part that you had missing is what to do when i doesn't equal 0. In that case, you want to set the old current node's next pointer to newNode.
After you leave the if/then/else, you want to set current equal to newNode.
I also suggest you add a for loop at the end where you traverse the list and print so that you can verify that you really have a linked list.
Do you understand? If you don't, please ask questions but also get rid of the if/then/else in your code and replace it with a test of i against 0 and post the updated code along with your question. (Just getting a student to type the code himself has educational value.)
I'm going to bed now but I'll check back in the morning to continue with helping you through this.
Following up on SouthMod, it's not just the instructors but other students or former students who are tutors, study group leaders or course assistants. (I'm the first two of those and working on becoming the 3rd of those.)
I would also like to add that it is important to learn these concepts. Some job interviews for programmers include a test such as "write this simple function using recursion." Fail the test, no job.
Having said all that, I'm quite willing to help you learn. If you produce more code, even non-working code, I will be quite willing to help you further. I can help you through this process.
I would also like to add that it is important to learn these concepts. Some job interviews for programmers include a test such as "write this simple function using recursion." Fail the test, no job.
Having said all that, I'm quite willing to help you learn. If you produce more code, even non-working code, I will be quite willing to help you further. I can help you through this process.
hmccurdy, the 'key part' was not missing. it was in the 'else' branch of the code posted by the questioner.
pqmerLA, if you would check for 'first' being NULL rather than for 'first->next' your code should work.
before using a -> operator you always need to make sure that the pointer left of the arrow is not NULL.
Sara
pqmerLA, if you would check for 'first' being NULL rather than for 'first->next' your code should work.
before using a -> operator you always need to make sure that the pointer left of the arrow is not NULL.
Sara
Sara, I tried that and, perhaps I tried it wrong, but it didn't work. I ended up rewriting a few more lines and I have the program working. Of course, as this appears to be homework, I'm not going to simply publish my working program.
One thing that is missing, for the development phase, is some test code. I added that to my program. I used a for loop which starts at first and traverses the list, printing the data as it goes. That didn't work for me with a simple test (but perhaps I broke the program first). After my changes, it did work.
I'm willing to help the author through the process if he/she wants help learning.
Hugh
One thing that is missing, for the development phase, is some test code. I added that to my program. I used a for loop which starts at first and traverses the list, printing the data as it goes. That didn't work for me with a simple test (but perhaps I broke the program first). After my changes, it did work.
I'm willing to help the author through the process if he/she wants help learning.
Hugh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sara,
The program runs but it didn't pass my test. My test showed a list with a single node. Again, I could have messed up. Anyway, the copy I ended up with showed a list with 7 nodes.
Of course, I could have broken the program before I fixed it. I don't know. At this point, unless the author shows up looking for tutoring. it doesn't much matter.
Hugh
The program runs but it didn't pass my test. My test showed a list with a single node. Again, I could have messed up. Anyway, the copy I ended up with showed a list with 7 nodes.
Of course, I could have broken the program before I fixed it. I don't know. At this point, unless the author shows up looking for tutoring. it doesn't much matter.
Hugh
Hugh,
i printed the list at end and got
for output.
Sara
i printed the list at end and got
25
30
45
60
65
80
90
for output.
Sara
OK, Sara, I must have done something to break it before I fixed it.
Now if the author comes back, we can help him/her.
I do think that last should be renamed to currentNode or something like that for clarity. But that's just style.
Now if the author comes back, we can help him/her.
I do think that last should be renamed to currentNode or something like that for clarity. But that's just style.
ASKER
Thank you guys.