body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

栈:(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;
}

栈(stack),C++模板实现的更多相关文章

  1. C++:栈(stack)的模板类实现

    1.基本概念 栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out) 只能在栈顶进行插入和删除操作 压栈(或推入.进栈)即push,将数据放入栈顶并将栈顶指针加一 出栈(或弹 ...

  2. STL(标准模板库) 中栈(stack)的使用方法

    STL 中栈的使用方法(stack) 基本操作: stack.push(x)  将x加入栈stack中,即入栈操作 stack.pop()  出栈操作(删除栈顶),只是出栈,没有返回值 stack.t ...

  3. C++ Templates (2.2 使用Stack类模板 Use of Class Template Stack )

    返回完整目录 目录 2.2 使用Stack类模板 Use of Class Template Stack 2.2 使用Stack类模板 Use of Class Template Stack 在C++ ...

  4. BSS段 data段 text段 堆heap 和 栈stack

    BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配.   数 ...

  5. [转]JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )

    这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题: 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(h ...

  6. 堆heap和栈Stack(百科)

    堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...

  7. (转)Java里的堆(heap)栈(stack)和方法区(method)(精华帖,多读读)

    [color=red][/color]<一> 基础数据类型直接在栈空间分配, 方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收.   引用数据类型,需要用new来创建,既在栈 ...

  8. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

  9. 栈stack(2):栈的链表实现

    定义 从上一篇我们知道,栈(stack)是一个只允许一端进行删除插入操作的线性表.同时,我们联想到线性表的链式结构,其特点是用一组任意的存储单元存储线性表的数据元素,因此我们选择使用链表去实现栈,规定 ...

随机推荐

  1. 论文笔记之:Continuous Deep Q-Learning with Model-based Acceleration

    Continuous Deep Q-Learning with Model-based Acceleration 本文提出了连续动作空间的深度强化学习算法. 开始正文之前,首先要弄清楚两个概念:Mod ...

  2. 论文笔记之:Dynamic Label Propagation for Semi-supervised Multi-class Multi-label Classification ICCV 2013

    Dynamic Label Propagation for Semi-supervised Multi-class Multi-label Classification ICCV 2013 在基于Gr ...

  3. 大数字运算, BigInteger

    package com.ykmimi.test1; import java.math.BigInteger; /** * 大数字运算 * @author ukyor * */ public class ...

  4. AtCoder Grand Contest 013 C :Ants on a Circle

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. ubuntu16.04上安装Jenkins,获取登陆密码

    sudo cat /usr/share/tomcat7/.jenkins/secrets/initialAdminPassword

  6. 禁用表单元素 && 禁止选中

    一.禁用表单元素 1.dom设置属性 disabled="disabled" || disabled=true 2.css样式(高版本浏览器) pointer-events:non ...

  7. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  8. English trip V1 - 4.Do you have it? Teacher:Patrick Key: have - has doesn't have

    In this lesson you will learn to describe what you have. STARTER Do you have a ...?  # 你有...吗? car b ...

  9. EFS 你应该知道的事

    需要备份或者还保留这个路径 %USERPROFILE%\AppData\Roaming\Microsoft\Crypto\RSA certmgr.msc 个人证书导出你开始使用EFS加密后的证书 ci ...

  10. 12月21日 简单理解Active Recore Callback, destroy_all和delete_all的区别。豆知识(alias),语言学习法(4核心)

    destroy_all and delete_all Destroy the records by instantiating each record and calling its #destroy ...