栈类

 

 

#define MAX_SIZE 20

class Stack {
private:
    int count;
    int capacity;
    int data[MAX_SIZE];

public:
    Stack();
    ~Stack();
    void push(const int& value);
    int top() const;
    void pop();
    int size() const;
    bool full() const;
    bool empty() const;
    void clear();

};
Stack::Stack()
    : count(0),capacity(MAX_SIZE) {

}

Stack::~Stack(){
    clear();
}

void Stack::push(const int& value) {
    if(!full()) {
        data[count++] = value;
    }
}

int Stack::top() const {
    if (!empty()) {
        return data[count-1];
    }
}

void Stack::pop() {
    if (!empty()) {
        count--;
    }
}

int Stack::size() const {
    return count;
}

bool Stack::full() const {
    return (size() == capacity ? true : false);
}

bool Stack::empty() const {
    return (size() == 0 ? true : false);
}

void Stack::clear() {
    count = 0;
}

 

posted @ 2013-09-08 19:43  StrikeW  阅读(178)  评论(0编辑  收藏  举报