## Introduction

When you build software there are many topics you need to think through first in order to achieve the goal and have a nice result.

You need to ask yourself questions such as:-

- How are you going to store data?
- How are you going to deal with static logic?
- How will you handle authentication? Etc.

## Algorithms

An Algorithm is a step-by-step procedure that defines a set of instructions to be executed in a certain order to get the desired output.

Algorithms are generally independent of the underlying languages. They can therefore be implemented in more than one language.

The main properties of an algorithm that you can consider include:-

- Input- An algorithm must possess 0 or more well-defined inputs supplied externally to the algorithm.
- Output- An algorithm should have 1 or more well-defined outputs as desired.
- Correctness- Every step of the algorithm must generate correct output.
- Definiteness-Algorithms should be clear/unambiguous, thus every step of the algorithm should be clear and well defined.
- Finiteness- The algorithm should have a finite number of steps that must be carried out to achieve the task at hand.

One thing that makes algorithms worth studying is that in most cases, there exists more than one algorithm capable of handling the task at hand.

## Data Structures

Data structures enable us to manage and utilize large datasets, handle multiple requests from users at once, and speed up data processing.

Therefore, learning Data Structures and Algorithms allows us to write different efficient and optimised computer programs.

**Letβs take a look at types of Data Structures in JS**

## Arrays

This is the most basic of all data structures. Basically, an array is a container object that is used to hold a list of items usually of the same type.

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.

JavaScript has dynamic arrays i.e their size is not predetermined, nor the type of data.

The easiest way to create a JavaScript Array is using array literal.

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

There are common methods associated with arrays that you should know.

*Common methods you should know*

## Queues

Queues are sequential structures that process elements in the order they have entered rather than the most recent element, also known as the FIFO(First In First Out).

A Queue has the following associated JavaScript methods:-

- enqueue: enter queue/add an element at the end.
- dequeue: leave queue/remove the front element and return it
- front: to get the first element
- isEmpty: determine whether the queue is empty
- size: get the number of element(s) in queue

```
function Queue() {
var collection = [];
this.print = function () {
console.log(collection);
}
this.enqueue = function (element) {
collection.push(element);
}
this.dequeue = function ()=> {
return collection.shift();
}
this.front = function () {
return collection[0];
}
this.isEmpty = function () {
return collection.length === 0;
}
this.size = function () {
return collection.length;
}
}
```

*JS implementation of a queue*

**Applications of Queues**

- They are helpful as a buffer for requests, storing each request in the order it was received until it can be processed.
- A convenient way to store order-sensitive data such as stored voicemails
- Ensures the oldest data is processed first

## Stack

Similar to Queue, Stack is a linear data structure that follows the LIFO(Last In First Out) or FILO(First In Last Out) principle

Stack is an abstract data type that serves as a collection of elements, with two principal operations:

- push, which adds an element to the stack, and
- pop, which removes the most recently added element that was not yet removed.

Whenever we add an element to the stack, it is added at the top of the stack and also whenever we delete an element from the stack it is deleted from the top of the stack.

```
function Stack() {
this.count = 0;
this.storage = {};
this.push = function (value) {
this.storage[this.count] = value;
this.count++;
}
this.pop = function () {
if (this.count === 0) {
return undefined;
}
this.count--;
var result = this.storage[this.count];
delete this.storage[this.count];
return result;
}
this.peek = function () {
return this.storage[this.count - 1];
}
this.size = function () {
return this.count;
}
}
```

*function stack example in JS*

## Linked lists

A Linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next.

A unilateral linked list normally has the following methods:

- size: Return the number of node(s)
- head: Return the element of the head
- add: Add another node in the tail
- remove: Remove certain node
- indexOf: Return the index of a node
- elementAt: Return the node of an index
- addAt: Insert a node at a specific index
- removeAt: Delete a node at a specific index

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

*A simple js Linked list.*

A new instance of the LinkedList object will have the head and tail pointers refer to the null value, while the length property will start at 0.

There are three known types of linked list data structure:

- Singly-linked list where each node only has the next pointer
- Doubly linked list where each node as both next and previous pointer
- Circular linked list where the tail node points to the head node, creating a circle.

## Conclusion

Data structures and algorithms can solve the most common problems efficiently. This will make a difference in the quality of the source code you write. This includes performance.

If you choose the incorrect data structure or algorithm depending on the scenario, you can have some performance issues, therefore, this informs that Data structures and algorithms are core elements in any system or software development ecosystem.

I hope I shed some light on DSA.

*Cheers!*

## Top comments (0)