Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Queue en JS

class Queue {
  constructor() {
    this.elements = {};
    this.head = 0;
    this.tail = 0;
  }
  enqueue(element) {
    this.elements[this.tail] = element;
    this.tail++;
  }
  dequeue() {
    const item = this.elements[this.head];
    delete this.elements[this.head];
    this.head++;
    return item;
  }
  peek() {
    return this.elements[this.head];
  }
  get length() {
    return this.tail - this.head;
  }
  get isEmpty() {
    return this.length === 0;
  }
}
Code language: JavaScript (javascript)
Comment

javascript queue

var stack = [];
stack.push(2);       // stack is now [2]
stack.push(5);       // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i);            // displays 5

var queue = [];
queue.push(2);         // queue is now [2]
queue.push(5);         // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i);              // displays 2
taken from "9 javascript tips you may not know"
Comment

javascript queue

var stack = [];
stack.push(2);       // stack is now [2]
stack.push(5);       // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i);            // displays 5

var queue = [];
queue.push(2);         // queue is now [2]
queue.push(5);         // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i);              // displays 2
Comment

javascript queue

// We create a class for each node within the queue
class Node {
    // Each node has two properties, its value and a pointer that indicates the node that follows
    constructor(value){
        this.value = value
        this.next = null
    }
}

// We create a class for the queue
class Queue {
    // The queue has three properties, the first node, the last node and the stack size
    constructor(){
        this.first = null
        this.last = null
        this.size = 0
    }
    // The enqueue method receives a value and adds it to the "end" of the queue
    enqueue(val){
        var newNode = new Node(val)
        if(!this.first){
            this.first = newNode
            this.last = newNode
        } else {
            this.last.next = newNode
            this.last = newNode
        }
        return ++this.size
    }
    // The dequeue method eliminates the element at the "beginning" of the queue and returns its value
    dequeue(){
        if(!this.first) return null

        var temp = this.first
        if(this.first === this.last) {
            this.last = null
        }
        this.first = this.first.next
        this.size--
        return temp.value
    }
}

const quickQueue = new Queue

quickQueue.enqueue("value1")
quickQueue.enqueue("value2")
quickQueue.enqueue("value3")

console.log(quickQueue.first) /* 
        Node {
            value: 'value1',
            next: Node { value: 'value2', next: Node { value: 'value3', next: null } }
        }
    */
console.log(quickQueue.last) // Node { value: 'value3, next: null }
console.log(quickQueue.size) // 3

quickQueue.enqueue("value4")
console.log(quickQueue.dequeue()) // value1
Comment

queue javascript

 printElements() {

      let currentNode = this.front;
      let output ='';

      while (currentNode) {
     output = ` ${output}${currentNode.value} ->  ` ;

          currentNode = currentNode.next;
      }
      console.log(`${output}null`);
      return true
  }
}

//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
Comment

javascript queue

class Queue {
  constructor() {
    this.records = [];
  }
  
  enqueue(record) {
    this.records.unshift(record);
  }
  
  dequeue() {
    return this.records.pop();
  }
  
  peek() {
    return this.records[this.records.length-1];
  }
  
  print() {
   console.log('The queue records are -', this.records); 
  }
}
Comment

enqueue method in queue with JavaScript

 enqueue(element) {

this.elements[this.tail] = element;
    this.tail++;
    return element;


  }
Comment

PREVIOUS NEXT
Code Example
Javascript :: js propagation stop 
Javascript :: get select2 selected value jquery 
Javascript :: getting form values in javascript 
Javascript :: js element text color 
Javascript :: javascript assign 
Javascript :: how to convert json result into datatable c# 
Javascript :: javascript style multiple properties 
Javascript :: exit from fullscreen 
Javascript :: javascript redirect to home page 
Javascript :: uncaught evalerror: refused to evaluate a string as javascript because 
Javascript :: on focus jquery 
Javascript :: javascript select first n elements from array 
Javascript :: process.env in nextjs 
Javascript :: object keys javascript 
Javascript :: how to check if a json object is empty 
Javascript :: how to use secondary color in material ui useStyle 
Javascript :: checkbox event listener 
Javascript :: touppercase 
Javascript :: npm run shell script 
Javascript :: push characters to a string javascript 
Javascript :: semantics ui complete responsive menu 
Javascript :: first and last char vowel reg exp same char 
Javascript :: set windows terminal as default vscode 
Javascript :: can i pass data with usenavigate react router 
Javascript :: ReferenceError: primordials is not defined 
Javascript :: javascript update local storage array 
Javascript :: array of objects to array 
Javascript :: javascript sort array by index 
Javascript :: dropzone get response 
Javascript :: vaidate youtube url 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =