hi guys, i'm New to this forum and would really appreciate your help. I've been learning linked lists recently and am trying to implement the josephus problem using c++, josephus problem is a game in which there are N number of players who are going to be eliminated after every M passes, i think i got the insertion part alright, and also the display function seems to work fine but I'm still struggling with the deletion after M passes. can some one please help give me some tips on how i might implement it.
below is my deletion algorithm
thanks
below is my deletion algorithm
Code:
[int list::Delete(int M)
{
//try to find the node with its value equal to n
node* prevNode = NULL;
node* currNode =head;
int currIndex =1;
while(currNode && currNode->data != M)
{
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
//found it
if(currNode)
{
if(prevNode)
{
prevNode->next = currNode->next;
delete currNode;
}
else //delete the first Node
{
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}]
i called it in main as
[for(int i = N; i =1; i--)
{
list.delete(M);
}}
Comment