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 :: use promise in angular 8 
Javascript :: usereduce 
Javascript :: jq not contains 
Javascript :: email validation in javascript 
Javascript :: type time angular 
Javascript :: fs readfile promise 
Javascript :: random name 
Javascript :: event in javascript 
Javascript :: oops in js 
Javascript :: chai js 
Javascript :: nextjs sitemap generator 
Javascript :: why node_modules are not installed anymore 
Javascript :: notification react native 
Javascript :: type of jvascript data 
Javascript :: how to use paystack with react 
Javascript :: return value 
Javascript :: jquery basics 
Javascript :: java script 
Javascript :: do while loop javascript 
Javascript :: Modify String with Uppercase 
Javascript :: what is random state 
Javascript :: binarysearch 
Javascript :: What is cookies, sessionStorage, localStorage. 
Javascript :: nodejs ecommerce cms 
Javascript :: dynamic routing 
Javascript :: break loop if condition is met 
Javascript :: conditional rendering react 
Javascript :: while loop javascript 
Javascript :: json.stringify file object return {} 
Javascript :: what is node in selenium grid 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =