1、stack 模板、动态内存分配、析构

#include "stack2.cpp"

#include <iostream>
using namespace std; int main()
{
// 测试int型
Stack<int> s1();
s1.push();
s1.push();
s1.push();
s1.push();
s1.push();
if(!s1.isEmpty()) cout << s1.pop() << endl;
cout << s1.pop() << endl;
cout << s1.pop() << endl;
cout << s1.pop() << endl;
cout << s1.pop() << endl;
cout << endl; // 测试char型
Stack<char> s2();
if(!s2.isFull()) s2.push('a');
s2.push('b');
s2.push('c');
s2.push('d');
s2.push('e');
s2.push('f');
cout << s2.pop() << endl;
cout << s2.pop() << endl;
cout << s2.pop() << endl;
cout << s2.pop() << endl;
cout << s2.pop() << endl;
//cout << s2.pop() << endl;
return ;
} template <class T>
class Stack {
private:
T *sptr;
int size;
int Top;
public:
// 构造器
Stack()
{
sptr = new T[];
size = ;
Top = ;
}
Stack(int n)
{
sptr = new T[n];
size = n;
Top = ;
}
// 析构函数
~Stack()
{
delete []sptr;
}
// push
void push(T i)
{
*(sptr++) = i;
++Top;
}
// pop
T pop()
{
return *(--sptr);
--Top;
}
bool isEmpty()
{
if (Top == )
return true;
return false;
}
bool isFull()
{
if (Top < size)
return false;
return true;
}
};

【c++习题】【17/4/13】stack的更多相关文章

  1. DPDK安装方法 17.12.13

    DPDK安装方法 17.12.13 Ubuntu: $ git clone https://github.com/DPDK/dpdk.git $ cd dpdk/ $ export RTE_ARCH= ...

  2. Django框架下数据存储实现时间戳格式存储到数据库2019-12-11 17:53:13

    2019-12-11 17:53:13 models.py class DomainDir(models.Model): date = models.DateTimeField() views.py ...

  3. SQL表操作习题3 11~13题

  4. NIOP模拟17.10.13

    太水,简述一下题意 T1 让你计算一个形如Σai * bi^ki 快速幂即可 #include <iostream> #include <cstdio> #include &l ...

  5. 算法(第四版)C# 习题题解——2.1

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  6. Java 集合系列Stack详细介绍(源码解析)和使用示例

    Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组实现 ...

  7. C语言程序设计100例之(17):百灯判亮

    例17   百灯判亮 问题描述 有序号为1.2.3.….99.100的100盏灯从左至右排成一横行,且每盏灯各由一个拉线开关控制着,最初它们全呈关闭状态.有100个小朋友,第1位走过来把凡是序号为1的 ...

  8. Java集合--Stack

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3308852.html 第1部分 Stack介绍 Stack简介 Stack是栈.它的特性是:先进后出(F ...

  9. torch.cat()和torch.stack()

    torch.cat() 和 torch.stack()略有不同torch.cat(tensors,dim=0,out=None)→ Tensortorch.cat()对tensors沿指定维度拼接,但 ...

随机推荐

  1. 【转】 VC++6.0 在Win7 64位下调试,Shift+F5无法退出

    Win7 64位VC++6.0调试代码无法关闭窗口解决方法 VC++6.0 在64位Windows7下调试的时候,再结束调试,程序无法退出,只能关闭VC++6.0 IDE环境. 问题描述:当我击F5开 ...

  2. jenkins配置svn、gradle、ssh

    1.先说下实现的效果,从svn拉取代码.调用gradle编译构建.将构建包分发到部署服务器并备份原来的部署包: 2.直接从http://mirrors.jenkins-ci.org/war/lates ...

  3. MySQL设置密码的三种方法

    其设置密码有三种方法: a. ./mysqladmin -u root -p oldpassword newpasswd(记住这个命令是在/usr/local/mysql/bin中外部命令) b. S ...

  4. python之range和xrange

    range 前面小节已经说明了,range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列. 比如: 1 >>> ...

  5. Python 基础之列表去重的几种玩法

    列表去重 1.方法1 借助一个临时列表 ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ids: if id not in news_ids ...

  6. 系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing)

    系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing) UAT SIT在前,UAT在后,UAT测完才可以上线

  7. 【BZOJ4547】Hdu5171 小奇的集合 矩阵乘法

    [BZOJ4547]Hdu5171 小奇的集合 Description 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大值.(数据保证这 ...

  8. 【BZOJ3144】[Hnoi2013]切糕 最小割

    [BZOJ3144][Hnoi2013]切糕 Description Input 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q ...

  9. 170111、MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  10. CodeForces 156A Message(暴力)

    A. Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...