又一次造轮子-C++栈(泛型编程)
来源:互联网 发布:梦幻西游 for mac 编辑:程序博客网 时间:2024/05/29 14:23
栈,一种简单的数据结构,再造一次轮子
<<Stack.h>>
#if ! defined STACK_H_#define STACK_H_template<class Type>class Stack{private: struct LinkStack { Type ele; LinkStack *next,*previous; }; LinkStack *linkstack,*newnode; int size;public: Stack(); ~Stack(); Type Top(); void Push(Type e); void Pop(); //为空时特殊处理 int Size(); bool isEmpty();};#endif
<<Stack.cpp>>
#include<cstdlib>#include "Stack.h"template<class Type>int Stack<Type>::Size(){ return size;}template<class Type>bool Stack<Type>::isEmpty(){ return !size;}template<class Type>Stack<Type>::Stack(){ size = 0;}template<class Type>Type Stack<Type>::Top(){ return linkstack->ele;}template<class Type>void Stack<Type>::Push(Type e){ newnode = new LinkStack; newnode->ele = e; if(size) { newnode->previous = linkstack; } linkstack = newnode; ++size;}template<class Type>void Stack<Type>::Pop(){ if(size && size != 1) { newnode = linkstack; linkstack = linkstack->previous; delete[] newnode; } else if(size==1) { delete[] linkstack; } --size;}template<class Type>Stack<Type>::~Stack(){ while(size) { Pop(); }}<<TestStack.cpp>
#include "Stack.cpp"#include<iostream>using namespace std;struct A{ int x,y;};class B{public: int x,y;};int C;int main(){ Stack<A> sta1; Stack<B> sta2; Stack<int> sta3; A a; for(int i=0;i<10;i++) { a.x = a.y = i; sta1.Push(a); } cout<<"A:"<<endl; cout<<sta1.isEmpty()<<endl; cout<<sta1.Size()<<endl; cout<<"Test A"<<endl; for(int i=0;i<10;i++) { A temp = sta1.Top(); cout<<"x:"<<temp.x<<" y:"<<temp.y<<endl; sta1.Pop(); } cout<<"A:"<<endl; cout<<sta1.isEmpty()<<endl; cout<<sta1.Size()<<endl; B b; for(int i=0;i<10;i++) { b.x = b.y = i; sta2.Push(b); } cout<<"B:"<<endl; cout<<sta2.isEmpty()<<endl; cout<<sta2.Size()<<endl; cout<<"Test B"<<endl; for(int i=0;i<10;i++) { B temp = sta2.Top(); cout<<"x:"<<temp.x<<" y:"<<temp.y<<endl; sta2.Pop(); } cout<<"B:"<<endl; cout<<sta2.isEmpty()<<endl; cout<<sta2.Size()<<endl; cout<<"Test C"<<endl; for(int i=0;i<10;i++) { sta3.Push(i); } cout<<"C:"<<endl; cout<<sta3.isEmpty()<<endl; cout<<sta3.Size()<<endl; for(int i=0;i<10;i++) { cout<<sta3.Top()<<endl; sta3.Pop(); } cout<<"C:"<<endl; cout<<sta3.isEmpty()<<endl; cout<<sta3.Size()<<endl;}
1 0
- 又一次造轮子-C++栈(泛型编程)
- web: _show -> _info 造轮子编程
- 重造轮子-最小栈
- 关于轮子跟造轮子。
- 其实,学习编程的过程就是学习造轮子的过程,还是拥抱造轮子吧
- C++,又一次美妙的旅程
- 又一次
- 又一次
- C++编程 - 真的不要重复造轮子吗
- 重造轮子-栈实现队列
- 重复造轮子-C语言风格string库函数的实现
- Python 造轮子/现有轮子 相关资料
- 值得推荐的C/C++框架和库—造轮子前先看看现有的轮子
- 开始造轮子
- 不要重复造轮子
- 谈谈自己造轮子
- 不要重复造轮子
- 不再重复造轮子
- 设计模式之桥接模式
- UVA 10791 Minimum Sum LCM(质因子问题)
- vlc代码分析(4)——mpgv的demux
- 2016"百度之星" - 资格赛 Problem D
- Android如何将图片上传到七牛云存储
- 又一次造轮子-C++栈(泛型编程)
- Storyboard学习六(ScrollView)
- volatile关键字
- bzoj 3571: [Hnoi2014]画框 最优乘积匹配
- bzoj3571: [Hnoi2014]画框
- VLC源码分析总结
- 第八周项目一(3)数组做数据成员
- 登陆界面
- Linux VFS虚拟文件系统