------------------------------------------------------------------------------
MC logo
C++ Int Stack Impl
[^] Names and Scope
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Java Class Scope] [Ada Int Stack Package] [Ada Int Stack Impl] [Ada Int Stack Driver] [Java Int Stack Class] [Java Int Stack Driver] [C++ Int Stack Class] [C++ Int Stack Impl] [C++ Int Stack Driver]
intstack.cpp
/*
 * Stack implementation methods.
 */

#include "intstack.h"

// Remove from the stack.
int IntStack::pop() {
        if (head != NULL) {
                // Unlink the top node.
                StackNode *oldtop = head;
                head = head->next;

                // Get the data out of it, and destroy it.
                int retval = oldtop->data;
                delete oldtop;

                // Return the top value.
                return retval;
        } else
                return 0;
}

// Remove all memory from the stack.
void IntStack::clear() {
        while(head != NULL) {
                // Advance the pointer and delete the old node.
                StackNode *zombie = head;
                head = head->next;
                delete head;
        }
        head = NULL;
}