DEV Community

Cover image for Introduction to Data Structures and Algorithms With Modern JavaScript
David Kyalo
David Kyalo

Posted on

Introduction to Data Structures and Algorithms With Modern JavaScript

Data Structures

Data structures allow you to manage data. JavaScript has primitive and non-primitive data structures. Primitive data structures and data types are native to the programming language. These include boolean, null, number, string, etc.
Non-primitive data structures are not defined by the programming language but rather by the programmer. These include linear data structures, static data structures, and dynamic data structures, like queue and linked lists.

1. Array

An array is a single variable that keeps numerous elements. In JavaScript, an array may hold different items such as Boolean, strings, and numbers, all of which can be stored in a single array.
Arrays can be declared in two ways. This is shown in the examples below.

let array = ['JavaScript','is','fun']
Enter fullscreen mode Exit fullscreen mode

or

let array = newArray('JavaScript','is','fun')
Enter fullscreen mode Exit fullscreen mode

Because arrays are indexed from 0, a number in square brackets is used to access elements in an array. This is shown below:

let array = ['JavaScript','is','fun']
console.log(array[0]) //JavaScript
console.log(array[1]) //is
console.log(array[2]) //fun
Enter fullscreen mode Exit fullscreen mode

The number of elements in an array is returned using the length property of arrays. An array's length attribute can be returned as shown below:

let array = ['JavaScript','is','fun']
console.log(array.length) //3
Enter fullscreen mode Exit fullscreen mode

We may assign a value to the next index to add a new value to our array.

let array = ['JavaScript','is','fun']
array[3]='always'
console.log(array)

//The output is: ['JavaScript','is','fun','always']
Enter fullscreen mode Exit fullscreen mode

We utilize the splice() function to remove or delete a specific item from an array. For example:

let array = ['JavaScript','is','fun']
array.splice(1,1)
console.log(array) //['JavaScript','fun']

Enter fullscreen mode Exit fullscreen mode

To loop through an array, we may use the for keyword to loop through the full array, taking use of the length parameter. For example:

let array = ['JavaScript','is','fun']

for(a=0;i<array.length;a++){
   console.log(a,array[a]
}


/* The output is:
0 'JavaScript'
1 'is'
2 'fun'
*/
Enter fullscreen mode Exit fullscreen mode

2. Queue

A queue is also a data structure but you can remove only the first added element. This principal is called FIFO (first in first out). The following is the constructor of the queue:

function Queue(){
  this.elements=[]
}
Enter fullscreen mode Exit fullscreen mode

The Queue() constructor function uses an array to store its elements. The enqueue() method adds an element at the end of the queue. We use the push() method of the array object to insert the new element at the end of the queue.

Queue.prototype.enqueue = function (e) {
   this.elements.push(e);
}
Enter fullscreen mode Exit fullscreen mode

The dequeue() method removes an element from the front of the queue. In the dequeue() method, we use the shift() method of the array to remove an element at the front of the queue.


Queue.prototype.dequeue = function () {
    return this.elements.shift();
}
Enter fullscreen mode Exit fullscreen mode

3. Stack

A stack is an ordered list which follows LIFO (last in first out) algorithm. You can access the elements of a stack from only a single end. The push() method allows you to add one or more elements to the end of the array. The push() method returns the value of the length property that specifies the number of elements in the array. This is shown below:

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]

Enter fullscreen mode Exit fullscreen mode

The pop() method removes the element at the end of the array and returns the element to the caller. If the array is empty, the pop() method returns undefined. This is shown below:

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

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

console.log(stack.pop()); //  1
console.log(stack); // []; // empty

console.log(stack.pop()); //  undefined
Enter fullscreen mode Exit fullscreen mode

4. Linked list

A linked list is a linear data structure similar to an array. However, unlike arrays, elements are not stored in a particular memory location or index. Rather each element is a separate object that contains a pointer or a link to the next object in that list. Each element (commonly called nodes) contains two items: the data stored and a link to the next node. The data can be any valid data type. The code below shows the implementation of a linked list class with a constructor.

class LinkedList {
    constructor(head = null) {
        this.head = head
    }
}
Enter fullscreen mode Exit fullscreen mode

Algorithms

An algorithm is a sequence of steps to solve a well-defined problem. A set of rules that precisely define a sequence of operations. We have different types of algorithms as stated below:

  • Recursion This is a programming technique using function or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first. Recursion involves solving problems by breaking things down into simpler/smaller versions of themselves
  • Binary Search This is a divide and conquer algorithm, that divides the array in half every time it checks whether an element of the array is the one, we're looking for.
  • Tail recursion
    This is when, instead of doing an invocation of the recursive function as the return statement, it does a jump and reuses the same context of the prior recursive called.

  • Big O notation
    This is a way of representing the general growth in the computational hardness of a task as you increase the data set.

  • Imperative code
    This is when you tell your program every single step to achieve a specific outcome as per your expected output.

Top comments (0)