Stack && queue - 401-advanced-javascript-v/seattle-javascript-401d29 GitHub Wiki

Push O(1) Push a node onto a stack:

ALOGORITHM Push(node) //INPUT <-- Node to add // OUTPUT <-- none

`node.Next <-- Top`
`Top <-- node`

Pop O(1) the Top node will be re-assigned to the node that lives below and the Top node is returned to the user.

ALGORITHM Pop() // INPUT <-- No input / OUTPUT <-- Top Node of stack

`Node temp <-- Top`
`Top <-- Top.Next`
`temp.Next <-- null`
`return temp`

Peek O(1) Traditionally, you always want to Peek before conducting a Pop. This will ensure that you do not receive a NullExceptionError on your Pop action.

ALGORITHM Peek() // INPUT <-- none // OUTPUT <-- Node of top of stack

`return Top`

Queue

Queues follow these concepts:

  1. FIFO - First In First Out - This means that the first item in the queue will be the first item out of the queue.
  2. LILO - Last In Last Out - This means that the last item in the queue will be the last item out of the queue.