zoukankan      html  css  js  c++  java
  • C++泛化栈

    栈是一种先进后出的数据结构(FILO),这里使用之前实现的动态数组实现。

    栈(stack.h)

    /*************************************************************************
    > File Name       : Stack.h
    > Author          : Harold
    > Mail            : 2106562095@qq.com
    > Github          : www.github.com/Haroldcc
    > Created Time    : 2020年03月03日  15时00分16秒
    ************************************************************************/
    
    #ifndef STACK_H_
    #define STACK_H_
    
    #include "arrayList.h"
    #include <iostream>
    
    /***** 栈 *****/
    
    /* 栈是一种特殊的链表结构,
     * 这里直接使用数组链表提供实现
     */
    template <typename T>
    class Stack
    {
    private:
        arrayList<T> list;
    
    public:
        ~Stack() {}
        int size() const;
        bool empty() const;
        void push(const T &element); // 入栈
        void pop();                  // 出栈
        T &top();                    // 获取栈顶元素
        void clear();
    
        //void output(std::ostream &out);
    };
    
    template <typename T>
    int Stack<T>::size() const
    {
        return list.size();
    }
    
    template <typename T>
    bool Stack<T>::empty() const
    {
        return list.isEmpty();
    }
    
    template <typename T>
    void Stack<T>::push(const T &element)
    {
        list.add(element);
    }
    
    template <typename T>
    void Stack<T>::pop()
    {
        list.removeByIndex(list.size() - 1);
    }
    
    template <typename T>
    T &Stack<T>::top()
    {
        return list.getElement(list.size() - 1);
    }
    
    template <typename T>
    void Stack<T>::clear()
    {
        list.clear();
    }
    
    #endif
    

    测试(testStack.cpp)

    /*************************************************************************
    > File Name       : testStack.cpp
    > Author          : Harold
    > Mail            : 2106562095@qq.com
    > Github          : www.github.com/Haroldcc
    > Created Time    : 2020年03月03日  15时14分31秒
    ************************************************************************/
    #include <iostream>
    #include "Stack.h"
    
    int main()
    {
        Stack<int> stack;
    
        stack.push(11);
        stack.push(22);
        stack.push(33);
        stack.push(44);
    
        std::cout << stack.top() << std::endl;
        stack.pop();
    
        std::cout << stack.top() << std::endl;
        stack.pop();
    
        std::cout << stack.top() << std::endl;
        stack.pop();
    
        std::cout << stack.top() << std::endl;
        stack.pop();
    
        return 0;
    }
    
    

    输出

    44
    33
    22
    11
    
  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/HaroldC/p/12437409.html
Copyright © 2011-2022 走看看