/**
stack.h
*****/
#include <iostream>
using namespace std;
template <class T>
class Stack
{
public:
Stack(int maxsize = 10);
~Stack(){delete stack;}
T Top();
void push(const T & x);
void pop();
bool empty();
private:
int top;
T * stack;
int maxtop;
};
/**
stack.cpp
**/
#include "stack.h"
template <class T>
Stack::Stack(int maxsize)
{
maxtop = maxsize;
top = -1;
stack = new T[maxsize];
}
template <class T>
bool Stack::empty()
{
if (top == -1)
{
return true;
}
return false;
}
template <class T>
T Stack::top()
{
return stack[top];
}
template <class T>
void Stack::push (const T & x)
{
top++;
stack[top] = x;
}
template <class T>
void Stack::pop()
{
top--;
}
原文地址:https://blog.51cto.com/wenxuehui/2368519
时间: 2024-10-12 09:13:16