Javascript Array Stack Queue








Javascript Array type provides methods for us to use array objects as a stack or queue.

A stack is a data structure where we can only access its elements from one side. It is a first-in-last-out structure.

A queue is a data structure where we can add elements in one side and access elements in the other side. It is a first-in-first-out structure.

Stack Methods

Javascript array can behave like a stack.

A stack is a last-in-first-out (LIFO) structure, meaning that the most recently added item is the first one removed.

The insertion and removal of items in a stack occur at only one point: the top of the stack. For a stack the inserting is called a push, while the deleting is called a pop.

Javascript arrays provide push() and pop() specifically to allow stack-like behavior.

The push() method accepts any number of arguments and adds them to the end of the array, returning the array's new length.

The pop() method removes the last item in the array, decrements the array's length, and returns the removed item.





Stack Methods Example


var colors = new Array();               //create an array
var count = colors.push("A", "B");      //push two items
console.log(count);  //2
console.log(colors);// ww  w  .j  a va 2 s.co  m

count = colors.push("black");           //push another item on
console.log(count);  //3
console.log(colors);

var item = colors.pop();                //get the last item
console.log(item);   
console.log(colors.length);  //2
console.log(colors);

The code above generates the following result.





Queue Methods

Queues restrict access in a first-in-first-out (FIFO) data structure.

A queue adds items to the end and retrieves items from the front.

shift() from array removes the first item in the array and returns it, decrementing the length of the array by one. push() method adds items to the end of an array.

We can use shift() and push() to treat arrays as queues.

Queue Methods Example


var colors = new Array();                //create an array
var count = colors.push("A", "B");       //push two items
console.log(count);  //2
console.log(colors);/*  w  w w .j ava2s .c o  m*/

count = colors.push("black");           //push another item on
console.log(count);  //3
console.log(colors);

var item = colors.shift();              //get the first item
console.log(item);                            
console.log(colors.length);  //2
console.log(colors);

The code above generates the following result.

Reversed queue

unshift() adds any number of items to the front of an array and returns the new array length.

Combining unshift() and pop(), we can use array as reversed queue , where new values are added to the front of the array and values are retrieved off the back.


var colors = new Array();               //create an array
var count = colors.unshift("A", "B");   
console.log(count);  //2
console.log(colors);/*from   ww  w .  j ava2  s.c  o m*/

count = colors.unshift("black");        
console.log(count);  //3
console.log(colors);

var item = colors.pop();               //get the first item
console.log(item);  
console.log(colors.length);  //2
console.log(colors);

The code above generates the following result.