c++栈的类模板,提供栈结构数据操作

以下是一个整数栈类的定义:
const int SIZE=100;
class Stack
{
public:
Stack();
~Stack();
void Push(int n);
int Pop();
private:
int stack[SIZE];
int top;
};
编写一个栈的类模板(包括其成员函数定义),以便为任何类型的对象提供栈结构数据操作。并在主函数中测试创建整数栈、字符栈和浮点数栈,并提供一些数据展示入栈、出栈和打印操作。

#include <iostream>
#include <stdexcept>

const int SIZE = 100;

template <typename T>
class Stack
{
public:
    Stack() : top(0) {}
    ~Stack() {}

    void Push(int n)
    {
        if (top == SIZE)
            throw std::runtime_error("stack is full");
        stack[top++] = n;
    }

    int Pop()
    {
        if (top == 0)
            std::runtime_error("stack is empty");
        return stack[--top];
    }

    void Print()
    {
        for (int i = 0; i < top; i++)
            std::cout << stack[i] << " ";
        std::cout << '\n';
    }

private:
    T stack[SIZE];
    int top;
};

template <typename T>
void test_stack()
{
    std::cout << "test for " << typeid(T).name() << '\n';
    Stack<T> si;
    for (int i = 'a'; i <= 'd'; i++)
        si.Push(i);
    si.Print();
    si.Pop();
    si.Print();
}

int main()
{
    try
    {
        test_stack<int>();
        test_stack<char>();
        test_stack<float>();
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what();
    }
}

运行结果截图:

img

参考代码如下,希望能帮助到您:



#include <iostream>
#include <cassert>

using namespace std;

template<class T, int SIZE = 100>
class Stack
{
public:
    Stack();                 //构造函数
    void push(const T& item);//压入栈
    T pop();                 //出栈
    void clear();            //清空栈
    int stackLength();         //长度
    const T& peek() const;   //读取栈元素
    bool isEmpty() const;    //判断是否为空
    bool isFull() const;     //判断是否为满
    void stackTranverse();//遍历栈
private:
    T list[SIZE];            //存放元素
    int top;                 //指向栈顶的指针
};
//模板的实现
template <class T, int SIZE>
Stack<T, SIZE>::Stack() :top(-1)//在建立一个栈的时候是没有元素的,在放入第一个元素后top才会变为0
{

}
template <class T, int SIZE>
void Stack<T, SIZE>::push(const T& item)
{
    assert(!isFull());
    list[++top] = item;//先加后压入
}
template <class T, int SIZE>
T Stack<T, SIZE>::pop()
{
    assert(!isEmpty());
    return list[top--];//先弹出后减
}
template <class T, int SIZE>
const T& Stack<T, SIZE>::peek() const
{
    assert(!isEmpty());
    return list[top];//返回栈顶元素
}
template <class T, int SIZE>
int Stack<T, SIZE>::stackLength()
{//栈长度
    return top;
}
template <class T, int SIZE>
bool Stack<T, SIZE>::isEmpty() const
{
    return top == -1;
}
template <class T, int SIZE>
bool Stack<T, SIZE>::isFull() const
{
    return top == SIZE - 1;
}
template <class T, int SIZE>
void Stack<T, SIZE>::clear()
{
    top = -1;
}
template <class T, int SIZE>
void Stack<T, SIZE>::stackTranverse() 
{//遍历栈
    int i = 0;
    for (i = 0; i < top; i++) 
    {
        cout << list[i];
    }
}

//坐标点类Coordinate
class Coordinate
{
public:
    friend ostream& operator<<(ostream& out, Coordinate& coor);
    Coordinate(int x = 0, int y = 0)
    {
        ix = x;
        iy = y;
    }

    ~Coordinate()
    {
    }
private:
    int ix;
    int iy;
};
ostream& operator<<(ostream& out, Coordinate& coor) 
{
    out << "(" << coor.ix << "," << coor.iy << ")" << endl;
    return out;
}


int main()
{
    Stack<Coordinate>* pStack = new Stack<Coordinate>();

    //坐标点入栈
    pStack->push(Coordinate(3, 5));
    pStack->push(Coordinate(7, 5));
    pStack->push(Coordinate(6, 5));
    pStack->push(Coordinate(4, 5));

    pStack->stackTranverse();//遍历栈

    Coordinate t;
    t = pStack->pop();//出栈
    cout << "弹出的t为:" << t;
    cout << "长度:" << pStack->stackLength() << endl;
    pStack->clear();//清空栈
    pStack->stackTranverse();

    //delete pStack;
    //pStack = NULL;

    system("pause");
}