template <typename value_type>
class Stack { //the stack is a FILO container.
static const int MAXSIZE = 1 << 10; //the max size of each stack.
value_type data[MAXSIZE]; //data domain.
int TopPointer; //pointer of the top of each stack.
public:
Stack() { //initialize the stack.
TopPointer = -1; //Top pointer is pointing virtual area initially.
fill(data, data + MAXSIZE, NULL); //There's nothing in the data domain initially.
}
void push(const value_type &Val) { //push an element into the stack.
data[++TopPointer] = Val;
}
void pop() { //pop the top element out.
--TopPointer;
}
value_type &top() { //return the reference of the top element.
return data[TopPointer];
}
const value_type &top() const { //return the const reference of the top element.
return data[TopPointer];
}
bool empty() const { //return the situation of the stack[empty or not].
return TopPointer == -1 ? true : false;
}
int size() const { //return the size of the stack.
return TopPointer + 1;
}
};
[C++]数组模拟栈
最新推荐文章于 2024-07-23 17:01:37 发布