Bobbin4Apples
Bobbin4Apples

Reputation: 145

Am I re-arranging my LinkedList nodes properly in this algorithm?

Please note that this is an assignment. I don't need outright answers for the issue, but I'd like to know where I'm messing up. I believe I'm getting an infinite loop or I'm losing a link at some point.

My task is to take in a list, remove duplicates (if they aren't the first duplicate), and then create a mirror image of the list. If there is only one duplicate of the data, then use that node and move it instead of deleting it. If there are no duplicates, create a node for it. I'm not allowed to use outside functions to do this. Which means a lot of pointer manipulation.

My thought process on it was that I would have an anchor and loop through to find if there are any duplicates. If there are, move the first duplicate to the end of the list and delete the rest of the duplicates. This would create the first half of the list as the original list and the second half would be the mirror.

Also note that if the list is empty, do nothing. If there is only one node, copy it and be done.

Here is what I've coded, but it seems to have an issue. It keeps telling me that the exe has stopped working when I try and build it.

Edit #2 Modifying code, will update shortly.

Can anyone help point out where my mistake is? I've been racking my brain over it.

Update It looks like I had an issue when dealing with making the last node link to nullptr. I've updated the code and now my issue seems to be that I'm not handling all the nodes. I'm assuming I'm not handling the last one? I'll keep working on this and checking in to see if anyone has pointed out my error.

Upvotes: 0

Views: 89

Answers (1)

trm
trm

Reputation: 420

Since end points to the last node in the list, while(anchor != end) causes the loop to end before copying the last node. You'll have to either change your loop's exit conditions or consider end a special case and copy it after the loop.

You might also want to consider what happens if the last node in the list is a duplicate of an earlier one and is therefore removed.

Upvotes: 1

Related Questions