Push Input: 准备压入堆栈的元素 Preconditions: 无 Press: 将元素压入栈顶 Output: 无 Postconditions:新压入元素成为栈顶元素 Peek Input: 无 Preconditions: 堆栈非空 Press: 访问栈顶元素 Output: 栈顶元素的数据值 Postconditions:堆栈不变
Push Input: 准备压入堆栈的元素 Preconditions: 无 Press: 将元素压入栈顶 Output: 无 Postconditions: 新压入元素成为栈顶元素 Peek Input: 无 Preconditions: 堆栈非空 Press: 访问栈顶元素 Output: 栈顶元素的数据值 Postconditions: 堆栈不变
end ADT Stack
end ADT Stack
4.1.2 栈的顺序表示和实现 栈内变化情况 321 3 1 top 0 A top -1 3 3 2 32 top 今 top ,1 B 1 0 A 0 A 0 top- -1
4.1.2 栈的顺序表示和实现 top 1 2 3 0 -1 top 1 2 3 0 A C B top 1 2 3 0 A B top 1 2 3 0 A top 1 2 3 0 -1 栈 内 变 化 情 况
1顺序栈的类定义 include <iostream.h> include <stdlib,h> const int MaxStackSize =50; Template<class T> Class Stack private T stacklist [MaxStackSize]; int top;
1 顺序栈的类定义 # include <iostream.h> # include <stdlib.h> const int MaxStackSize = 50; Template<class T> Class Stack private T stacklist[MaxStackSize]; int top;
public: Stack (void); void Push(const T&item); T Pop(void); void ClearStack(void); T Peek(void)const; int StackEmpty(void)const; int StackFull(void)const; };
public: Stack(void); void Push(const T& item); T Pop(void); void ClearStack(void); T Peek(void) const; int StackEmpty(void) const; int StackFull(void) const; };