4.2 Stack #
4.2.1 Stack ADT #
feature LIFO
插入和删除只能发生在线性表的一端
- Push(插入)
- Pop(移除)
- Top(栈顶元素)
- Length
// Stack abtract class
template <typename E> class Stack {
private:
void operator =(const Stack&) {} // Protect assignment
Stack(const Stack&) {} // Protect copy constructor
public:
Stack() {} // Default constructor
virtual ˜Stack() {} // Base destructor
// Reinitialize the stack. The user is responsible for
// reclaiming the storage used by the stack elements.
virtual void clear() = 0;
// Push an element onto the top of the stack.
// it: The element being pushed onto the stack.
virtual void push(const E& it) = 0;
// Remove the element at the top of the stack.
// Return: The element at the top of the stack.
virtual E pop() = 0;
// Return: A copy of the top element.
virtual const E& topValue() const = 0;
// Return: The number of elements in the stack.
virtual int length() const = 0;
};
根据题意判断可能的出栈顺序 模拟操作
4.2.2 Array based #
1个数组,2个整型变量
- listArray, maxSize, top(也就是栈的元素个数)
规定top指向目前顶端元素的上一个元素,大小等于元素个数,初始化为0
-
错误
-
溢出
-
栈空
-
4.2.3 Linked #
1个指针,1个整型变量
-
top, size
-
错误
- 只有栈空问题
其它 #
应用1:进制转换
WHILE(N)
PUSH(N%D)
N/=D
WHILE(!STACKEMPTY)
PRINT POP()