Stack类模板

//-------------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();  
}  

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值