HomeGitHubAboutBlog

Data structures : Stack

27 February, 2020 - 2 min read

What is stack?

Stack seems like the most basic of data structures. Examples of stack is:

  • Undo/Redo in text editors
  • Going back and forth on browsers
  • Recursion Recursion Recursion

How can I describe stack operations?

LIFO stands for Last-in-first-out

Method Description
push pushing an element on to the stack
pop removing the 'top' element from the stack
peek look at the top element without taking it out
empty returns true if stack is empty
swap swap the two top elements

This helps a developer to understand how to add, remove and organize data in a sequential way.

Javascript Call Stack

  • A call stack is a data structure that uses the LIFO principle to temporarily store and manage function invocation.
  • Code execution is synchronous
  • It is single threaded

What is a stack overflow?

A stack overflow is when the browser has a maximum stack call that it can call before throwing an error. This happens when there is a recursive function that call itself.

function neverGonnaGiveYouUp() {
  neverGonnaGiveYouUp()
}
neverGonnaGiveYouUp()