Javascript Data Structure Queue 2

Description

Javascript Data Structure Queue 2

function Queue() { 
   this.dataStore = []; /*w ww.  j a va 2 s. c  o  m*/
   this.enqueue = enqueue; 
   this.dequeue = dequeue; 
   this.front = front; 
   this.back = back; 
   this.toString = toString; 
   this.empty = empty; 
} 

function enqueue(element) { 
   this.dataStore.push(element); 
} 

function dequeue() { 
   return this.dataStore.shift(); 
} 

function front() { 
   return this.dataStore[0]; 
} 

function back() { 
   return this.dataStore[this.dataStore.length-1]; 
} 

function toString() { 
   let retStr = ""; 
   for (let i = 0; i < this.dataStore.length; ++i) { 
      retStr += this.dataStore[i] + "\n"; 
   } 
   return retStr; 
} 

function empty() { 
   if (this.dataStore.length == 0) { 
      return true; 
   } 
   else { 
      return false; 
   } 
} 

// test program 

let q = new Queue(); 
q.enqueue("A"); 
q.enqueue("B"); 
q.enqueue("C"); 
console.log(q.toString()); 
q.dequeue(); 
console.log(q.toString()); 
console.log("Front of queue: " + q.front()); 
console.log("Back of queue: " + q.back()); 



PreviousNext

Related