zoukankan      html  css  js  c++  java
  • 栈(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;
    }
  • 相关阅读:
    上传文件至服务器(图片)
    centos7语言更改
    centos7无法访问虚拟机web服务
    Java中四种访问权限总结
    线程和进程、程序、应用程序之间的关系
    kafka
    图文并茂理解iptables
    扩展模块
    根据子网掩码计算最大主机数
    iptables匹配条件总结1
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9190105.html
Copyright © 2011-2022 走看看