Linked list

Pages: 12
When you delete head, you never make it NULL. The phrase if (!head) only works if you have set it to NULL yourself.

I would suggest you make a linked list of ints and focus on add/remove/delete methods.

You seem to have three delete functions, surely there only needs to be one.

Many colleges post homework/lectures online. It can take some manual URL editing, but there is some really great stuff that a google search might not find.

Just found this:
http://gauss.ececs.uc.edu/Courses/C228.html

Btw, I post links as their own line on this site, it thought that the comma was part of your link.
closed account (D80DSL3A)
I have tracked down the cause of the crash at program end.

The functions Vampire() and AddAge() don't check if head is NULL before dereferencing, so they cause a crash when the list is empty.

Adding if( !head ) return 0; and if( !head ) return; in
Vampire and AddAge (respectively) as the first line stops the crashing.

EDIT: Actually, in AddAge, what is the else for? What value would curr have?
I believe it doesn't belong. That is causing the crash when head==NULL.
Last edited on
Thanks a bunch It has fixed the issue. I get no more crashes. One of the delete functions is commented out as it was the original and I didn't want to remove it in case I ever need to see where I was making mistakes. The reason I have 2 reapers is one is for normal rabbits that die at age 10 and the other is for vampire rabbits which die at age 50. I realise these could be put into one function but I decided to separate them since I was running into trouble and wanted to make things less complicated. I will definitely look at the site and practice with ints.

I started this exercise as I wanted practice with pointers and thought it would help me also understand their uses in real life scenarios, which I find hard to see if you look at the standard examples/tutorial for pointers, such as:

http://www.cplusplus.com/doc/tutorial/pointers/

Thanks for all the help! I will mark this thread as solved.

Last edited on
closed account (D80DSL3A)
You're welcome. I had to find the bugs because I couldn't find any more problems with my own version of the remove functions.

The initial version without the else was advancing iter twice following a node deletion, hence some skip overs (missing cases that should be removed) and the occasional crash (when the node skipped over is the tail).

@LowestOne: I didn't see your post before posting. I didn't think to check as it was approaching 24 hours since last reply.
Ninja coincidence is amazing sometimes.

When you delete head, you never make it NULL. The phrase if (!head) only works if you have set it to NULL yourself.

I actually chased several ghosts around the room trying to fix my unbroken Reaper function, and that was one of them. The NULL being sought is the one marking end of list. Most functions are relying on this to terminate iteration, so there would be many problems if that didn't work right (and it would be AddNode's fault).

Topic archived. No new replies allowed.
Pages: 12