• 栈(stack),C++模板实现


    栈:(stack.h)

    #ifndef __STACK_H__
    #define __STACK_H__
    #include <iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    template <typename T,int num>
    class Stack
    {
            private:
                    int _top;
                    T _parr[num];
            public:
                    Stack();
                    ~Stack();
                    bool full();
                    bool empty();
                    bool push(T elem);
                    bool pop(T &);
                    int getPos()
                    {
                            return _top;
                    }
    };

    template <typename T,int num>
    Stack<T,num>::Stack():_top(-1)
    {}
    template <typename T,int num>
    Stack<T,num>::~Stack()
    {}
    template <typename T,int num>
    bool Stack<T,num>::full()
    {
            return _top == (num-1);
    }
    template <typename T,int num>
    bool Stack<T,num>::empty()
    {
            return _top == -1;
    }
    template <typename T,int num>
    bool Stack<T,num>::push(T elem)
    {
            if(!full())
            {
                    _parr[++_top] = elem;
                    return true;
            }
            return false;
    }
    template <typename T,int num>
    bool Stack<T,num>::pop(T & t)
    {
            if(!empty())
            {
                    t = _parr[_top--];
                    return true;
            }
            else
                    return false;
    }
    #endif
    测试代码(testStack.cpp)

    #include"stack.h"
    int test0(void)
    {      
            Stack<int, 10> stackInt;
            cout << "开始时stakcInt是否为空?" << stackInt.empty() << endl;
            stackInt.push(5);
            cout << "此始时stakcInt是否为空?" << stackInt.empty() << endl;

            for(int idx = 1; idx !=10; ++idx)
            {
                    stackInt.push(idx);
            }
            cout << "此时stakcInt是否已满?" << stackInt.full() << endl;

            for(int idx = 0; idx != 10; ++idx)
            {
                    int elem = 0;
                    stackInt.pop(elem);
                    cout << elem << " ";
            }
            cout << endl;
            return 0;
    }
    int test1(void)
    {      
            Stack<string, 10> stackInt;
            cout << "开始时stakcInt是否为空?" << stackInt.empty() << endl;
            stackInt.push("aa");
            cout << "此始时stakcInt是否为空?" << stackInt.empty() << endl;

            for(int idx = 1; idx !=10; ++idx)
            {
                    string s(2, 'a' + idx);  
    //string类的一个构造函数,表示含有2个元素的string对象,其中每个元素都初始化为后面的字符
                    stackInt.push(s);
            }
            cout << "此时stakcInt是否已满?" << stackInt.full() << endl;

            for(int idx = 0; idx != 10; ++idx)
            {
                    string elem;
                    stackInt.pop(elem);
                    cout << elem << " ";
            }
            cout << endl;
            return 0;
    }
    int main()
    {
            test0();
            test1();
            return 0;
    }
  • 相关阅读:
    noip模拟赛 集合
    noip模拟赛 旅行
    noip模拟赛 终末
    noip模拟赛 少女
    noip模拟赛 无题
    Java基础知识强化73:正则表达式之分割功能
    Java基础知识强化72:正则表达式之判断功能(手机号码判断 和 校验邮箱)
    Java基础知识强化71:正则表达式之基本规则 和 常用正则表达式
    Java基础知识强化70:正则表达式之引入案例(QQ号码校验)
    Java基础知识强化69:基本类型包装类之Character案例(统计字符串中大写小写以及数字的次数)
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9190105.html
Copyright © 2020-2023  润新知