Stack 클래스 템 플 릿

3588 단어 데이터 구조
//-------------DStackT.h----------------  
#include  
  
#ifndef DSTACK  
#define DSTACK  

template
class Stack  
{  
public:  
    Stack(int numElements=128);  
    Stack(const Stack & original);  
    ~Stack();  
    const Stack & operator=(const Stack &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   
#include

template  
Stack::Stack(int numElements)  
{  
    assert(numElements>0);  
    myCapacity=numElements;  
  
    myArray=new (nothrow) StackElement[myCapacity];  
    if(myArray!=0)  
        myTop=-1;  
    else  
    {  
        cout<
Stack::Stack(const Stack & 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<
Stack::~Stack()  
{  
    delete [] myArray;  
}  
  
template
const Stack & Stack::operator=(const Stack & rightHandSide)  
{  
    if(this!=&rightHandSide)  
    {  
        if(myCapacity!=rightHandSide.myCapacity)  
        {  
            delete [] myArray;  
  
            myCapacity=rightHandSide.myCapacity;  
            myArray=new (nothrow) StackElement[myCapacity];  
            if(myArray==0)  
            {  
                cerr<
bool Stack::empty() const  
{  
    return (myTop==-1);  
}  
  
template
void Stack::push(const StackElement & value)  
{  
    if(myTop
void Stack::display(ostream & out) const  
{  
    for(int i=myTop;i>=0;i--)  
        out<
inline ostream & operator<& st)
{
	st.display(out);
	return out;
}

template
StackElement Stack::top() const  
{  
    if(!empty())  
    {  
        return (myArray[myTop]);  
    }  
    else  
    {  
        cerr<
void Stack::pop()  
{  
    if(!empty())  
        myTop--;  
    else  
        cerr<
//-------------DStackT_main.cpp---------------------  
#include  
#include
using namespace std;  
  
#include"DStackT.h"  

template
void print(Stack st)  
{ st.display(cout); }  
  
int main()  
{  
    int cap;  
    cout<>cap;  
  
	Stack intSt;
	Stack charSt;
	for(int i=1;i<=4;i++)  
      intSt.push(100*i);  
	cout<t;
	t=intSt;
	cout<

좋은 웹페이지 즐겨찾기