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.
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.
The unshift () _method adds an element at the beginning of the array.
It is possible to add elements or change the elements by accessing the index value.
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.
*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
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.
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.
**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.
Top comments (0)