Re: Shuffling a linked list



Hi again,

Richard Heathfield wrote:
Just use the standard shuffling algorithm, mildly adapted for lists.

Point one of your pointers, P1, to the first unprocessed item in the list,
item I. (There are 32 - I unprocessed items.) Pick a random number in the
range 1 to 32 - I. Counting the item pointed to by P1 as 1, keep counting
along the list, using P2 to point, until you hit the random number you
generated. Swap the data at P1 with the data at P2. Now move P1 to point to
the next item on the list. Iterate until done. (When P1 is NULL - because
it fell off the end of the list - you're done.)

oops actually it is not so good idea, because
I have to run from the beginning again and again
(to find a position i).

I'll probably go for 2 lists then.


Regards
Alex

--
http://preferans.de

.



Relevant Pages

  • Re: very strange pthread problem
    ... The worker threads need access to a class member function or two. ... STL vector so each index into the vector points to the list of pointers ... it's just storing the pointers to the STL lists and the vector ... Then I moved the whole thing to a single cpu Linux ...
    (comp.programming.threads)
  • Re: Implementing Lisp in C?
    ... What I did was to pack the pointers ... it meant bigger header blocks on boxed objects, ... Whenever the "to-visit" list becomes empty, ... both the garbage and to-visit lists are empty, ...
    (comp.lang.lisp)
  • Re: Newbie question about "cons"?
    ... cons structure is just a list of conses jointed by cdr pointers, ... can substitute an array consisting entirely of "car" pointers. ... CDR-coding ALLOWS lists to have cells allocated ...
    (comp.lang.lisp)
  • Re: abstract containers: does such a thing exist, conceptually?
    ... create a firewall between different types of lists. ... > behaviour by the additional pointers. ... I do not like much STL's style of iteration with iterator objects, ... For example, an array view for containers with ordered elements, ...
    (comp.object)
  • Re: A novel design of linked lists (was: Address of an object)
    ... Let's consider referential double-linked lists as an example. ... other languages would be adding Prev, Next pointers to the elements. ... The language will let you convert between the two access ... you're trying to trick your Ada compiler into allowing ...
    (comp.lang.ada)