Skip to content

Instantly share code, notes, and snippets.

@Jagathishrex
Last active February 6, 2020 19:51
Show Gist options
  • Save Jagathishrex/d6c9b4688b9f996ca0c49a90ead09f08 to your computer and use it in GitHub Desktop.
Save Jagathishrex/d6c9b4688b9f996ca0c49a90ead09f08 to your computer and use it in GitHub Desktop.
class Queue {
constructor(){
this.data = [];
this.rear = 0;
this.size = 10;
}
enqueue(element) {
if(this.rear < this.size ) {
this.data[this.rear] = element;
this.rear = this.rear + 1;
}
}
length() {
return this.rear;
}
isEmpty() {
return this.rear === 0;
}
getFront() {
if(this.isEmpty() === false) {
return this.data[0];
}
}
getLast() {
if(this.isEmpty() === false) {
return this.data[ this.rear - 1 ] ;
}
}
dequeue() {
if(this.isEmpty() === false) {
this.rear = this.rear-1;
return this.data.shift();
}
}
print() {
for(let i =0; i < this.rear; i++) {
console.log(this.data[i]);
}
}
clear() {
this.data.length = 0;
this.rear = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment