Question

What I have to do is this:

Add a function replace to the deck class that is passed a card as a parameter by value. The card is placed on the bottom of the deck

CardPtr is a private class variable, it is set to the top card of the deck in the constructor

node<card> *newCard; //card pointer
newCard = new node<card>(card(2, 0), NULL);  //points to the first card
CardPtr = newCard; //sets cardpointer to newcard

A separate class takes the top card off and diplays the value, deleting that node from the deck in the process. I am trying to put that card back in on the bottom. then this is the code:

    `void deck::replace(card BottomCard)
    {
        int count = 0;
        node<card> *replaceptr;
        bottom = NULL;
        replaceptr = new node<card>(BottomCard, NULL);
        if (bottom != NULL)
        {
    bottom->next = replaceptr;
        }
        else
        {
    bottom = replaceptr;
        }

        if (count >= 52)
        {
            throw overflowError("ERROR: Deck is already full");
        }

        //CardPtr = new node<card>(BottomCard, NULL);
        //bottom->next = CardPtr;
}

I implement it in my main code and it seems to replace the card at the top of the deck. Can anyone help me please? Thank you.

Here is the Constructor I have in full:

    deck::deck() // deck constructor
{
    node<card> *newCard; //card pointer
    newCard = new node<card>(card(2, 0), NULL);  //points to the first card
    CardPtr = newCard; //sets cardpointer to newcard

    for (int i = 0; i < 4; i++)
    {
        for (int j = 2; j < 15; j++)
        {
            if (i == 0 && j == 2) continue;

            node<card> *temp = new node<card>(card(j, i), NULL);  // temporary pointer sets card value and suit to numbers for value and suit

            newCard->next = temp;// sets newcard pointer .next to temp
            newCard = temp;// sets newcard to temp          
        }
    }
}
Was it helpful?

Solution

If it's the responsibility of CardPtr to always point at the top card (the start of the linked list), then this method's code should be changed so that it does not modify CardPtr. Perhaps declare another local variable, initialize it to equal CardPtr and then modify that pointer repeatedly in your loop (so that CardPtr never changes).

It's also possible that your other method that deals a card from the top of the deck is not leaving CardPtr in a good state (pointing at the new top card), which means you've got more than one problem to fix.

OTHER TIPS

You need to determine the second-to-last node in the list (the card before the bottom card), because that node's next pointer will need to refer to the new bottom card.

Then, simply change the next entry of the second-to-last node to the new node.

enter image description here

(Be sure to delete the old bottom card node after you insert the new one, or else you may end up with a memory leak)

EDIT

If you were to use a circular list, you could save the effort of scanning the list for the last node. You could store a pointer to the last node, whose next pointer (instead of being null) would refer to the first node.

Look here

bottom = new node <card>;

while (CardPtr != NULL)
{
    if (CardPtr->next == NULL)
    {
        bottom = CardPtr;
    }
}

First, you allocate a new card and assign it to bottom, then in your while loop you assign bottom to CardPtr which will cause a memory leak. you should instead remove the new and just set bottom to NULL.

CardPtr = new node<card>(BottomCard, NULL);
if ( bottom != NULL )
{
  bottom->next = CardPtr;
}
else
{
  bottom = CardPtr;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top