Understand Data Structure : Stacks and Queues

Core concept of Stack and Queues with example code in Javascript

Table of contents

Stack and Queues are what we can call two of these appendices Linear Data Structures. Linear data structures make us can treat them sequentially for every index or we can look at the value one by one.

Stacks

Stack is a data structure like the spell of if, stack means pile, like a pile of plates, pile of clothes in the cupboard. Let's say we want to take the plate from the kitchen because you want to take breakfast, now if the plates are arranged in a rack from top to bottom, which plate do you first take? Yes, you will take the plate at the very top. That is how Stacks works. we have a slogan for it LIFO (Last In First Out). Let's see the image below

Photo by Rachel Claire from Pexels: https://www.pexels.com/photo/close-up-of-a-pile-of-plated-in-a-kitchen-cabinet-8112377/

Based on the image I provide can you imagine the illustration I talk before? If not you can refer to this simulation: https://www.cs.usfca.edu/~galles/visualization/SimpleStack.html

Alright, now how to implement it in coding? let's go to the code implementation. In this article, I will write the code using Javascript, but I will be sure you will be adapt it to your language preference.

class Stack {
    constructor(){
        this.array = [];
    }
    peek() { // this is for get the element from the stack, remember Firt in Last Out
        return this.array[this.array.length-1];
    }
    push(value){ // this is for add element in to the stack
        this.array.push(value);
        return this;
    }
    pop(){ // this is for delete the element in the stack
        this.array.pop();
        return this;
    }
}

// Here how to implement it

const myStack = new Stack();
myStack.peek();
myStack.push("Cappucino");
myStack.push('Espresso');
myStack.push('Latte');
console.log(myStack.peek());

Queues

What comes to your mind when you hear "queues"? Before we talk about it in Data structure, let's say you want to go to a restaurant and you ordered aceh noodles and orange juice. the restaurant writer will write your order and give it to the chef in the kitchen. Afterward, there is another visitor to the restaurant who orders the same menu as you. Now who is the first will get the menu? Yes, you will get the first menu because you come and ordered in the restaurant before another visitor. in Queues we call it FIFO (First In First Out). Let's see the image below

Photo by zhang kaiyv from Pexels: https://www.pexels.com/photo/a-group-of-people-waiting-in-line-4568193/

In the data structure, the Queue is suitable for Linked-List implementation.

Great, now let's jump out to the coding section. In this case, I want to make a cup of Sanger Espresso. Then the recipe is :

  1. Espresso 2 Shot

  2. Sweet milk

  3. Creamer

class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Queue {
    constructor(){
        this.first = null;
        this.last = null;
        this.length = 0;
    }
    peek() {
        return this.first;
    }
    enqueue(value){
        const newNode = new Node(value);
        if (this.length === 0) {
            this.first = newNode;
            this.last = newNode;
        } else {
            this.last.next = newNode;
            this.last = newNode;
        }
        this.length++;
        return this;
    }
    dequeue(){
        if (!this.first) {
            return null;
        }
        if (this.first === this.last) {
            this.last = null;
        }
        const holdingPointer = this.first;
        this.first = this.first.next;
        this.length--;
        return this;
    }

}

const myQueue = new Queue();
myQueue.peek();
myQueue.enqueue('Coffee Powder');
myQueue.enqueue('Sweet Milk');
myQueue.enqueue('Creamer');
console.log(myQueue.peek());

Summary

There are pros and cons between Stacks and Queues, let's have a look

  • Stack has some pros like fast operations such as removing and inserting, fast peek and order. Besides stack also have cons such as it so slow for Lookup

  • The Queue has some pros like being suitable for implementing algorithms that need to handle scheduling processes and the First In First Out queue can be very useful if you want to keep the data ordered. Besides it, queue have some pros it will not efficient if you want to implement random access and operation like inserting or removing the element.