|
|
|
|
|
|
|
The private variable head is the external pointer to the linked list, and the private variable listLength keeps track of the current length of the list. |
|
|
|
|
|
|
|
|
The class constructor, copy-constructor, and destructor: These functions are essentially identical to those in the OrdList class. The only significant difference is in the constructor. In addition to setting head to NULL, the constructor must set listLength to zero. |
|
|
|
|
|
|
|
|
The InsertTop and RemoveTop functions: These functions are the same as InsertTop and DeleteTop in the OrdList class, with the following differences. After inserting a new node, InsertTop must increment listLength; and after deleting a node, RemoveTop must decrement listLength. |
|
|
|
|
|
|
|
|
The Length function: Because the private variable listLength always indicates the current length of the list, no looping and counting are required. The function body is a single statement: |
|
|
|
|
|
|
|
|
The CardAt function: To access the card at position n in the pile, we must start at the front of the list and sequence our way through the first n - 1 nodes. When we get to the desired node, we return the card member of the node as the function return value. |
|
|
|
|
|
|
|
|
Set currPtr = head
FOR count going from 1 through n -1
Set currPtr = link member of *currPtr
Return card member of *currPtr |
|
|
|
|
|
|
|
|
|
Here is the implementation file containing the definitions of the CardPile member functions: |
|
|
|
|
|
|
|
|
//******************************************************************
// IMPLEMENTATION FILE (cardpile.cpp)
// This file implements the CardPile class member functions
// List representation: a linked list of dynamic nodes
//******************************************************************
#include cardpile.h
#include <stddef.h> // For NULL |
|
|
|
|
|