Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

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.
#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
pgmerLA
Asked:
pgmerLA
  • 9
  • 3
  • 2
1 Solution
 
Hugh McCurdyCommented:
I'll look into it.
0
 
Hugh McCurdyCommented:
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
 
pgmerLAAuthor Commented:
How can I fix it to make the program work?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Hugh McCurdyCommented:
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
 
Hugh McCurdyCommented:
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
 
Hugh McCurdyCommented:
I'm going to bed now but I'll check back in the morning to continue with helping you through this.
0
 
Hugh McCurdyCommented:
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
 
sarabandeCommented:
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
 
Hugh McCurdyCommented:
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
 
sarabandeCommented:
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
 
Hugh McCurdyCommented:
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
 
sarabandeCommented:
Hugh,

i printed the list at end and got

25
30
45
60
65
80
90

Open in new window


for output.

Sara
0
 
Hugh McCurdyCommented:
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
 
pgmerLAAuthor Commented:
Thank you guys.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now