Stack class

const int max_stack = 10;
typedef int Stack_entry; // Now, stack data type is int

enum Error_code {
    succeed,
    overflow,
    underflow
};

class Stack{
public:
    Stack();
    Stack(const Stack & rhs);
    ~Stack();

    Stack & operator=(const Stack & rhs); // return Stack& for a=b=c

    bool empty() const;
    Error_code top(Stack_entry & item) const;
    Error_code pop();
    Error_code push(const Stack_entry & item);

private:
    int count;
    Stack_entry entry[max_stack];
};

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

Stack::Stack(const Stack & rhs) : count(rhs.count) {
    for (int i=0; i<rhs.count; i++) {
        entry[i] = rhs.entry[i];
    }
}

Stack::~Stack() {

}

Stack & Stack::operator=(const Stack & rhs) {
    count = rhs.count;
    for (int i=0; i<count; i++) {
        entry[i] = rhs.entry[i];
    }

    return (*this);
}

bool Stack::empty() const{
    if (count > 0)
        return false;
    return true;
}

Error_code Stack::top(Stack_entry & item) const {
    Error_code stat = succeed;
    if (count == 0)
        stat = underflow;
    else
        item = entry[count-1];

    return stat;
}

Error_code Stack::pop() {
    Error_code stat = succeed;
    if (count == 0)
        stat = underflow;
    else
        count--;

    return stat;    
}

Error_code Stack::push(const Stack_entry & item) {
    Error_code stat = succeed;
    if (count == max_stack)
        stat = overflow;
    else
        entry[count++] = item;

    return stat;
}

int main(int argc, char * argv[]) {
    return 0;
}

 

posted on 2014-04-18 11:05  aoun  阅读(360)  评论(4)    收藏  举报