DEV Community

Cover image for Introduction to Data Structures and Algorithms with Modern JavaScript

Posted on

Introduction to Data Structures and Algorithms with Modern JavaScript


In JavaScript, an array is a variable that stores several elements.Arrays are used when we want store a list of elements and retrieve them with single variable.
In modern JS, an array may hold different data types such as strings, numbers, boolean. All this can be stored in one array. Elements in arrays are denoted by index and therefore the first element of an array is always denoted with the [0] index.
Below is an illustration:

arr = ["lux","Alvin", "academy"]
Enter fullscreen mode Exit fullscreen mode

Hence the element lux is in index 0, element Alvin is in index 1 and the element academy is index 2.


It is one of the important data structures and a useful one, with a wide range of applications. It is a linear data structure where by any addition or deletion of an element follows a particular order i.e Last in Fast Out (LIFO) and Fast in Last Out (FIFO).

An example of its implementation is as below:

// Adding element to the stack

// Printing the stack element
// prints [10, 20, 30]

// returns 30

// returns 30 and remove it from stack

//returns [10, 20]

Enter fullscreen mode Exit fullscreen mode

For more information on stack, check out this link.[]


Unlike stack, Queue works on the First in First Out (FIFO) principle. Queue performs two basic operations i.e additional of elements at the end of the queue and removal of elements at the front of the queue. It is also a linear data structure. Queue contains functions such as enqueue, dequeue, front(), and is Empty().
An illustration of how the enqueue and dequeue function is as below:

// enqueue function
    // adding element to the queue

Enter fullscreen mode Exit fullscreen mode


// dequeue function
    // removing element from the queue
    // returns underflow when called
    // on empty queue
        return "Underflow";
    return this.items.shift();

Enter fullscreen mode Exit fullscreen mode

For moreinformation on how Queue works visit [].

Linked list

Similar to arrays, stack and queue, the linked list data structure is also a linear data structure. Here elements are not stored in a particular memory. They are stored in a separate object that contains a pointer to the next object in that list.
It always has an entry point called the head and which is a reference to the first node in the linked list. The last node on the list points to null.
Null reference means that the list is empty.

An illustration of how Linked list is as below :

`const list = {
head: {
value: 6
next: {
value: 10
next: {
value: 12
next: {
value: 3
next: null

To read more on types of linked lists, checkout this link [].

Top comments (0)