I’m learning about data structures and algorithms in JavaScript. I’m using various online resources and going to be sharing my notes as a series of blog posts. The topics are below:
- Data Structures: List ADT | Stacks | Queues | Linked Lists | Trees, Tries & Graphs | Heaps | Hash Tables
- Algorithms: Breadth-First Search | Depth-First Search | Binary Search | Merge Sort | Quick Sort
- Concepts: Memory (Stack vs. Heap) | Recursion | Big O Time & Space
2. Stacks
- A stack is a list of elements that are only accessible from the top of the list.
- As such, a stack is referred to as a Last-in, First-out (LIFO) data structure.
- It is efficient and fast because we can add or remove data only from the top of the list.
- Any element that is not at the top of the stack can not be accessed directly.
- The push method adds an element to the top of the stack, while pop removes an element from the top of a stack.
- The peek method returns the value of the element at the top without removing it as pop does.
- The top property tracks the top position of the stack while the dataStore is a an array used to store data internally.
- Eg. usage: ‘undo’ operation of a text editor.
- Here is an implementation of a Stack class:
https://gist.github.com/tamg/bbb264eba6eced1561210ddf5d213332
- An example using the Stack class:
https://gist.github.com/tamg/c4aa2a56ef714fd04617c853deeb2946