JavaScript Stack and Queue
This tutorial will discuss the implementation of stack and queue using array in JavaScript.
Implement Stack and Queue Using Array in JavaScript
We can implement stack and queue using array and linked lists in JavaScript. Stack works on the first in last out principle, which means the first element added to the stack will be removed after removing all the other elements and vice versa. Queues work on the first-in, first-out principle, which means the first element added to the stack will be removed first and vice versa. Implementing stack and queue with the array is easy as compared to linked lists because we can get many predefined functions to use. For example, to implement stack, we can use the push()
and pop()
function. The push()
function is used to add an element at the end of a stack or array, and the pop()
function is used to remove an element from the end of the stack or array. Let’s implement a stack using an array in JavaScript. See the code below.
var MyStack = [];
MyStack.push(1);
MyStack.push(9);
console.log('Stack before pop', MyStack);
MyStack.pop();
console.log('Stack after pop', MyStack);
Output:
Stack before pop (2) [1, 9]
Stack after pop [1]
In the above code, we use the push()
function to add two values, 1 and 9, into the stack, and you can see in the output the two values are stored in the stack. We used the pop()
function to remove the last element from the stack, and you can see in the output the last element has been removed from the stack. You can check the size of the stack using the length
function. The stack implementation performance with the array is quite high because we are just adding an element at the end of the array also removing it from the end. You can also implement a stack with linked lists, but you will not find any prebuild functions to help you. Now, Let’s implement a queue using array , push()
and shift()
function. The push()
function is used to add an element at the end of the queue or array, and the shift()
function is used to remove an element from the beginning of the stack or array. See the below code.
var MyQueue = [];
MyQueue.push(1);
MyQueue.push(9);
console.log('Queue before shift', MyQueue);
MyQueue.shift();
console.log('Queue after shift', MyQueue);
Output:
Queue before shift (2) [1, 9]
Queue after shift [9]
In the above code, we use the push()
function to add two values, 1 and 9, into the queue, and you can see in the output the two values are stored in the queue. We used the shift()
function to remove the first element from the queue, and you can see in the output the first element has been removed from the queue. You can check the size of the queue using the length
function. The performance of the queue build with the array is quite low because when the shift()
functions remove the first element, it has to move each element one step back in the array. So, if you want to implement a queue containing a large number of elements, then you should use the linked list to implement a queue.
Related Article - JavaScript Array
- How to Check if Array Contains Value in JavaScript
- How to Create Array of Specific Length in JavaScript
- How to Convert Array to String in JavaScript
- How to Remove First Element From an Array in JavaScript
- How to Search Objects From an Array in JavaScript
- How to Convert Arguments to an Array in JavaScript