stack.h (涂鸦作)
2006-10-07 11:02 老博客哈 阅读(705) 评论(0) 收藏 举报
 #pragma once
#pragma once
 #include <new>
#include <new> #include <stdexcept>
#include <stdexcept>

 template<typename T, int size = 100>
template<typename T, int size = 100> class stack
class stack {
{ public:
public: stack();
    stack(); ~stack();
    ~stack(); void push(const T & element);
    void push(const T & element); T    top() const;
    T    top() const; bool empty() const;
    bool empty() const; void pop();
    void pop(); private:
private: T* data;
    T* data; int currentsize;
    int currentsize; };
};
 template<typename T, int size>
template<typename T, int size> inline stack<T, size>::stack()
inline stack<T, size>::stack() {
{ data = new T[size];
    data = new T[size]; currentsize = -1;
    currentsize = -1; if( data == NULL )
    if( data == NULL ) {
    { throw std::bad_alloc();
        throw std::bad_alloc(); }
    } }
}
 template<typename T, int size>
template<typename T, int size> inline stack<T, size>::~stack()
inline stack<T, size>::~stack() {
{ delete []data;
    delete []data; }
}
 template<typename T, int size>
template<typename T, int size> inline void stack<T, size>::push(const T &element)
inline void stack<T, size>::push(const T &element) {
{ if( currentsize == size - 1)
    if( currentsize == size - 1) throw std::overflow_error( "stack push overflow" );
        throw std::overflow_error( "stack push overflow" ); else
    else data[++currentsize] = element;
        data[++currentsize] = element; }
}
 template<typename T, int size>
template<typename T, int size> inline bool stack<T, size>::empty() const
inline bool stack<T, size>::empty() const {
{ return currentsize == -1;
    return currentsize == -1; }
}
 template<typename T, int size>
template<typename T, int size> inline T stack<T, size>::top() const
inline T stack<T, size>::top() const {
{ if( empty() )
    if( empty() ) throw std::range_error("stack top range error");
        throw std::range_error("stack top range error"); else
    else return data[currentsize];
        return data[currentsize]; }
}
 
     template<typename T, int size>
template<typename T, int size> inline void stack<T, size>::pop()
inline void stack<T, size>::pop() {
{ if( empty() )
    if( empty() ) throw std::range_error("stack pop range error");
        throw std::range_error("stack pop range error"); else
    else --currentsize;
        --currentsize; }
}写着玩玩的, 当真用偶还是去用STL的!
 
                     
                    
                 
                    
                

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号