Data Structure: Stack and Queue

Simplified explanation between the two data structure

Data structure is a data format on how you organize your data based on your specification or purpose. In layman’s term “Ways of organizing objects”.

It is used on list of Arrays, files, data stream and so much more.

Stack:

Stack data structure has famous term called LIFO or also known as Last In First Out. It means the last inserted on the stack will be the first to be removed one element at a time starting at the TOP.

It’s a constraint where you follow the rule for adding and removing data in an orderly way using LIFO.

Remember the word rule, it means you have a set of instructions to follow.

The image above explains how Stack works.

Steps in hierarchical order

Step 1: Data insertion must be one at a time

Step 2: The last data should be at the bottom of the stack

Step 3: New inserted data should be at the top of the stack

Step 4: The top layer of the stack should be the first to be picked if getting a data from the stack.

These are the steps on how Stack works in a code base. It has an array of instructions that should be followed and should not be skipped or jump on a random step.

IMPLEMENTATION using Arrays in JavaScript

Array.push(): Inserting element on top of the array or stack.

Array.pop(): Removing an element at the top of the array or the stack.

The illustration above explains the stack data structure using a constant value of an array of names. Array.push() and Array.pop() are the fundamental operation methods to use for inserting and deleting data from the array. I know there are other ways in JavaScript but let’s just stick to the example.

Queue:

A Queue is a linear structure of reading or creating code which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).

Where the first executed code SHOULD be the first to be invoked. If where gonna do an example using a programming language, The language should be a synchronous readout of the line of the code.

ILLUSTRATION:

Example above explains how Queue data structure works.

The data are aligned with each other. The start and end process of the In / Create of the data is the same start and end process used by the Out / Delete.

Steps in hierarchical order

Step 1: Data insertion must be one at a time

Step 2: The first data to be inserted is also the first to be removed

Step 3: The data are ordered along with each other

Using Python as a perfect example to portray the queue stack by reading the code. JavaScript is not the perfect example because it can also use Asynchronous readout which might lead to confusion to new aspirant developers.

The image above portrays the queue where the “my_first_function()” is called first after my_second_function(). As queue means the process of reading in linear. Then it means there is NO WAY for my_second_function() to run first which is called first before my_first_function() in theory.

CONCLUSION:

YES! Data structure ordering is one of the most common to deal with. To be mindful of the behavior of your data flow in the application. It’s one of the essential complexity to take notice and can introduce an accidental complexity on your development if you take no consideration if you wan’t to become an aspirant developer.

Hello there! I am a software engineer who likes to share my thoughts through words.

Hello there! I am a software engineer who likes to share my thoughts through words.