So i'm currently doing an assignment using a linked list and pointers for creating a playlist. For a specific section, we need to create code that changes the position of each song. The specific direction:
"Implement the "Change position of song" menu option. Prompt the user for the current position of the song and the desired new position. Valid new positions are 1 - n (the number of nodes). If the user enters a new position that is less than 1, move the node to the position 1 (the head). If the user enters a new position greater than n, move the node to position n (the tail). 6 cases will be tested."
I have written this much so far but when I run it, there is a segmentation fault that I am unable to recognize to fix. I was wondering where it occurs.
*already declared
PlaylistNode* head = 0;
PlaylistNode* tail = 0;
and PlaylistNode is a class that has the member functions GetNext, SetNext, etc*
Line 40 to 43: You use currCurrentPosition without checking whether it is valid. This would happen if head is null or the new position is not found (See line 8).
Note: instead of 0 or NULL better use the nullptr to initialize a pointer.