首页 > 编程语言 > 详细

c++模板类堆栈

时间:2014-02-26 00:41:45      阅读:341      评论:0      收藏:0      [点我收藏+]

#ifndef __MyConsole__Stack__

#define __MyConsole__Stack__

 

#include <iostream>

template<class T>

class Stack{

public:

    explicit Stack(int size);

    ~Stack();

    bool IsEmpty();

    int CurrentSize();

    bool Push(const T& item);

    bool IsFull();

    void ShowStack();

    bool Pop(T *pResult);

private:

    int top;

    int maxSize;

    T *data;

    

    

};

 

 

using std::cout;

using std::cin;

using std::endl;

 

template<class T>

Stack<T>::Stack(int size){

       top = 0;

        maxSize = size;

        data = new T[maxSize];

}

template<class T>

Stack<T>::~Stack(){

    delete []data;

}

template<class T>

bool Stack<T>::IsEmpty(){

    

    return top == 0;

}

 

template<class T>

int Stack<T>::CurrentSize(){

    return top;

}

 

template<class T>

bool Stack<T>::Push(const T& item){

    if (IsFull()) {

        returnfalse;

        

    }

    

    data[top++] = item;

    returntrue;

}

 

template<class T>

bool Stack<T>::Pop(T *pResult)

{

    

    if (IsEmpty()) {

        returnfalse;

    }

    

    

    *pResult = data[--top];

    

    returntrue;

}

 

 

template<class T>

bool Stack<T>::IsFull(){

    return top == maxSize;

}

template<class T>

void Stack<T>::ShowStack(){

    cout << "Some informations about Stack as follows:" << endl;

    cout << "CurrentSize = " << top << " MaxSize = " << maxSize << endl;

    for (int i = 0; i < top; ++i) {

        cout << data[i] << " ";

        if (i % 4 == 0 && i != 0) {

            cout << endl;

        }

    }

    

    cout << endl;

}

 

#endif /* defined(__MyConsole__Stack__) */

c++模板类堆栈

原文:http://www.cnblogs.com/ganquanfu2008/p/3566902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!