//******************************************************************
// IMPLEMENTATION FILE (cardpile.cpp)
// This file implements the CardPile class member functions
// List representation: a linked list of dynamic nodes
//******************************************************************
#include "cardpile.h"
#include // For NULL
using namespace std;
typedef NodeType* NodePtr;
struct NodeType
{
CardType card;
NodePtr link;
};
// Private members of class:
// NodePtr head; External pointer to linked list
// int listLength; Current length of list
//******************************************************************
CardPile::CardPile()
// Constructor
// Postcondition:
// head == NULL && listLength == 0
{
head = NULL;
listLength = 0;
}
//******************************************************************
CardPile::CardPile( const CardPile& otherPile )
// Copy-constructor
// Postcondition:
// listLength == otherPile.listLength
// && IF otherPile.head == NULL
// head == NULL
// ELSE
// head points to a new linked list that is a copy of
// the linked list pointed to by otherPile.head
{
NodePtr toPtr; // Pointer into new list being built
NodePtr fromPtr; // Pointer into list being copied from
listLength = otherPile.listLength;
if (otherPile.head == NULL)
{
head = NULL;
return;
}
// Copy first node
fromPtr = otherPile.head;
head = new NodeType;
head->card = fromPtr->card;
// Copy remaining nodes
toPtr = head;
fromPtr = fromPtr->link;
while (fromPtr != NULL)
{
toPtr->link = new NodeType;
toPtr = toPtr->link;
toPtr->card = fromPtr->card;
fromPtr = fromPtr->link;
}
toPtr->link = NULL;
}
//******************************************************************
CardPile::~CardPile()
// Destructor
// Postcondition:
// All linked list nodes have been deallocated from free store
{
CardType temp; // Temporary variable
while (listLength > 0)
RemoveTop(temp);
}
//******************************************************************
int CardPile::Length() const
// Postcondition:
// Function value == listLength
{
return listLength;
}
//******************************************************************
CardType CardPile::CardAt( /* in */ int n ) const
// Precondition:
// 1 <= n <= listLength
// Postcondition:
// Function value == card member of list node at position n
{
int count; // Loop control variable
NodePtr currPtr = head; // Moving pointer variable
for (count = 1; count < n; count++)
currPtr = currPtr->link;
return currPtr->card;
}
//******************************************************************
void CardPile::InsertTop( /* in */ CardType newCard )
// Precondition:
// newCard is assigned
// Postcondition:
// New node containing newCard is 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 no longer in linked list
// && listLength == listLength@entry - 1
{
NodePtr tempPtr = head; // Temporary pointer
topCard = head->card;
head = head->link;
delete tempPtr;
listLength--;
}