Created
August 10, 2023 18:05
-
-
Save Deshan555/6da9ab89188950fd14f969b2559cb542 to your computer and use it in GitHub Desktop.
Queue Implementation of Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Enqueue: Add an element to the end of the queue | |
| Dequeue: Remove an element from the front of the queue | |
| IsEmpty: Check if the queue is empty | |
| IsFull: Check if the queue is full | |
| Peek: Get the value of the front of the queue without removing it | |
| */ | |
| class queue{ | |
| constructor() { | |
| this.elements = {}; | |
| this.head = 0; | |
| this.tail = 0; | |
| } | |
| enqueue(data) { | |
| this.elements[this.tail] = data; | |
| this.tail++; | |
| return data+' inserted'; | |
| } | |
| dequeue(){ | |
| const item = this.elements[this.head]; | |
| delete this.elements[this.head]; | |
| this.head++; | |
| return item; | |
| } | |
| get length(){ | |
| return this.tail - this.head; | |
| } | |
| get isEmpty(){ | |
| return this.length === 0; | |
| } | |
| peek() { | |
| return this.elements[this.head] | |
| } | |
| get printQueue(){ | |
| return this.elements; | |
| } | |
| } | |
| const queue_ = new queue() | |
| console.log(queue_.enqueue(7)); | |
| console.log(queue_.enqueue(10)); | |
| console.log(queue_.dequeue()); | |
| console.log(queue_.length); | |
| console.log(queue_.isEmpty); | |
| console.log(queue_.peek()); | |
| console.log(queue_.printQueue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment