Stack

1.Introduction

  • Last element you add to the stack is the first one you access (Last In First Out)

  • Top (最後一個)

  • Push: Adding a new element to the top of the stack

  • Pop: Removing an element from the top of the stack

  • Peek

2.The complexity

  • Push, pop: O(1)

  • isEmpty, IsFull, size: O(1)

3.Where can stacks be used

  • Implementing undo in an application

  • Implementing the back button on the web browser

  • Holding the memory for recursive calls in a programming language

  • Translating infix notation for expressions to postfix

4.Match parenthesis in an expression

  • map: 存放} {, ) (, ] [

  • set: 存放{, (, [

  • stack: 後進先出, 用來確認上一個元素是否符合map的值

5.Find the minimum element in a stack in constant time

Last updated