I'm trying to perform a perfect shuffle on a doubly linked list and am running into some issues that I cannot handle.
I'm able to split the linked list into two separate piles both starting at the back of the lists. I can also get the first two cards to shuffle from both sides of the list. But I lose control of the linked list and it all changes and messes up the list. I need some assistance on what to do.
I know that the loop isn't complete. The whole list breaks before I get out there so I have been stuck there.
This is what it should do.
First half, 1 , 2 , 3 , 4 Second half 5 , 6 , 7 , 8
After shuffle from the bottom of the deck - 4, 8 , 3 , 7 , 2 , 6 , 1 , 5
I'm a bit out of my element here, any help would be appreciated.
Rest of the code.
http://pastebin.com/yk727sYa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
bool Shuffle(Card* &head, Card* &tail, int ShuffleAmnt)
{
Card* temp_tail = nullptr;
Card* temp_head = head;
Card* temp_list = new Card;
for (int i = 1; i < 26; i++)
temp_head = temp_head->next;
temp_tail = temp_head->next;
temp_tail->previous = nullptr;
temp_head->next = nullptr;
while (head->next != NULL)
{
head = head->next;
temp_tail = temp_tail->next;
}
bool FirstPass = true;
for (int i = 1; i < 26; i++)
{
if (FirstPass == true)
{
temp_list = temp_head;
temp_list->next = nullptr;
temp_list->previous = temp_tail;
temp_list->previous->next = temp_head;
}
temp_head = temp_head->previous;
temp_tail = temp_tail->previous;
}
return(true);
}
|