栈(stack),C++模板实现
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++模板实现的更多相关文章
- C++:栈(stack)的模板类实现
1.基本概念 栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out) 只能在栈顶进行插入和删除操作 压栈(或推入.进栈)即push,将数据放入栈顶并将栈顶指针加一 出栈(或弹 ...
- STL(标准模板库) 中栈(stack)的使用方法
STL 中栈的使用方法(stack) 基本操作: stack.push(x) 将x加入栈stack中,即入栈操作 stack.pop() 出栈操作(删除栈顶),只是出栈,没有返回值 stack.t ...
- 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++ ...
- BSS段 data段 text段 堆heap 和 栈stack
BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配. 数 ...
- [转]JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题: 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(h ...
- 堆heap和栈Stack(百科)
堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...
- (转)Java里的堆(heap)栈(stack)和方法区(method)(精华帖,多读读)
[color=red][/color]<一> 基础数据类型直接在栈空间分配, 方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收. 引用数据类型,需要用new来创建,既在栈 ...
- 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现
本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是 ...
- 栈stack(2):栈的链表实现
定义 从上一篇我们知道,栈(stack)是一个只允许一端进行删除插入操作的线性表.同时,我们联想到线性表的链式结构,其特点是用一组任意的存储单元存储线性表的数据元素,因此我们选择使用链表去实现栈,规定 ...
随机推荐
- 误把Linux运行级别设置为6后的解决方法【转】
本文转载自:http://www.wuji8.com/meta/841011126.html 误把Linux运行级别设置为6后的解决方法 我们知道,Linux有7个运行级别,而运行级别设置为6 ...
- How to create Excel file in C#
http://csharp.net-informations.com/excel/csharp-create-excel.htm Before you create an Excel file in ...
- [SpringBoot] - 一份笔记
一. Spring Boot 入门 1. Spring Boot 简介 简化Spring应用开发的一个框架; 整个Spring技术栈的一个大整合; J2EE开发的一站式解决方案; 2. 微服务 201 ...
- hdu 5652 India and China Origins 并查集+二分
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- python 集合并集
#Union setx = set(["green", "blue"]) sety = set(["blue", "yellow& ...
- pyqt 实现的俄罗斯方块
from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication from PyQt5.QtCore impo ...
- Codeforces 580A - Kefa and First Steps
580A - Kefa and First Steps 思路:dp dp[i]表示包括前i个元素中a[i]在内的最大增序列. 代码: #include<bits/stdc++.h> usi ...
- ThreadLocal实现原理
一.ThreadLocal介绍 这是一个线程的局部变量.也就是说,只有当前线程可以访问.既然是只有当前线程可以访问的数据,自然是线程安全的. 为每一个线程分配不同的对象,需要在应用 ...
- codeforces 578c//Weakness and Poorness// Codeforces Round #320 (Div. 1)
题意:一个数组arr,一个数字x,要使arr-x的最大子段最小,问该最小值. 三分x,复杂度logn,内层是最大子段的模板,只能用n复杂度的.因为是绝对值最大,正负各求一次,取大的.精度卡得不得了,要 ...
- Html显示地图
Html可以通过JS来实现第三方地图的显示,如: 高德: 效果如下:浏览器小区域和全屏展示 代码如下:把key换成自己申请的key值 <script type="text/javasc ...