Introduction to Data Structures and Algorithms With Modern JavaScript.
Tags: javascript, beginners, algorithms, programming
Published: 2022-02-21 09:33:11


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/370lpvbfgr4g7ix2h5ta.png)

Data Structures are a specific method of arranging and storing data in computers so that we can execute more efficient operations on the data.

Data structures are used in a wide range of fields, including Computer Science and Software Engineering. 
Data can be retrieved and stored in a variety of ways.
Arrays and objects are two common JavaScript data structures that you are already familiar with.

In JavaScript data structures, you'll get to learn how to access and change data using useful JS techniques like splice() and Object.keys(). 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v03h2jhrli44hoimn9vd.jpg)

An algorithm is a set of instructions that describe how to perform something step by step. Breaking down an issue into smaller sections and carefully considering how to solve each portion using code will help you develop an effective algorithm.

## Arrays
An array in JavaScript is an ordered list of values. Each value is referred to as an element, and it is identified by an index.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhqmd703kxh4385yx0e3.gif)

An array in JS has a couple of characteristics,

- For starters, an array can include values of many sorts. You can have an array with items of the kinds number, string, and boolean, for example.
- Second, an array's size is dynamic and grows on its own. To put it another way, you don't have to declare the array size in advance. 

**Initializing a JavaScript Array**
There are two ways to make an array with JavaScript. The first is to utilize the Array constructor in the following way:

```
let results = new Array();
```
The `results` array is empty, and there are no elements in it. You can establish an array with an initial size if you know how many elements the array will hold, as seen in the following example:

```
let results = Array(5);
```
The elements are passed as a comma-separated list to the Array() constructor to create an array and initialize it with certain elements.
For example, the following builds the scores array that has five members (or numbers):

```
let scores = new Array(20,90,80,74,63);
```

However, The array literal notation is the preferred method of creating an array. The square brackets [] are used to wrap a comma-separated list of elements in the array literal form.

```
let arrayName = [element1, element2, element3, ...];
```

```
let cars = ['honda', 'toyota', 'volvo'];
```
The above example creates the cars array that holds string elements:

You use square brackets without specifying any elements to construct an empty array, like in:

```
let emptyArray = [];
```
**Basic Array operations**
**1) Removing an element from an array's end** 
Use the `pop` method:

```
let teams = ['chelsea', 'arsenal', 'ManU', Liverpool', 'tottenham'];
const lastElement = teams.pop();
console.log(lastElement); 

**Output**
tottenham
```
**2) Removing an element from the array's beginning**
 Use the `shift()` method:

```
let teams = ['chelsea', 'arsenal', 'ManU', Liverpool', 'tottenham'];
const firstElement = teams.shift();
console.log(firstElement); 

**Output**
chelsea
```
**3) Adding a new element to an array's end**
Use the `push()` method:

```
let teams = ['chelsea', 'arsenal', 'ManU', Liverpool', tottenham'];
teams.push('Westham');
console.log(teams); 

**Output**
['chelsea', 'arsenal', 'ManU', Liverpool', 'tottenham','Westham']
```
**4) Inserting an element at the start of an array** 
Use the `unshift()` method:

```
let teams = ['arsenal', 'ManU', Liverpool', tottenham'];
teams.unshift('chelsea');
console.log(teams); 

**Output**
['chelsea', 'arsenal', 'ManU', Liverpool', 'tottenham','Westham']
```
**5) Determine whether a value is an array.**
Use `Array.isArray()` method:

```
console.log(Array.isArray(teams)); // true
```
**6) Locating an element's index in an array.**
Use the `indexOf()` method:

```
let teams = ['arsenal', 'ManU', Liverpool', tottenham'];
let index = teams.indexOf('Liverpool');

console.log(index); // 2
```

## Queue

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6693ata6ko7lz7mm3pma.png)

A queue is used by Javascript to keep track of the code that needs to be executed. Your printer also uses a queue to keep track of which documents are due to be printed next. To choose which patient should be admitted first, hospitals use a priority queuing system. 
To put it another way, there are queues everywhere. The queue is a crucial data structure to understand, from your local cafe to your computer's CPU and server or network.

A queue works with the concept of `FIFO(First In First Out)`.
As a result, it conducts two basic operations: adding elements to the queue's end and removing elements from the queue's front. 

## Stack

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvlno2vfc4vbnq58jzvy.png)

A data structure that holds a list of elements is known as a stack. A stack works on the `Last In, First Out (LIFO)` principle, which means that the most recently added element is the first to be removed.
Push and pop are the two main operations that occur only at the top of a stack. The push action adds an element to the top of the stack, whereas the pop operation removes one from the top.

A stack can be used in a variety of ways. For instance, the most basic is to reverse a word. You do this by pushing a word into the stack, letter by letter, and then popping the letters out.
The stack's other uses include text editors' "undo" features, syntax parsing, function calls, and expression conversion (infix to post-fix, infix to prefix, post-fix to infix, and prefix to infix).
The push() and pop() functions of the JavaScript Array type allow you to use an array as a stack.

```
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]

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

## Linked List

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkpf19nf7ncbkrpmsrfu.png)

A linked list, like an array, is a linear data structure. Unlike arrays, however, elements are not kept in a specific memory region or index. Rather, each element is its own object with a pointer or link to the next item in the list.

Each element (often referred to as a node) has two components: data and a link to the next node. Any suitable data type can be used.