• C++ 链栈 基本算法实现


    C++ 链栈 基本算法实现

    #ifndef LinkStack_h
    #define LinkStack_h
    #include <iostream>
    template <class T>
    struct Node{
        T data;
        struct Node <T> * next;
    };
    template <class T>
    class LinkStack{
    public:
        LinkStack() {top = NULL;}
        ~LinkStack();
        void Push (T x);
        T Pop();
        T GetTop();
        bool Empty() { return (NULL == top)? true:false;}
    private:
        struct Node <T> *top;
    };
    template <class T>
    void LinkStack<T>::Push (T x){
        struct Node <T> * p = new Node <T>;
        p->data = x;
        p->next = top;
        top = p;
    }
    template <class T>
    T LinkStack<T> :: Pop(){
        if(Empty()) throw "下溢";
        T x = top->data;
        struct Node <T> * p = top;
        top = top->next;
        delete p;
        return x;
    }
    template <class T>
    LinkStack<T>::~LinkStack(){
        while(top){
            struct Node <T> *p = top;
            top = top-> next;
            delete p;
        }
    }
    template <class T>
    T LinkStack<T>::GetTop(){
        if(Empty()) throw "溢出";
        return top->data;
    }
    #endif /* LinkStack_h */
  • 相关阅读:
    下雪诗
    华视身份证阅读器100UC HTTP模式二次开发
    C# Action 和 Func 区别
    网站部署——文件系统
    前端-JavaScript DOM和BOM
    IO多路复用
    python-协程
    python-线程
    python-进程
    计算机与操作系统简介
  • 原文地址:https://www.cnblogs.com/ycbeginner/p/10006402.html
Copyright © 2020-2023  润新知