JavaScript program to implement queue using stack Last Updated : 16 May, 2024 Comments Improve Suggest changes Like Article Like Report A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to the stack is the first one to be removed. The different operations associated with stack include push, pop, peek etc. ApproachWe need two Stack to implement the queue because the Queue is First In First Out (FIFO) data structure and the Stack is Last In, First Out (FIFO) i.e. the first element added to the queue is the first one to be removed and the last element added to the stack is the first one to be removed. Define a class named QueueUsingStack. Inside the constructor, initialize two empty arrays stack1 and stack2. These arrays will serve as the two stacks required for implementing the queue.Implement the enqueue(item) method to add elements to the queue. Simply push the item onto stack1.Implement the dequeue() method to remove and return the oldest element from the queue. If stack2 is empty, pop all elements from stack1 and push them onto stack2. Then, pop and return the top element from stack2.Implement the peek() method to return the oldest element from the queue without removing it. Similar to dequeue, if stack2 is empty, first move all elements from stack1 to stack2. Then, return the top element of stack2. Implement the isEmpty() method to check if the queue is empty. The queue is empty when both stack1 and stack2 are empty.Example: To demonstrate implementation of queue using stack. JavaScript class QueueUsingStack { constructor() { this.stack1 = []; this.stack2 = []; } // Enqueue operation enqueue(item) { // Push the item into stack1 this.stack1.push(item); } // Dequeue operation dequeue() { // If stack2 is empty, pop all elements // from stack1 and push into stack2 if (this.stack2.length === 0) { while (this.stack1.length > 0) { this.stack2.push(this.stack1.pop()); } } // Pop the top element from stack2 // (which is the oldest element in the queue) return this.stack2.pop(); } // Peek operation peek() { // If stack2 is empty, pop all elements // from stack1 and push into stack2 if (this.stack2.length === 0) { while (this.stack1.length > 0) { this.stack2.push(this.stack1.pop()); } } // Peek at the top element // of stack2 without removing it return this.stack2[this.stack2.length - 1]; } // Check if the queue is empty isEmpty() { // Both stack1 and stack2 should be // empty for the queue to be empty return this.stack1.length === 0 && this.stack2.length === 0; } } const queue = new QueueUsingStack(); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); console.log(queue.dequeue()); console.log(queue.peek()); console.log(queue.isEmpty()); Output1 2 false Time complexity: O(1) Space complexity: O(n) Comment More infoAdvertise with us Next Article JavaScript program to implement queue using stack bug8wdqo Follow Improve Article Tags : JavaScript Web Technologies JavaScript-DSA Similar Reads JavaScript program to implement stack using queue In this article, we implement a JavaScript program to make a stack using a queue data structure. It provides essential stack methods like push(), pop(), and peek(), isEmpty() operations, utilizing either one or two queues to simulate the behavior of a stack. Examples: Input:push(2)push(3)pop()peek() 4 min read C++ Program to Implement Queue using Linked List Queue is the fundamental data structure that follows the First In, First Out (FIFO) principle where the elements are added at the one end, called the rear and removed from other end called the front. In this article, we will learn how to implement queue in C++ using a linked list. Queue Using Linked 5 min read C++ Program to Implement Stack using array Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both 4 min read C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu 8 min read Java Program to Implement the Queue Data Structure Queue is the fundamental data structure that follows the First-In-First-Out(FIFO) principle where the element that is inserted first is one that gets removed first. Imagine the queue of the people waiting in the line at the ticket counter: the person who arrives the first gets served first and so on 4 min read How to implement Stack and Queue using ArrayDeque in Java ArrayDeque in Java The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both si 6 min read Java Program to Implement Stack Data Structure Stack is the fundamental Data Structure that can follow the Last In, First Out(LIFO) principle. It can work that the last element added to the stack will be the first one to be removed. It can operate like a stack of plates: We can only add or remove the topmost plate at any given time. The simplici 5 min read Implement enqueue and dequeue using only two stacks in JavaScript ? In this article, we will implement queue's operations (enqueue and dequeue) using only two stacks (in the form of plain arrays) in JavaScript. Before directly jumping into understanding the problem statement let us understand briefly what exactly a stack is and also what exactly a queue is. A stack 6 min read Queue Implementation Using Linked List in Java Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the cons 4 min read Most efficient way to implement Stack and Queue together Introduction to Stack:A stack is a linear data structure in computer science that follows the Last-In-First-Out (LIFO) principle. It is a data structure in which the insertion and removal of elements can only be performed at one end, which is called the top of the stack.In a stack, elements are push 15+ min read Like