lbcjbcgn 2022-10-07 15:09 采纳率: 75%
浏览 75
已结题

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

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

  • 写回答

2条回答 默认 最新

  • _GX_ 2022-10-07 15:40
    关注
    #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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 来灵 2022-10-07 15:42
    关注

    运行结果截图:

    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");
    }
    
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月7日
  • 已采纳回答 10月7日
  • 创建了问题 10月7日

悬赏问题

  • ¥15 Linux操作系统中的,管道通信问题
  • ¥15 请问这张multisim图的原理是什么,这是一个交通灯,是课程要求,明天要进行解析,但是我们组没一个人会,所以急要,今天要
  • ¥15 ansible tower 卡住
  • ¥15 等间距平面螺旋天线方程式
  • ¥15 通过链接访问,显示514或不是私密连接
  • ¥100 系统自动弹窗,键盘一接上就会
  • ¥50 股票交易系统设计(sql语言)
  • ¥15 调制识别中这几个数据集的文献分别是什么?
  • ¥15 请大家看看报错原因,为啥会这样
  • ¥30 Cocos转UWP应用,支付调起后闪退