Implementation of Queue using Linked List | JavaScript
queue using linked list | Implementation of Queue using Linked List | Queue in Javascript | queue js | javascript queue implementation | javascript queue data structure | queue data structure javascript | javascript queue example
The queue rule says that insertion takes place at one end and deletion takes place at the other end, i.e., First In, First Out(FIFO).
A queue is a linear data structure
that follows the First in, First out principle(FIFO).
It can be implemented using an array and linked list. The benefit of implementing a queue using a linked list over arrays is that it allows to grow the Queue as per the requirements, i.e., memory can be allocated dynamically.
A good example of a Queue is a Ticket window, where the customer who comes first will be served first.
Queue = [4, 5, 3, 1, 9, 6]
-> Entry -> Exit
(Front) (Rear)
(Enqueue) (Dequeue)
Operations on Queue –
1. enqueue(x) = Insert element at rear end
2. dequeue() = Remove element from front side
3. isEmpty() = Check queue is empty or not
4. front() = Get front element
5. rear() = Get rear element
Implementation of Queue using LinkedList
In Linked List Implementation, We will track the Head and Tail both nodes for processing.
Example-
enqueue(2)
enqueue(4)
enqueue(7)
dequeue()
dequeue()
enqueue(5)
[2]
head / tail
[2] => [4]
head tail
[2] => [4] => [7]
head tail
[4] => [7]
head tail
[7]
head / tail
[7] => [5]
head tail
Create a Node Class
class Node {
data;
next;
constructor(value) {
this.data = value;
this.next = null;
}
}
Queue Class Design
class Queue {
head = null;
tail = null;
constructor() { }
enqueue(value) {
const node = new Node(value);
if (this.head == null) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node; // make new node to current tail next element
this.tail = node; // now make tail to last newly added element
}
}
dequeue() {
if (this.head == null) {
return null;
}
// we know that we have to remove node from front :: And front is head so just make head.next to head
let node = this.head;
if (this.head.next == null) { // means there is only one element
this.head = null;
this.tail = null;
} else {
this.head = this.head.next;
}
return node.data;
}
isEmpty() {
return this.head == null;
}
front() {
return this.head ? this.head.data : -1;
}
rear() {
return this.tail ? this.tail.data : -1;
}
}
Run above Code
const q = new Queue();
q.enqueue(3);
q.enqueue(7);
q.enqueue(9);
q.enqueue(11);
q.enqueue(13);
console.log(q.head);
{
"data": 3,
"next": {
"data": 7,
"next": {
"data": 9,
"next": {
"data": 11,
"next": {
"data": 13,
"next": null
}
}
}
}
}
console.log(q.front()); ===> 3
console.log(q.rear()); ===> 13
console.log(q.isEmpty()); ===> false
q.dequeue(11);
q.dequeue(3);
q.dequeue(13);
console.log(q.head);
{
"data": 11,
"next": {
"data": 13,
"next": null
}
}
Time Complexity
The time complexity of both operations enqueue() and dequeue() is O(1)
as it only changes a few pointers in both operations.
Space Complexity is also O(1).
Implementation of Queue using Linked List | Queue in Javascript | queue js | javascript queue implementation | javascript queue data structure | queue data structure javascript | javascript queue example
- CSS Display Property – Deep dive in block | inline | inline-block
- Implementation of Queue using Linked List | JavaScript
- Insert node in Linked list | Algorithm | JavaScript
- Insertion Sort in data structure | Algorithm with Examples
- Selection Sort Algorithm & K’th Largest Element in Array
- Quick Sort Algorithm with example | Step-by-Step Guide
- Dependency Inversion Principle with Example | Solid Principles
- Object-Oriented Programming | Solid Principles with Examples
- ASCII Code of Characters | String Operations with ASCII Code
- Negative Binary Numbers & 2’s Complement | Easy explanation
- Factors of a Number | JavaScript Program | Optimized Way
- LeetCode – Game of Life Problem | Solution with JavaScript
- Fibonacci series using Recursion | While loop | ES6 Generator
- JavaScript Coding Interview Question & Answers
- LeetCode – Coin Change Problem | Dynamic Programming | JavaScript
- HackerRank Dictionaries and Maps Problem | Solution with JavaScript
- React Redux Unit Testing of Actions, Reducers, Middleware & Store
- Micro frontends with Module Federation in React Application
- React Interview Question & Answers – Mastering React
- Top React Interview Question & Answer | React Routing
- React Interview Questions and Answers | React hooks
- Higher Order Component with Functional Component | React JS
- Top React Interview Questions and Answers | Must Know
- Interview Question React | Basics for Freshers and Seniors
- Cyber Security Fundamental Questions & Answers You must know
- Application & Web Security Interview Questions & Answers
- Top Scrum Master and Agile Question & Answers 2022
- Trapping Rain Water Leetcode Problem Solution
- Array Representation of Binary Tree | Full Tree & Complete Binary Tree
- Graphs in Data Structure, Types & Traversal with BFS and DFS, Algorithms
- Traversing 2 D array with BFS & DFS Algorithm in JavaScript
- Time Complexity & Calculations | All You should know
- Backspace String Compare Leetcode Problem & Solution
- Angular Interview Questions & Answers 2021 – Part 3 – JS Mount
- Why Angular is a Preferred Choice for Developers? Top Features
- Angular Interview Questions & Answers You should know Part 2
- Top 30 JavaScript Interview Questions and Answers for 2021
- React JS Stripe Payment Gateway Integration with Node | Step by Step Guide
- Create Year Month & Date dropdown List using JavaScript & JQuery
- Create Custom QR Code Component using QR Code Styling in React JS
- How to create a common Helper class or util file in React JS
- React Build Routing with Fixed Header and Navigation | React Router Tutorial
- React Create Dashboard Layout with Side Menu, Header & Content Area
- Web Application Security Best Practices | Top Tips to Secure Angular App
- HTML Form with Pure CSS & JavaScript | Custom Radio and Checkbox
- NgRx Top Interview Questions and Answers You should know
- Top 40 Awesome CSS Interview Questions & Answers You should know | CSS Tutorial
- Tips to Boost Angular App Performance | Web page Speed Optimization
- JavaScript Rotate 2D matrix 90 degrees clockwise | Top Interview Question
- HashTable Data Structure in JavaScript with Add Delete & Search Algorithms
- Trie Data Structure – Insert Search Delete & Print Program with JavaScript
Read Fibonacci Series Implementation –
https://www.jsmount.com/fibonacci-series-using-recursion-while-loop-es6-generator/
Learn JavaScript Array in detail –
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array