Week 7 Flashcards
both are versatile data structures commonly used in the implementation of other, more complex data structures.
stacks and queues;
is a data structure in which only the last inserted element can be removed and accessed. Think about stacking plates on a table. To get to the bottom one, you must remove all the other ones on the top. This is a principle known as last in, first out (LIFO).
A stack
is great because it is fast. Since it is known that the last element is to be removed, the lookup and insertion happen in a constant time of O(1).
A stack
should be used over arrays when you need to work with data in the LIFO form where the algorithm needs to access only the last-added element. The limitation of —- is that they cannot access the non-last-added element directly like arrays can; in addition, accessing deeper elements requires you to remove the elements from the data structure.
Stacks
is also a data structure, but you can remove only the first added element. This is a principle known as first in, first out (FIFO).
A queue
is also great because of the constant time in its operations. Similar to a stack, it has limitations because only one item can be accessed at a time.
A queue
should be used over arrays when you need to work with data in the FIFO form where the algorithm only needs to access the first added element.
Queues