Introduction to Data Structures and Algorithms With Modern JavaScript
Tags: javascript, beginners, webdev, programming
Published: 2022-02-21 21:27:46

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pt5m1rz2ldltxhc90sil.png)

 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8olwybucuktyxxaovv8.png)

The _unshift () _method adds an element at the beginning of the array. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz3y5sht2md8ocw8kjxm.png)
It is possible to add elements or change the elements by accessing the index value. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6cnkpebme7jsbtfcbgg.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq4oduqhus4t4z49mgtw.png)
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4mom3swjrim59rgsr63.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4ys0dhj12lkwjzdrwrn.png)
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef6q9p4o96enxht3qslo.png)
**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.