Take the 2-minute tour ×

I'm back again. So I have created a Deck of cards in Linked List fashion using Cards as the nodes. I am having trouble with my riffle shuffle function, however. The function should create two Deck objects that each store half of my Deck drawPile. Those two half piles should then take a card off one at a time and add it back to the drawPile. I am not getting an error, but it is not shuffling the cards at all. I call the function as such: drawPile.shuffle(drawPile);

    top = NULL;
    SIZE = 0;

Deck::Deck(Card *n)
    top = n;
void Deck::shuffle(Deck &d)

        //split deck in half -- leftHalf and rightHalf
        Deck leftHalf, rightHalf;

        int j = SIZE/2;
        for (int k = SIZE; k > j; k--){
            drawCard(leftHalf);  //drawCard off Deck and addCard to leftHalf
        for (j; j > 0; j--){
            drawCard(rightHalf);  //drawCard off Deck and addCard to rightHalf
        while (leftHalf.getSIZE() > 0 && rightHalf.getSIZE() > 0){
            int probPile = leftHalf.getSIZE()/(leftHalf.getSIZE() + rightHalf.getSIZE())*100;
            int randomNum = rand() % 100 + 1;
            if (randomNum <= probPile){
                leftHalf.drawCard(d);   //drawCard off leftHalf and addCard to Deck
                rightHalf.drawCard(d);  //drawCard off rightHalf and addCard to Deck
        while (leftHalf.getSIZE() > 0){
    while (rightHalf.getSIZE() > 0){

void Deck::drawCard(Deck &d)    
    Card *draw = top;
    if (draw != NULL){
        top = top->getNext();
            d.addCard(draw->getVALUE(), draw->getSUIT());
}//end drawCard

void Deck::addCard(int val, string s)
    top = new Card(val, s, top);
}//end addCard
share|improve this question
Instead of making your own container, consider using (or at least wrapping) a standard container, like std::vector. –  Joachim Pileborg Mar 28 at 5:44
As for your problem, have you tried stepping through the code in a debugger, line by line? Do it with just a small "deck" (say ten "cards") to make it easier. –  Joachim Pileborg Mar 28 at 5:47
This is for a class and I am not allowed to use any structure other than linked list. :/ –  Shannon Mar 28 at 5:47
Ignoring whatever contrived probability you're attempting to create, ultimately this is to split the deck, then take a random draw off one pile or the other repeatedly until one side is exhausted, finally append the remainder of whatever side has the last cards to the tail and you're done. Is that about right? If so, this is way more complicated than it needs to be. (and it appears you're forgetting to append the "half" that had at least one card remaining as the finishing move). –  WhozCraig Mar 28 at 6:15
Yes, that is the goal. But, I am unsure how to append the "half" as you mentioned. –  Shannon Mar 28 at 6:22

1 Answer 1

up vote 0 down vote accepted

Your problem is here:


If you have drawn all the cards from leftHalf but rightHalf still contains some cards then your cycle ends. In that case your shuffled Deck doesn't contains all cards.
After that cycle you need for example to implement two cycles like this:

while (leftHalf.getSIZE() > 0)
    // Draw all remaining cards from leftHalf
while (rightHalf.getSIZE() > 0)
    // Draw all remaining cards from rightHalf

Also you can get a "Null pointer access" error here:


because draw can be null.

Also here:

for (int l = SIZE; l > 0; l--){

you try to draw too many cards. You can start the cycle from j instead of SIZE.

Another problem is here:


probPile is always 0 because you use integer division (e.g. 5/100 is 0 if we use integer division). One of the solution to use floating-point division. You can convert int value to double before division in some way, e.g. by multiplying to 1.0 and then convert the result back to int:

int probPile = static_cast<int>(leftHalf.getSIZE() * 1.0 /(leftHalf.getSIZE() + rightHalf.getSIZE())*100);

Or you can multiply by 100 before division:

int probPile = leftHalf.getSIZE() * 100/(leftHalf.getSIZE() + rightHalf.getSIZE());
share|improve this answer
Thank you so much! I ended up putting d.addCard(draw->getVALUE(), draw->getSUIT()); inside of the if statement and that go rid of an error I was having. I am about to test out the new while loops for the remaining cards –  Shannon Mar 28 at 6:41
Ok, so when I incorporated the two extra while loops, I am not getting any errors at all...but the cards after "shuffle" are still ordered. –  Shannon Mar 28 at 6:47
@Shannon regarding your EDIT. It looks like shuffle was called twice at the same time. Perhaps when en error window ("Null pointer") appeared you called shuffle for the second time in some way. –  Alex Antonov Mar 28 at 6:48
My edit was my own mistake, I accidentally forced it to loop again with breakpoints. –  Shannon Mar 28 at 6:54
@Shannon I added solution to the problem that the Deck remains ordered. –  Alex Antonov Mar 28 at 7:01

Your Answer


