Implementing queue in javascript

 

Queue in Javascript
 
Inside the world computer science, a queue is a collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue.
This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed.
This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.
Queues* provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer.

The process of adding the data is called as enqueuing

The process  of deleting the data is called as dequeuing

Implementing Queue in javascript

 

Queue follows : FIFO (First In First out)

Add To queue : Array.unshift()

Remove from queue : Array.pop()

Operation :

const q = new Queue();

q.add(1);

q.remove()

 

Implementation in ES6:

class Queue{

constructor(){

this.data  = [];

}

add(record){

this.data.unshift(record);

}

remove(){

return this.data.pop();

}

peek(){//remove method , but it is not actually removing 

return this.data[this.data.length-1];

}

}

 

Weave in queue.

Here we creating a function weave(). The weave function containing 2 different queues as an arguments. We have to merge both the queues into a single queue

Create Queue.js

class Queue{

constructor(){
this.data= [];
}
 
add(record){
this.data.unshift(record);
}
 
remove(){
returnthis.data.pop();
}
 
peek(){
returnthis.data[this.data.length-1];
}
 
}
 
module.exports=Queue;

 

Now import the above file inside the weave.js

 

var Queue = require('./queue');
function weave(source1,source2){
var obj = new Queue();
while(source1.peek() || source2.peek()){

if(source1.peek()){
obj.add(source1.remove());
}

if(source2.peek()){
obj.add(source2.remove())
}

}
return obj;
}

var source1 = new Queue();
source1.add(100);
source1.add(200);
source1.add(300);
source1.add(400);
var source2 = new Queue();
source2.add(1000);
source2.add(2000);
source2.add(3000);
source2.add(4000);
var res = weave(source1,source2);
console.log("The final destination is ",res);

 

 

Implementing the stack in javascript

 

Stack

<html>
<script>
class Stack {
constructor(){
this.data= [];
}

push(record){
this.data.push(record);
}

pop(){
returnthis.data.pop();
}

peek(){
returnthis.data[this.data.length-1];
}
}
</script>
</html>

Now, implements queue from stack.  Create the queue.js

To implement the queue from the stack , we need to use 2 stacks for it.

 

var Stack = require('./stack');
class Queue{
constructor(){
this.first=newStack();
this.second=newStack();
}

add(record){
this.first.push(record);
}

remove(){
while(this.first.peek()){
this.second.push(this.first.pop())
}
var record=this.second.pop();
while(this.second.peek()){
this.first.push(this.second.pop())
}
return record;
}


peek(){
while(this.first.peek()){
this.second.push(this.first.pop())
}
var record=this.second.peek();
while(this.second.peek()){
this.first.push(this.second.pop())
}
return record;
}

}

 

Leave a reply:

Your email address will not be published.