Stack类模板
来源:互联网 发布:sql语句编写 编辑:程序博客网 时间:2024/06/06 03:42
//-------------DStackT.h---------------- #include<iostream> #ifndef DSTACK #define DSTACK template<typename StackElement>class Stack { public: Stack(int numElements=128); Stack(const Stack<StackElement> & original); ~Stack(); const Stack & operator=(const Stack<StackElement> &rightHandSide); bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop(); private: int myTop; int myCapacity; StackElement *myArray; }; #include<new> #include<cassert>template<typename StackElement> Stack<StackElement>::Stack(int numElements) { assert(numElements>0); myCapacity=numElements; myArray=new (nothrow) StackElement[myCapacity]; if(myArray!=0) myTop=-1; else { cout<<"Inadequate memory to allocate stack \n"; exit(1); } } template<typename StackElement>Stack<StackElement>::Stack(const Stack<StackElement> & original) :myCapacity(original.myCapacity),myTop(original.myTop) { myArray=new (nothrow) StackElement[myCapacity]; if(myArray!=0) for(int pos=0;pos<=myTop;pos++) myArray[pos]=original.myArray[pos]; else { cerr<<"Inadequate memory to allocate stack ***\n"; exit(1); } } template<typename StackElement>Stack<StackElement>::~Stack() { delete [] myArray; } template<typename StackElement>const Stack<StackElement> & Stack<StackElement>::operator=(const Stack<StackElement> & rightHandSide) { if(this!=&rightHandSide) { if(myCapacity!=rightHandSide.myCapacity) { delete [] myArray; myCapacity=rightHandSide.myCapacity; myArray=new (nothrow) StackElement[myCapacity]; if(myArray==0) { cerr<<"*** Inadequate memory ***\n"; exit(1); } } myTop=rightHandSide.myTop; for(int pos=0;pos<=myTop;pos++) myArray[pos]=rightHandSide.myArray[pos]; } return *this; } template<typename StackElement>bool Stack<StackElement>::empty() const { return (myTop==-1); } template<typename StackElement>void Stack<StackElement>::push(const StackElement & value) { if(myTop<myCapacity-1) { myTop++; myArray[myTop]=value; } else { cerr<<"***Stack full -- can't add new value ***\n"; exit(1); } } template<typename StackElement>void Stack<StackElement>::display(ostream & out) const { for(int i=myTop;i>=0;i--) out<<myArray[i]<<endl; } template<typename StackElement>inline ostream & operator<<(ostream & out,const Stack<StackElement>& st){st.display(out);return out;}template<typename StackElement>StackElement Stack<StackElement>::top() const { if(!empty()) { return (myArray[myTop]); } else { cerr<<"***Stack is empty -- returning garbage value ***" <<endl; StackElement garbage; return garbage; } } template<typename StackElement>void Stack<StackElement>::pop() { if(!empty()) myTop--; else cerr<<"*** Stack is empty -- can't remove a value ***\n"; } #endif
//-------------DStackT_main.cpp--------------------- #include<iostream> #include<iomanip>using namespace std; #include"DStackT.h" template<typename T>void print(Stack<T> st) { st.display(cout); } int main() { int cap; cout<<"Enter stack capacity: "; cin>>cap; Stack<int> intSt;Stack<char> charSt;for(int i=1;i<=4;i++) intSt.push(100*i); cout<<intSt<<endl;for(char ch='A';ch<='D';ch++) charSt.push(ch); cout<<charSt<<endl;cout<<"Content of stacks intSt (via print):\n"; print(intSt); cout<<endl; Stack<int>t;t=intSt;cout<<"Content of stacks t after t=stInt (via print):\n"; print(t); cout<<endl; cout<<"Stack t empty? "<<boolalpha<<t.empty()<<endl; cout<<"Top value in t: "<<t.top()<<endl; while(!t.empty()) { cout<<"Popping t: "<<t.top()<<endl; t.pop(); } cout<<"Stack t empty? "<<t.empty()<<endl; cout<<"\nNow try to retrieve top value from t."<<endl; cout<<"Top value in t: "<<t.top()<<endl; cout<<"\nTrying to pop t: "<<endl; t.pop(); }
阅读全文
1 0
- c++ stack模板类
- 类模板stack小试牛刀
- 模板类实现stack
- 模板类stack
- Stack类模板
- 类模板 stack
- Stack--C++模板类
- C++stack模板类
- Stack类模板
- C++之Stack模板类
- 用类模板改写stack
- Stack模板
- C++ Templates笔记 4 类模板Stack
- STL<list stack queue 模板类>
- 模板类 stack 的理解 积累
- 自己实现的stack模板类
- C++之模板类stack实例
- C++:栈(stack)的模板类实现
- 递归入门
- MVP实现RecyclerView
- 012-题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- SQL笔记
- VS 2013密匙
- Stack类模板
- HDU
- 4590: [Shoi2015]自动刷题机
- Android surfaceflinger (4) -Vsync产生上报流程
- JVM内幕:Java虚拟机详解
- 【spring源码分析】加载bean过程(1)
- Java NIO系列教程(2):Channel
- Scrapy框架爬取有验证码的登录网站
- Aerospike迁移