Skip to content

Commit

Permalink
Adressing issue Yet-Another-Series#103 - Added js-queues.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pnshiralkar committed Oct 16, 2019
1 parent 19e15a2 commit fa5057a
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Data_Structures/queues/js-queues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Queue_DataStructure implementation using dynamic memory allocation
Methods :
Push(value) - Push value into the Queue
Pop() - Delete front element of Queue
Front() - Get the front element element
isEmpty() - Returns Boolean whether Queue is empty (true) or not (false)
*/

// Class for node with members for value and next node
class Node{
constructor(v){
this.val = v;
this.nxt=null;
}
}

// Class Queue
class Queue{
constructor(){
this.first=null;
this.last=null;
}

// Add element from back
push(n){
var newElem = new Node(n);
if(this.last){
this.last.nxt = newElem;
this.last = newElem
}
else{
this.last=newElem;
this.first=newElem;
}
}

// Delete element from front
pop(){
if(this.first){
this.first = this.first.nxt;
}
else
throw new Error("Queue Empty!");
}

// Get front element
front(){
if(!this.isEmpty())
return this.first.val;
else
throw new Error("Queue Empty!");
}

// Check if Queue is empty
isEmpty(){
return !(this.first);
}

}



// Sample implementation :



// Create a new Queue
var q = new Queue;

// Push 10, 20 and 30
q.push(10);
q.push(20);
q.push(30);
console.log(q.front()); // Print front element
q.push(40); // Insert 40
q.pop(); // Pop element
console.log(q.front()); // Print front element
// Pop elements
q.pop();
q.pop();
console.log(q.front()); // Print front element
q.pop(); // Pop element

0 comments on commit fa5057a

Please sign in to comment.