【c++习题】【17/4/13】stack
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的更多相关文章
- DPDK安装方法 17.12.13
DPDK安装方法 17.12.13 Ubuntu: $ git clone https://github.com/DPDK/dpdk.git $ cd dpdk/ $ export RTE_ARCH= ...
- Django框架下数据存储实现时间戳格式存储到数据库2019-12-11 17:53:13
2019-12-11 17:53:13 models.py class DomainDir(models.Model): date = models.DateTimeField() views.py ...
- SQL表操作习题3 11~13题
- NIOP模拟17.10.13
太水,简述一下题意 T1 让你计算一个形如Σai * bi^ki 快速幂即可 #include <iostream> #include <cstdio> #include &l ...
- 算法(第四版)C# 习题题解——2.1
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...
- Java 集合系列Stack详细介绍(源码解析)和使用示例
Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组实现 ...
- C语言程序设计100例之(17):百灯判亮
例17 百灯判亮 问题描述 有序号为1.2.3.….99.100的100盏灯从左至右排成一横行,且每盏灯各由一个拉线开关控制着,最初它们全呈关闭状态.有100个小朋友,第1位走过来把凡是序号为1的 ...
- Java集合--Stack
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3308852.html 第1部分 Stack介绍 Stack简介 Stack是栈.它的特性是:先进后出(F ...
- torch.cat()和torch.stack()
torch.cat() 和 torch.stack()略有不同torch.cat(tensors,dim=0,out=None)→ Tensortorch.cat()对tensors沿指定维度拼接,但 ...
随机推荐
- 对Linux命令进一步学习vim(二)
今天,进一步学习Linux相关的命令,可能会有重复的地方,但学习本来就是不断重复的过程.故作小记! 1.安装了:vim ,,,一款Linux爱好者经常用到的ide sudo apt-get inst ...
- 【Raspberry pi】set up an ftp server
http://www.debian-administration.org/articles/228 As a means of distributing large collections of fi ...
- 详解ASP.NET提取多层嵌套json数据的方法
本篇文章主要介绍了ASP.NET提取多层嵌套json数据的方法,利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,有兴趣的可以了解一下. 本文实例讲述了ASP.NET利用第三 ...
- 【转】 VC++6.0 在Win7 64位下调试,Shift+F5无法退出
Win7 64位VC++6.0调试代码无法关闭窗口解决方法 VC++6.0 在64位Windows7下调试的时候,再结束调试,程序无法退出,只能关闭VC++6.0 IDE环境. 问题描述:当我击F5开 ...
- 用12个例子全面示范Angular的模板语法
template分支,用12个例子全面示范Angular的模板语法 // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component ...
- Android网络框架-Volley实践 使用Volley打造自己定义ListView
这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0下面)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921.本文出自:[张鸿洋的博客] 1.概述 之前写过一篇博文:Andro ...
- Java每日一题
1.(单选题)What will be printed when you execute the following code? class C { C() { System.out.print(&q ...
- Delphi TreeView – 自动给标题上加图片
Delphi TreeView – 自动给标题上加图片 当处理完TreeView控件树形结构的数据后,根据不同的树形节点Level,加上不同的图片. 图片的ImageList已经放置好,并且TreeV ...
- 51NOD 1013(3的幂的和)
题目链接:传送门 题目大意:求(3^0+3^1+3^2+3^3+...+3^n)%1e9的值 题目思路:乘法逆元裸题 #include <iostream> #include <cs ...