DEV Community

Cover image for Introduction to Data Structures and Algorithms with Modern Javascript.
Joseph Kilatya
Joseph Kilatya

Posted on

Introduction to Data Structures and Algorithms with Modern Javascript.

A data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently.
More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.
There is huge concept behind data structures and algorithms. Therefore we are going to check few but key concepts in this sector.

Arrays
In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable. In JavaScript therefore array is a single variable that stores multiple elements.
Array syntax

var Array = [ ]; // method 1 
var Array = new Array(); // method 2 
Enter fullscreen mode Exit fullscreen mode

Sample code

//method 1
// array with string elements
var myArray = ['cat','dog','goat','cow']; 

// array with integer elememts
var myArray = [1,2,3,4,5,6]; 


//method 2
 // array with string elements
var myArray = new Array('cat','dog','goat','cow');

//array with integer elements
var myArray2 = new Array(1,2,3,4,5,6); 
Enter fullscreen mode Exit fullscreen mode

The first element in an array has index 0 while the last element has index n-1
You can also check different array methods in javascript here

Stack

A stack is a data structure that holds a list of elements. A stack works based on the LIFO principle i.e., Last In, First out, meaning that the most recently added element is the first one to remove.
A good analogy to explain this is using books placed on top of each other. The last book to be added on top is the first one you can access. The first book(bottom book) will be accessed last.
Stack methods in Javascript;

  1. The push() method : allows you to add one or more elements to the end of the array.

  2. The pop() method : removes the element at the end of the array and returns the element to the caller.

Sample code

let stack = [];

stack.push(1);
console.log(stack); // [1]

stack.push(2);
console.log(stack); // [1,2]

stack.push(3);
console.log(stack); // [1,2,3]

stack.push(4);
console.log(stack); // [1,2,3,4]

Enter fullscreen mode Exit fullscreen mode

Linked lists
A linked list is a linear data structure that represents a collection of elements, where each element points to the next one. The first element in the linked list is the head and the last element is the tail.

Each element of a linked list data structure must have the following properties:

  1. value:The value of the element.
  2. next: A pointer to the next element in the linked list.

The main properties of a linked list data structure are:

  • size: The number of elements in the linked list.
  • head: The first element in the linked list.
  • tail: The last element in the linked list.

The main operations of a linked list data structure are:

  • insertAt: Inserts an element at the specific index.
  • removeAt: Removes the element at the specific index.
  • getAt: Retrieves the element at the specific index
  • clear: Empties the linked list.
  • reverse: Reverses the order of elements in the linked list.

Example of a linked list code

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

Enter fullscreen mode Exit fullscreen mode

Queue
A queue is a data structure that follows First In First Out (FIFO) principle. The element that is added first is accessed at first. The last element to be added is therefore accessed at last.

Example code of queue in Javascript

class Queue {
    constructor() {
        this.items = [];
    }

    // add element to the queue
    enqueue(element) {
        return this.items.push(element);
    }
    // view the last element
    peek() {
        return this.items[this.items.length - 1];
    }
}
let queue = new Queue();

queue.enqueue(1);

queue.enqueue(2);

queue.enqueue(4);

queue.enqueue(8);

console.log(queue.items);

console.log(queue.peek());

Enter fullscreen mode Exit fullscreen mode

Conclusion
So where can we how can we apply data structures in our programming journey.

  1. Search − Algorithm to search an item in a data structure.
  2. Sort − Algorithm to sort items in a certain order.
  3. Insert − Algorithm to insert item in a data structure.
  4. Update − Algorithm to update an existing item in a data structure.

The topic on Javascript data structures and algorithms is very broad and wide. You can check on other resources out there to learn more.

Happy Coding.

Oldest comments (0)