Stack

2 min read

They say, picture is worth a thousand words, so take a look at the image below.

The man in the forklift loads second out of five pallets, with barrels on it containing some hazardous materials. Width of the cargo is similar to the width of the trailer, so packages will be moved one after another. After pallet is put on the edge of trailer, forklift operator must leave his vehicle, climb on the platform and move parcel to the front using palett jack. Then he goes back to the rear door, jumps off the truck, gets back in to his lifting machine and takes another part of the shipment.

First In, Last Out

Every package has its ordinal, this is written on the white sheet of paper and stick to the parcel. The number are 1 to 5, and were loaded in ascending order. Package number 1 was loaded first, number 2 - second, and so on.

When the time is right, palettes will be unloaded in order reversed to one during load. This is not possible to retrieve them different way, because first element loaded will be the last to leave the trailer. Such storing capacity is called First In, Last Out (FILO).

Methods

This is how Stack works. Adding and removing elements relies on the rule that first one leaves last. There are two methods supported by this data structure.

Usages

First obvious usage is the web browser, the program you are using to view this content. Everytime you visit a new page, its URl is pushed on to the stack and when you click the Back button, it gets popped from it. Stack is also helpful when it comes to reverse a word or backtrack, meaning calculate in the reverse order. Also used in compilers to check if parenthesis match.