Before we dive into things, let's take a step back and try to understand what data structures are and why they are useful.
Data Structures are simply systems through which data is organized and stored, to enable easy access to said data. An example of a non-digital data structure is a dictionary. A dictionary contains words and their meanings, and these words are arranged alphabetically for easier access. The data in this context are the words and their definitions when this Pair is arranged to enable easy searching and reading; that's a data structure.
Computers use data structures to store data to make it easier for us to access and use it; some examples of computer data structures are arrays, linked lists, trees, stacks, queues, hash tables, etc.
Data structures are divided into 2 main types Linear and Non-linear. Linear data structures have their data organized in a straightforward manner, with each element following the previous and connecting to the next. Examples of linear data structures include arrays, stacks, queues, Linked Lists, etc.
Non-linear data structures on the other hand store their data in a hierarchy with branches and levels. Examples include trees and graphs.
Data structures are also categorized into static and dynamic data structures. Learn why here
Now that we have a basic understanding of what data structures are, why they are useful, and how they are categorized; let's finally take that dive into visualizing stacks and queues.
Stack data structures are similar to stacks in real life. The most common analogy used to describe what a stack data structure looks like is a stack of plates.
In this analogy the plates represent data and the data can be anything; functions, strings, images etc. In order to take a plate from the stack we have to take it from the top, and when adding plates to the stack we add it from the top. This process of adding and removing from the stack is called push (add to stack) and pop (remove from stack). Stacks operate on what is called a LIFO (Last In First Out) approach. The Top/Head of the stack is always the newest element.
stacks also have methods like peek and isEmpty; the peek method shows the current top element of the stack and the isEmpty method returns true or false if the stack has an element inside or not.
Stacks are used in many algorithms and are present in browsers, operating systems, and games.
Stack data structure is used in undo and redo typing functions. Stacks help keep track of when new words are added and when words are deleted, which can be reversed using the redo or undo functions.
Queue data structures have a similar structure to stacks but operate differently, just like queues in real life use a FIFO (First In First Out) approach, so do queue data structures. Unlike stacks where the adding (pushing) and removing (popping) of elements occurs at the top/head of the stack, queues add (enqueue) their elements to the end of the queue and remove (dequeue) from the top/head.
Just like stacks, queues also have the peek and isEmpty methods and do the same thing as mentioned above.
After the first click the call stack will be busy carrying out the function; all other clicks will be pushed (enqueued) into the Task Queue to wait for the Call Stack to be free. When the call stack is free, the oldest queued task is removed (dequeued) and pushed into the call stack and the cycle continues till both the Call Stack and Task Queue are empty; We can see this in action below.
Queues are also used in CPU Scheduling; In situations where memory is limited processes need to be scheduled to ensure they are completed as fast as possible. One of the simplest scheduling algorithms to implement is the FCFS (first come, first served) Scheduling Algorithm, which is basically a queue that stores the processes in the order they arrive.