![]() ![]() Here is a link to the documentation on std::random_shuffle. It is the foundation of elegant, maintainable code. If you are looking forward to a rewarding career writing c++, take time to learn the standard library thoroughly - many c++ programmers don't bother to learn the algorithm library - this is a mistake. Std::random_shuffle(deck.begin(), deck.end()) If you decide to do the Right Thing(tm) and make your deck of cards a vector, it's even simpler: typedef std::vector deck_of_cards Std::move(temp.begin(), temp.end(), back_inserter(the_deck)) move the cards back into the linked list in the correct order Std::random_shuffle(temp.begin(), temp.end()) those clever fellows in the standards committee thought of everything. ![]() clear out the zombie objects that will have been left by the move Std::make_move_iterator(the_deck.end())) Std::vector temp(std::make_move_iterator(the_deck.begin()), c++11 takes advantage of efficiently copying by value. Typedef std::list deck // that's it - a complete linked list implementation If you really do want to keep cards in a linked list (you don't, a vector is more natural) you can refactor to this: #include In short, there is no reason to consider writing a linked list ( std::list) or writing a shuffle algorithm ( std::random_shuffle). These building blocks are in use daily in billions of computers, have been optimised for efficiency with your compiler and have been peer-reviewed by every single one of the World's most expert programmers. The c++ standard library already has template implementations of (almost) every container type and logical operation you will ever need. This answer may not directly answer the question but it is here to guide and educate if that is desired. While (head->next != NULL) // get to the bottom of head bool Shuffle(deck* &head, deck* &tail, int ShuffleAmnt) I just don't know how to correctly sort through these lists and pluck them out one by one and stick them back together. I know my mistake is in the pointers and that they are over-writing each other. Left Half is 1 2 3 Right Half is 4 5 6 After the shuffle 4 1 5 2 6 3 So you cut the deck in half take the bottom card off the first half place it in the new pile, take the bottom card off the other half and place it on top of that one. There are 52 nodes (Deck of cards) what I'm trying to do is perform a a shuffle as if you shuffled them by hand. Head is the first node and tail is the last node. I have a function that takes a doubly linked list. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |