How do you implement a Stack and a Queue in JavaScript?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Implementing a Stack and a Queue in JavaScript

Implementing a Stack and a Queue in JavaScript can be done using arrays or linked lists. Below are simple implementations using arrays.

Stack Implementation

A stack is a data structure that follows the Last In, First Out (LIFO) principle. The most recently added element is the first one to be removed.

Stack using Array

You can use JavaScript arrays and the push and pop methods to implement a stack.

Implementation:

class Stack { constructor() { this.items = []; } // Add an element to the stack push(element) { this.items.push(element); } // Remove and return the top element from the stack pop() { if (this.isEmpty()) { return "Underflow"; } return this.items.pop(); } // Return the top element without removing it peek() { if (this.isEmpty()) { return "No elements in Stack"; } return this.items[this.items.length - 1]; } // Check if the stack is empty isEmpty() { return this.items.length === 0; } // Return the size of the stack size() { return this.items.length; } // Print the elements of the stack printStack() { let str = ""; for (let i = 0; i < this.items.length; i++) { str += this.items[i] + " "; } return str; } } // Example usage: const stack = new Stack(); stack.push(10); stack.push(20); stack.push(30); console.log(stack.printStack()); // Output: 10 20 30 console.log(stack.pop()); // Output: 30 console.log(stack.peek()); // Output: 20 console.log(stack.printStack()); // Output: 10 20

Queue Implementation

A queue is a data structure that follows the First In, First Out (FIFO) principle. The earliest added element is the first one to be removed.

Queue using Array

You can use JavaScript arrays and the push and shift methods to implement a queue.

Implementation:

class Queue { constructor() { this.items = []; } // Add an element to the queue enqueue(element) { this.items.push(element); } // Remove and return the front element from the queue dequeue() { if (this.isEmpty()) { return "Underflow"; } return this.items.shift(); } // Return the front element without removing it front() { if (this.isEmpty()) { return "No elements in Queue"; } return this.items[0]; } // Check if the queue is empty isEmpty() { return this.items.length === 0; } // Return the size of the queue size() { return this.items.length; } // Print the elements of the queue printQueue() { let str = ""; for (let i = 0; i < this.items.length; i++) { str += this.items[i] + " "; } return str; } } // Example usage: const queue = new Queue(); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); console.log(queue.printQueue()); // Output: 10 20 30 console.log(queue.dequeue()); // Output: 10 console.log(queue.front()); // Output: 20 console.log(queue.printQueue()); // Output: 20 30

Summary

  • Stack:

    • Uses the LIFO (Last In, First Out) principle.
    • Basic operations: push, pop, peek, isEmpty, size, printStack.
  • Queue:

    • Uses the FIFO (First In, First Out) principle.
    • Basic operations: enqueue, dequeue, front, isEmpty, size, printQueue.

These implementations use arrays to store the elements and provide the essential operations for both data structures. For more in-depth knowledge and practical examples on data structures and algorithms, consider exploring Grokking the Coding Interview on DesignGurus.io, which offers comprehensive courses on essential coding and interview techniques.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Tech Interview Coaching
What are the 7 steps of the technical writing process?
Master-level strategies for handling complex system design trade-offs
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.