再读C++ Primer 写了个小例子——实现stack类的压栈弹栈功能(08-12-10)
2008-03-19 22:01
441 查看
再读C++ Primer 写了个小例子——实现stack类的压栈弹栈功能
#pragma once
#define MAX 100
template <class T>
class Stack
...{
public:
Stack(void)
...{
top=0;
total=0;
}
~Stack(void)
...{}
void push(T ele)
...{
if(top <MAX && top>=0)
...{
element[total]=ele;
top++;
total++;
}
}
T pop()
...{
return element[top-1];
}
void empty()
...{
top=0;
total=0;
}
void isEmpty()
...{
if(total==0)
cout<<"是空栈!"<<endl;
else
...{
cout<<"不是空栈!"<<endl;
}
}
protected:
T element[MAX];
int top;
int total;
};
// test02.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Stack.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
...{
int a,b,c;
float a1,b1,c1;
Stack<int> intStack;
cout<<"请输入三个整数"<<endl;
cin>>a>>b>>c;
intStack.push(a);
intStack.push(b);
intStack.push(c);
cout<<"请输入三个浮点数"<<endl;
Stack<float> floatStack;
cin>>a1>>b1>>c1;
floatStack.push(a1);
floatStack.push(b1);
floatStack.push(c1);
cout<<"判断是否为空:"<<endl;
floatStack.isEmpty();
cout<<"pop操作:"<<endl;
cout<<floatStack.pop()<<endl;
cin>>a1;
return 0;
}
实现stack类的压栈弹栈功能
#pragma once
#define MAX 100
template <class T>
class Stack
...{
public:
Stack(void)
...{
top=0;
total=0;
}
~Stack(void)
...{}
void push(T ele)
...{
if(top <MAX && top>=0)
...{
element[total]=ele;
top++;
total++;
}
}
T pop()
...{
return element[top-1];
}
void empty()
...{
top=0;
total=0;
}
void isEmpty()
...{
if(total==0)
cout<<"是空栈!"<<endl;
else
...{
cout<<"不是空栈!"<<endl;
}
}
protected:
T element[MAX];
int top;
int total;
};
// test02.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Stack.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
...{
int a,b,c;
float a1,b1,c1;
Stack<int> intStack;
cout<<"请输入三个整数"<<endl;
cin>>a>>b>>c;
intStack.push(a);
intStack.push(b);
intStack.push(c);
cout<<"请输入三个浮点数"<<endl;
Stack<float> floatStack;
cin>>a1>>b1>>c1;
floatStack.push(a1);
floatStack.push(b1);
floatStack.push(c1);
cout<<"判断是否为空:"<<endl;
floatStack.isEmpty();
cout<<"pop操作:"<<endl;
cout<<floatStack.pop()<<endl;
cin>>a1;
return 0;
}
实现stack类的压栈弹栈功能
相关文章推荐
- 再读C++ Primer 写了个小例子——实现Vector类的自动添加元素功能(08-12-10)
- 再读C++ Primer 写了个小例子(08-01-16)
- 再读C++ Primer 写了个小例子——练习多态虚函数的特性(08-01-25)
- 再读C++ Primer 写了个小例子——运算符重载(08-01-17)
- 再读C++ Primer 写了个小例子——模板的操作(08-01-18)
- 使用EF6和MVC5实现一个简单的选课系统--使用EF6处理并发操作(10/12)
- 用CSS实现类似导航翻转功能例子
- MFC总结(10) ---- MFC中CFileDialog 实现浏览文件和浏览目录功能
- 简单快速开发C\S架构程序用最简单的不分层最快的效率达到功能要求的例子程序FrmCommnetList 所有评论列表的功能实现
- 简单快速开发C\S架构程序用最简单的不分层最快的效率达到功能要求的例子程序FrmKnowledge日积月累功能的实现
- Spring+JMS+使用JMS+ActiveMQ中间件+实现简单聊天功能的小例子
- 简单的一个C++中traits功能的实现例子
- ArcGIS.Server.9.2.DotNet实现EditorTask功能扩展(自带例子 十、三)
- 使用Altiris DS可以实现的哪些功能?[10-1-8]
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 个在 Android 上实现录像和录音功能的小例子。
- 10 实现Django投票的功能
- Android例子源码仿支付宝手势密码的功能实现
- ASP.NET实现License Key输入功能的小例子