Stacks & Queues — Data Structures
Data structures are a big part of computer science and programming and are frequently used in programming. I will go into more detail about two data structures that are often grouped together in learning Stacks and Queues.
Stacks
One of the main properties of a stack is LIFO which stands for Last In First Out. If we think of a ‘stack’ of books like in the above picture the last one put on is at the top and it would be the first one you would take off. So when removing from a stack we can only remove the last item inserted. The main functions we associate with stacks is insertion and removal (adding to the top of the stack and removing from the top of the stack). Both of these in terms of Big O have a constant runtime (O(1)). It is possible to simply use an array and make it behave like a stack by only using the built-in array methods push and pop.
Queues
A queue in a way is almost the opposite of a stack in that its main property is FIFO which is First In First Out. Again looking at the above picture when you ‘queue’ up to get in somewhere the first person in line is the first person to get in or off the line. Similarly to stacks, the functions of a queue are mainly insertion and removal (enqueue and dequeue) and like stacks have Constant runtime. We can’t make an array behave like a queue however because we would have to use either shift or unshift and that wouldn't give us constant time. For a queue, we would have to build out the data structure.
More on both
Stacks and queues aren’t very complex data structures as you can see they are restricted by rules for adding to and removing from them. They work very efficiently in this though with constant runtime. Therefore are useful if you need a data structure that is good in that aspect and follows the FIFO or LIFO order. Searching or gaining access to a particular node is O(n) because there's no order or simpler way of doing this. Furthermore, just simply based on the rules we can’t insert a node at just any point for queues nodes always go to the end and for stacks nodes always go to the top. Likewise for removing nodes.
Working through Udemy courses JavaScript Algorithms and Data Structures Masterclass & Master the Coding Interview: Data Structures + Algorithms helped me to understand and use both Stacks & Queues.