------------------------------------------------------------------------------
MC logo
Template Generalized Stack
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Template Functions] [Template Generalized Stack] [Template Generalized Stack Driver] [Array Template Stack] [Array Template Test Driver] [Generalized Safe Array] [Safe Array Demo] [Vector Iterator Demo]
stackt1.h
//
//*****************************************************************************
//* C++ stack using templates.  Note that everything is here; there is no
//* implementation CPP file.
//*****************************************************************************
//

#ifndef _STACKT_H_
#define _STACKT_H_

#include <string.h>
#include <stdlib.h>
#include <exception>
#include <stdexcept>

using namespace std;

// This is a template class.  The name T stands for any type.  By saying
// Stack<whatever>, we create a stack of whatevers.
template <typename T>
class Stack
{
public:
        // Create an empty stack.
        Stack() { head = NULL; }

        // Destroy the stack.  The pointers to data are not deleted.
        ~Stack() {
                while(!empty()) { pop(); }
        }

        // Add an element to the stack.  Note that you must send a pointer,
        // and the stack actually stores the pointer.
        void push(const T & e)
        {
                head = new Node(e, head);
        }

        // Pop the stack and return the top item pointer.  If the stack is
        // empty, return the null pointer.
        T pop();

        // Return the top item pointer.  If none, return the null pointer.
        const T & top() const {
                if(head == NULL) throw runtime_error("Top on empty stack");
                return head->data;
        }

        // Tell if the stack is empty.
        bool empty() const { return head == NULL; }

private:
        // Nodes.
        struct Node
        {
        public:
                T data;         // Note: Data field is a whatever.
                Node *next;
                Node(const T & e, Node *n = 0) {
                        data = e;
                        next = n;
                };
        };

        // Head of the list.
        Node *head;
};

// Pop the stack.
template <typename T>
T Stack<T>::pop() {
        if(head == 0) throw runtime_error("Pop on empty stack");

        Node *zombie = head;                    // Save list head.
        head = head->next;                      // Advance the list head.
        T retval = zombie->data;                // Remember data.
        delete zombie;                          // Free the old node.

        return retval;
}

// Note: Templates usually must be in header files.  This is a compiler
// implementation issue (I think).  

#endif

This is a generalized stack class. Note that the methods which check for empty (top and pop) have been modified to throw exceptions instead of return zero. That's because we don't know if 0 is legal value of T, so the return may not be legal.