|
| 1 | +## Introduction |
| 2 | + |
| 3 | +Stacks and queues are foundational data structures that are useful when adding and removing in particular orders. It's important to be comfortable with these two data structures. |
| 4 | + |
| 5 | +## Stacks |
| 6 | +A **stack** is a data structure that stores objects in which the most recently stored objects are the first ones to be removed, (LIFO: last in, first out). An example to help you remember the mechanics of a stack is to associate it with stacks in real life. With a stack of plates, the plates that are placed on top of a stack will be the first ones that are removed from the top! |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +It's important to know the common operations of a stack. The two key stack operations are: |
| 11 | +1) pop(): removing an item from the stack in a last in, first out order (LIFO) |
| 12 | +2) push(item): adding an item to the stack |
| 13 | + |
| 14 | + |
| 15 | +## Queues |
| 16 | +A **queue** is a data structure that stores objects in which the most stored objects are the first ones to be removed. A helpful acronym associated with queues is FIFO, first in first out. An example to help you remember the mechanics of a queue is to associate it with queues in real life. With a queue of people waiting to get a seat in a restaurant, the first people to get in the queue will be the first people seated at that restaurant. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +It's important to know the common operations associated with a queue. The two important queue operations are: |
| 21 | +1) dequeue(): removing an item from the queue in a first in, first out order (FIFO) |
| 22 | +2) enqueue(item): adding an item to the queue |
| 23 | + |
| 24 | +## Key takeaways |
| 25 | +* Stacks are very useful for it's backtracking features. For example, parsing questions tend to use stacks because of the LIFO property. |
| 26 | +* Stacks can be used to implement recursive solutions iteratively. |
| 27 | +* Queues are useful when the ordering of the data matters as it preserves that ordering. For example, they're used for caching. |
| 28 | + |
| 29 | +## Example problems |
| 30 | +* Stacks: |
| 31 | + * [Implement a queue with stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/) |
| 32 | + |
| 33 | +* Queues: |
| 34 | + * [LRU Cache](https://leetcode.com/problems/lru-cache/) |
| 35 | + |
| 36 | +## Resources |
| 37 | +### Guides |
| 38 | +* [Overview of stacks & queues with applications](https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html) |
| 39 | +* [In depth stacks guide](https://medium.com/basecs/stacks-and-overflows-dbcf7854dc67) |
| 40 | +* [In depth queues guide](https://medium.com/basecs/to-queue-or-not-to-queue-2653bcde5b04) |
| 41 | + |
| 42 | +### Libraries |
| 43 | +* [Java queue library](https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html) |
| 44 | +* [Java stack library]( |
| 45 | +https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html) |
| 46 | +* [Python queue library]( |
| 47 | +https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-queues) |
| 48 | +* [Python stack library](https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-stacks) |
| 49 | + |
0 commit comments