Ideas of stack and queue you can find in different program languages. Let's find out what differences between stack and queue and how they can be help us.
We have deal with queus everywhere. In shops - it is a group of people which stay one after another to the cashier in the store. The one who stand closer to cashier will pay for products first.
In programming, a queue works similarly. A queue in programming is a collection of elements that are processed on “a first in, first out” (FIFO) basis. It is better to use queu for tasks which should be done in stages. For example, when user loads several images to site, after server needs to process all images. All that images put to queue for processing. And the first file that the user has uploaded will be the first one processed on the server.
The stack works differently. A stack is also a collection of elements, but it works on a "last in, first out" (LIFO) basis. In everyday life, we can come across an analogy when we put documents on top of each other, and then iterate over them, starting from the top one. It's like working with a stack: you can work only with the last element that got into the collection.
Frequent use of the stack in programming it is organising logs of changes in file or document. Text editors often record user’s changes in file to the stack. And after it is easily in reverse mode undo changes which user did in file.
When you work with stack or queue you cant access an arbitrary element in collection. Becouse it would violate the principles LIFO and FIFO. If you need to get neccessery element, you need itarate all element before it.
In programming, the stack and queue are often used for various complex mathematical calculations, data manipulation and operations. The principles of FIFO and LIFO allow us to structure and organize our work with data. This allows you to be sure that data and operations will be processed in the right order.
Top comments (0)