Connecting Tech Pros Worldwide Help | Site Map

SHuffling a Linked List

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 14th, 2006, 11:05 PM
RishiD
Guest
 
Posts: n/a
Default SHuffling a Linked List

Hi,

Trying to shuffle a linked list of cards around. I know not the ideal
structure to use, but it is what my professor wants. I wrote all the
code and the logic makes sense, but for some reason when my random
integer is 0, it links the current node to itself and just breaks the
entire linked list.

Thanks for any help,

Rishi

void shuffle()
{

node<card*curr = NULL, *prev = NULL;

int myIndex;

randomNumber rnd;

// move 50 cards around
for (int i = 0; i < 50; i++)
{
long rndNum = rnd.random(52);

myIndex = 0;

curr = front;

// scan until index reaches rndNum or reach end of list
while (curr != NULL && myIndex != rndNum)
{
prev = curr;
curr = curr->next;
myIndex++;
}

// if prev == null, that means we are shuffling first node so just
dont do anything
if (prev != NULL)
{
cout << myIndex << " - curr " << curr->nodeValue.getValue() << "
prev " << prev->nodeValue.getValue() << endl;
prev->next = curr->next;
curr->next = front;
front = curr;
}
}
}


  #2  
Old November 15th, 2006, 04:25 AM
Mark P
Guest
 
Posts: n/a
Default Re: SHuffling a Linked List

RishiD wrote:
Quote:
Hi,
>
Trying to shuffle a linked list of cards around. I know not the ideal
structure to use, but it is what my professor wants. I wrote all the
code and the logic makes sense, but for some reason when my random
integer is 0, it links the current node to itself and just breaks the
entire linked list.
>
Thanks for any help,
>
Rishi
>
void shuffle()
{
>
node<card*curr = NULL, *prev = NULL;
You initialize prev once, up here, and then...
Quote:
>
int myIndex;
>
randomNumber rnd;
>
// move 50 cards around
for (int i = 0; i < 50; i++)
{
long rndNum = rnd.random(52);
>
myIndex = 0;
>
curr = front;
>
// scan until index reaches rndNum or reach end of list
while (curr != NULL && myIndex != rndNum)
{
prev = curr;
curr = curr->next;
myIndex++;
}
>
// if prev == null, that means we are shuffling first node so just
dont do anything
.... you check it down here, when it may not have been reinitialized.
Think: what is the state of prev in the case where rndNum = 0?
Quote:
if (prev != NULL)
{
cout << myIndex << " - curr " << curr->nodeValue.getValue() << "
prev " << prev->nodeValue.getValue() << endl;
prev->next = curr->next;
curr->next = front;
front = curr;
}
}
}
>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.