Introduction to Data Structures and Algorithms with Modern JavaScript
Alvinimbua
Posted on February 21, 2022
Arrays
In JavaScript, an array is a variable that stores several elements.Arrays are used when we want store a list of elements and retrieve them with single variable.
In modern JS, an array may hold different data types such as strings, numbers, boolean. All this can be stored in one array. Elements in arrays are denoted by index and therefore the first element of an array is always denoted with the [0] index.
Below is an illustration:
arr = ["lux","Alvin", "academy"]
Hence the element lux is in index 0, element Alvin is in index 1 and the element academy is index 2.
Stack
It is one of the important data structures and a useful one, with a wide range of applications. It is a linear data structure where by any addition or deletion of an element follows a particular order i.e Last in Fast Out (LIFO) and Fast in Last Out (FIFO).
An example of its implementation is as below:
// Adding element to the stack
stack.push(10);
stack.push(20);
stack.push(30);
// Printing the stack element
// prints [10, 20, 30]
console.log(stack.printStack());
// returns 30
console.log(stack.peek());
// returns 30 and remove it from stack
console.log(stack.pop());
//returns [10, 20]
console.log(stack.printStack());
For more information on stack, check out this link.[https://www.geeksforgeeks.org/implementation-stack-javascript/]
Queue
Unlike stack, Queue works on the First in First Out (FIFO) principle. Queue performs two basic operations i.e additional of elements at the end of the queue and removal of elements at the front of the queue. It is also a linear data structure. Queue contains functions such as enqueue, dequeue, front(), and is Empty().
An illustration of how the enqueue and dequeue function is as below:
// enqueue function
enqueue(element)
{
// adding element to the queue
this.items.push(element);
}
//dequeue
// dequeue function
dequeue()
{
// removing element from the queue
// returns underflow when called
// on empty queue
if(this.isEmpty())
return "Underflow";
return this.items.shift();
}
For moreinformation on how Queue works visit [https://www.geeksforgeeks.org/implementation-queue-javascript/].
Linked list
Similar to arrays, stack and queue, the linked list data structure is also a linear data structure. Here elements are not stored in a particular memory. They are stored in a separate object that contains a pointer to the next object in that list.
It always has an entry point called the head and which is a reference to the first node in the linked list. The last node on the list points to null.
Null reference means that the list is empty.
An illustration of how Linked list is as below :
`const list = {
head: {
value: 6
next: {
value: 10
next: {
value: 12
next: {
value: 3
next: null
}
}
}
}
}
};
`
To read more on types of linked lists, checkout this link [https://www.freecodecamp.org/news/implementing-a-linked-list-in-javascript/].
Posted on February 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.