|
|
|
|
|
|
|
// Postcondition:
// New node containing newCard is inserted at top of linked list
// && listLength == listLength@entry + 1
{
NodePtr newNodePtr = new NodeType; // Temporary pointer
newNodePtr->card = newCard;
newNodePtr->link = head;
head = newNodePtr;
listLength++;
}
//******************************************************************
void CardPile::RemoveTop( /* out */ CardType& topCard )
// Precondition:
// listLength > 0
// Postcondition:
// topCard == card member of first list node at entry
// && Node containing topCard is removed from linked list
// && listLength == listLength@entry - 1
{
NodePtr tempPtr = head; // Temporary pointer
topCard = head->card;
head = head->link;
delete tempPtr;
listLength--;
} |
|
|
|
|
|
|
|
|
Problem-Solving Case Study Solitaire Simulation |
|
|
|
|
|
|
|
|
Problem: There is a solitaire game that is quite simple but seems difficult to win. Let's write a program to play the game, then run it a number of times to see if it really is that difficult to win or if we have just been unlucky. |
|
|
|
|
|
|
|
|
Although this card game is played with a regular poker or bridge deck, the rules deal with suits only; the face values (ranks) are ignored. The rules are listed below. Rules 1 and 2 are initialization. |
|
|
|
|
|