🕋Estrutura de Dados (FIFO vs LIFO) - 2 visões
- #Estrutura de dados
- #Desperte o potencial
- #TDD
Após ver uma discussão muito interessante me lembrei que não tive dificuldades em entender o assunto por ter conhecimento prévio em outra área mas com contexto muito semelhante e resolvi compartilhar o resumo* básico de minhas pesquisas comparando os termos como segue abaixo, curtam e comentem.
Como conhecia os termos na contabilidade, simplesmente mudo os conceitos e é basicamente o mesmo entre as duas áreas, em contabilidade estes são mais utilizados para controles de estoque (a mesma coisa para technology).
Talvez tenham menos dúvidas utilizando esta analogia como complemento, porque cada método tem objetivos diferentes (em relação a custo):
>>>$$$Na contabilidade:
FIFO (“First-In, First-Out”) assumes that the oldest products in a company's inventory have been sold first and goes by those production costs. The LIFO (“Last-In, First-Out”) method assumes that the most recent products in a company's inventory have been sold first and uses those costs instead.
>>>Na programação.exe:
First In, First Out (Queue)
FIFO is a term that refers to use whatever was added first. For example, let’s assume that a stack of trays is being set on the floor. The older trays are stacked on the bottom while the newer trays are stacked on the top. Now, when we work using FIFO, the last tray which arrived first would be used first. The trays will be used in the same way the trays were stacked.
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Last in, First Out (Stack)
Last In, First Out (LIFO) is a term that is used to refer to whatever was added last is used first. Using the tray example; the trays are stacked in order of the oldest one at the end and the newest one at the top. Now, newer trays are added to the top and they are used in the same way. The newer trays are used first, with the older trays being used last.
In data structures, concept is the same: let's assume we have an array, but we can add new elements only on top of it and pop elements only from top of it. It would behave as stack of trays from example above. In fact, such array is called stack on computer programming.