Solved

linked list problem

Posted on 2011-09-26
15
296 Views
Last Modified: 2012-05-12
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.
#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;
}

Open in new window

0
Comment
Question by:pgmerLA
  • 9
  • 3
  • 2
15 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36681259
I'll look into it.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36683149
The short answer is that the program is crashing at this statement    
if(first->next == NULL)

Open in new window


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.
0
 

Author Comment

by:pgmerLA
ID: 36699803
How can I fix it to make the program work?
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36707472
Unless I'm mistaken, fixing just that problem won't make the program work.  I'll need some time to study what you did.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36707501
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.)



0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36707537
I'm going to bed now but I'll check back in the morning to continue with helping you through this.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36709806
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.


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:sarabande
ID: 36710865
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
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36710980
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
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 36711045
Hugh, i only removed the '->next' from original code and it runs to end of main with a valid 'last' pointer.

at end of main the statement  'return 0;' is missing (though some compilers won't complain about).

also, you are right that a 'print' function which iterates the list after successfully creating it, surely would be a valuable add-on.

Sara
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36711260
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
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36711455
Hugh,

i printed the list at end and got

25
30
45
60
65
80
90

Open in new window


for output.

Sara
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36712743
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.
0
 

Author Comment

by:pgmerLA
ID: 36901484
Thank you guys.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now