[C++]數組模擬棧

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;
		}
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章