473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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;
}
}
}

Nov 15 '06 #1
1 9113
RishiD wrote:
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...
>
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?
if (prev != NULL)
{
cout << myIndex << " - curr " << curr->nodeValue.getValue() << "
prev " << prev->nodeValue.getValue() << endl;
prev->next = curr->next;
curr->next = front;
front = curr;
}
}
}
Nov 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.