Re: Shuffling a linked list
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 29 Dec 2006 18:13:21 -0500
Oliver Wong wrote:
"A. Farber" <Alexander.Farber@xxxxxxxxx> wrote in message
I have a doubly-linked list of 32 playing cards, which I'd like
to randomly and evenly (!) shuffle.
(For several reasons I'm not using an array, which I could go
through once, keeping swapping cards at random positions.)
I have a list head with 2 pointers - to the very 1st and to the
last elements and I have 2 macros to insert an element at the
"head" or at the "tail".
Does anybody please have a good algorithm for me?
You have 2 lists: an input list and an output list. At the start
of the algorithm, your input list is of length 32, and your output
list is of length 0. At the end of the algorithm, your input list
will be length 0 and your output list will be length 32.
while(there are still cards in the input list) {
choose a card randomly from the input list.
Move that card to the tail of the output list.
}
That algorithm can be simplified by making the input list
circular. I also see no need for a doubly linked list, unless the
second link is used to form the output list in place.
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
.
- References:
- Shuffling a linked list
- From: A. Farber
- Re: Shuffling a linked list
- From: Oliver Wong
- Shuffling a linked list
- Prev by Date: Re: reading BIOS dump
- Next by Date: Re: reading BIOS dump
- Previous by thread: Re: Shuffling a linked list
- Next by thread: Re: Shuffling a linked list
- Index(es):
Relevant Pages
|