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:

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:

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

EXAMPLE:

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:

Does it matter?

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.