Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

circular queue implementation using js

class CircularQueue {
    constructor(size) {
        this.element = [];
        this.size = size 
        this.length = 0 
        this.front = 0 
        this.back = -1
    }
    isEmpty() {
        return (this.length == 0)
    }
    enqueue(element) {
        if (this.length >= this.size) 
            throw (new Error("Maximum length exceeded")) 
        this.back++
        this.element[this.back % this.size] = element 
        this.length++
    }
    dequeue() {
        if (this.isEmpty())
                throw (new Error("No elements in the queue"))
        const value = this.getFront()
        this.element[this.front % this.size] = null 
        this.front++
        this.length--
        return value
    }
    getFront() {
        if (this.isEmpty()) 
            throw (new Error("No elements in the queue")) 
        return this.element[this.front % this.size]
    }
    clear() {
        this.element = new Array();
        this.length = 0 
        this.back = 0 
        this.front = -1
    }
}
Comment

Circular Queue in javascript

class CircularQueue { constructor(size) {  this.element = [];  this.size = size  this.length = 0  this.front = 0  this.back = -1 }isEmpty() {  return (this.length == 0) }enqueue(element) {  if (this.length >= this.size) throw (new Error("Maximum length exceeded"))  this.back++   this.element[this.back % this.size] = element  this.length++ }dequeue() {  if (this.isEmpty()) throw (new Error("No elements in the queue"))  const value = this.getFront()  this.element[this.front % this.size] = null  this.front++  this.length--  return value }getFront() {  if (this.isEmpty()) throw (new Error("No elements in the queue"))  return this.element[this.front % this.size] }clear() {  this.element = new Array()  this.length = 0  this.back = 0  this.front = -1 }}
Comment

PREVIOUS NEXT
Code Example
Javascript :: datatable table header not responsive 
Javascript :: black adam release date 
Javascript :: find max number in java 
Javascript :: curl send json as variable 
Javascript :: js set iframe code 
Javascript :: lodash find 
Javascript :: js sleep 1 sec 
Javascript :: ANGULAR locale fr 
Javascript :: jquery get fail 
Javascript :: get all object key names 
Javascript :: ip address validation regex angular 
Javascript :: mongoose findone multiple conditions 
Javascript :: cheerio example 
Javascript :: create javascript array with 1 value 
Javascript :: javascript await return value 
Javascript :: date without seconds react 
Javascript :: how to manage logging using winston for production and development in node js "github" 
Javascript :: class javascript 
Javascript :: joi regex validate 
Javascript :: how to create date object with specific time in moment js 
Javascript :: react hook form reset only one field 
Javascript :: js find in array 
Javascript :: Beautifule JS Console Log 
Javascript :: setProps jest 
Javascript :: use queryselectro to select by form name 
Javascript :: react native docs 
Javascript :: angular how to copy text with button 
Javascript :: javascript limit number of lines in div 
Javascript :: reactjs date display 
Javascript :: Format of fetch 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =