跳过正文

数据结构与算法-2-栈

·191 字· loading · loading ·
Masterlong
作者
Masterlong
熬夜,但是世界之夜
目录
数据结构与算法 - 这篇文章属于一个选集。
§ 3: 本文

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()
数据结构与算法 - 这篇文章属于一个选集。
§ 3: 本文

相关文章

数据结构与算法-3-队列
·173 字· loading · loading
数据结构与算法-1-线性表
·419 字· loading · loading
数据结构与算法-0
·672 字· loading · loading