Introduction to Data Structures and Algorithms With Modern JavaScript

kwizeral

LAMBERT KWIZERA

Posted on February 21, 2022

Introduction to Data Structures and Algorithms With Modern JavaScript

Introduction
JavaScript has pre-established data structures that helpful to solve certain needs and real-world problems. Their mastery makes a difference to the developers. Here we cover arrays, queue, stack and linked list.

Arrays
In JavaScript, an array is the most basic in all data structures. This array stores all data to be used later. Each array has a fixed number of cells decided on its creation, and each cell has a corresponding numeric index used to select its data. Indices help access data that are in array.
Image description

To add an element to an array you use these functions : push() and unshift ()
The push() function adds an element at the end of the array.
Image description

The unshift () _method adds an element at the beginning of the array.
Image description
It is possible to add elements or change the elements by accessing the index value.
Image description
It is possible to remove an element from an Array. The _pop() _method to remove the last element from an array. The same method [_pop ()
] also returns the returned value.
Image description
*Queue *
A queue is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. It follows the first-in, first-out (FIFO) principle
A queue has two main operations:

  • Insert a new element at the end of the queue, which is called enqeue.
  • Remove an element from the front of the queue, which is called dequeue.

To implement a JavaScript Queue we use an array with these two methods:

  • Add an element at the end of the array using the push() method. This method is equivalent to the enqueue operation.
  • Remove an element from the front of an array using the _shift() _method. It is equivalent to the dequeue operation.

The implementation looks like this in Visual Code

Image description
The function “Queue ()” function helps store the elements using an array, while the “enqueue ()” helps add an element at the end of the queue, but the “push ()” of an array helps insert the new element at the end of the queue.
Image description
Stack **
As a type of data structure, the stack helps keep a list of elements. It works based on the principle of Last In, First out (LIFO), to mean that the most recently added element is the first one to remove.
The stack has two operations: push and pop. The push put an element at the top of the stack while the pop removes an element from the top of the stack.
Image description
**Linked list.

Linked List is a linear data structure. But its elements are not stored at the location. The elements are linked using the pointers. 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.

💖 💪 🙅 🚩
kwizeral
LAMBERT KWIZERA

Posted on February 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How JavaScript works
javascript How JavaScript works

November 8, 2024

A Beginner's Guide to JavaScript Closures
javascript A Beginner's Guide to JavaScript Closures

November 5, 2024

Type Coercion in JavaScript Explained
javascript Type Coercion in JavaScript Explained

November 18, 2024

Introduction to Asynchronous JavaScript
javascript Introduction to Asynchronous JavaScript

October 14, 2024

Strings -- Manipulating the Immutable.
javascript Strings -- Manipulating the Immutable.

November 15, 2024