------------------------------------------------------------------------------
MC logo
C++ Int Stack Class
[^] 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.h
#ifndef _intstack_h_
#define _intstack_h_

#include <cstddef>

// Implements a stack of integers.
class IntStack {
private:
        // Stack node type.
        struct StackNode {
                StackNode(int d, StackNode *n) {
                        data = d;
                        next = n;
                }
                int data;
                StackNode *next;
        };
    
        // Head of the stack
        StackNode *head;

public:
        // Must make sure stack is empty at construct.
        IntStack() {
                head = NULL;
        }

        // Push onto the stack.
        void push(int i) {
                head = new StackNode(i, head);
        }

        // Remove from the stack.
        int pop();

        // Find out what's on the top.
        int top() {
                if(head == NULL)
                        return 0;
                else
                        return head->data;
        }

        // So, are we empty yet?
        bool isEmpty() {
                return head == NULL;
        }

        // Remove all memory from the stack.
        void clear();
};

#endif