我有一个函数需要一个双向链表。头是第一个节点,尾是最后一个节点。有52个节点(卡牌)我试图做的是执行一个洗牌,就像你用手洗牌一样。所以你把甲板砍下一半,从上半部分拿下底牌,将底牌从另一半上取下,并将其放在顶部。将一个链表重新排序
所以。左半部分是1 2 3右半部分是4 5 6后洗牌4 1 5 2 6 3
我知道我的错误是在指针中,他们相互覆盖。我只是不知道如何正确地对这些列表进行排序,并将它们一个接一个地抽出来,然后将它们粘在一起。
bool Shuffle(deck* &head, deck* &tail, int ShuffleAmnt)
{
deck* temp_tail = nullptr;
deck* temp_head = head;
deck* temp_list = new deck;
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) // get to the bottom of head
head = head->next;
temp_head->previous = nullptr;
temp_tail->next = nullptr;
for (int i = 1; i < 26; i++)
{
temp_list->next = temp_head;
temp_head = head->previous;
temp_list = temp_list->next;
temp_list->next = temp_tail;
}
head = temp_list;
return(true);
}
你的第三个参数是做什么的? – TheDillo 2014-12-13 08:09:40
这是谁的想法来代表一个链接列表卡组?一个'std :: vector','std :: array'或者一个普通的旧数组更有意义。 – PaulMcKenzie 2014-12-13 08:17:09
洗牌链接列表的最安全方法是将数据复制到数组,将数组随机混洗,然后将数组结果复制回您的列表。假设你有通过你的列表的函数,并且可以将数据复制回你的列表。此外,洗牌应该是随机的,而不是像你的代码似乎暗示的那样“控制”。例如:在洗牌后,甲板上的第一张牌可能会潜在甲板上的任何位置。 – PaulMcKenzie 2014-12-13 08:23:00