Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

linked list problem

Posted on 2011-09-26
15
299 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
LVL 33

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 33

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 33

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 89
C++ mouse_event mouse look 7 97
designing in object programming 12 94
Android development question 2 54
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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