JavaScript Data Structures - 9 - Queue Implementation

preview_player
Показать описание

📱 Follow Codevolution

Queue Implementation
JavaScript Data Structures
Data Structures in JavaScript
Рекомендации по теме
Комментарии
Автор

if you are going to teach data structure and algo, don't use the built in functions.
so that the beginner knows how to pop, push to array by logical insight

Dracometeor
Автор

// no build in function
class Queue {
constructor() {
this.arrQueue = [];
this.front = -1;
this.rear = -1;
}
enqueue(element) {
if (this.front == -1 && this.rear == -1) {
this.front = this.rear = 0;
this.arrQueue[this.front] = element;
} else {
this.arrQueue[++this.rear] = element;
}
}
isEmpty() {
return this.front == -1 && this.rear == -1;
}
dequeue() {
if (this.isEmpty()) {
console.log("you should add elements first then dequeue");
}

if (this.front == this.rear) {
this.front = this.rear = -1;
this.arrQueue.length = 0;
} else {
return this.arrQueue[this.front++];
}
}
size() {
return this.arrQueue.length;
}
print() {
if (this.isEmpty()) {
console.log("the Queue is Empty : ");
} else {

for (let i = this.front; i <= this.rear; i++) {

}
}
}
peek() {
if (!this.isEmpty()) {
return this.arrQueue[this.front];
}
}
}

let q = new Queue();
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);
q.enqueue(8);
q.dequeue();
q.dequeue();
console.log(q);
q.print();
console.log("the top element is : ", q.peek());
console.log(q);
q.dequeue();
q.dequeue();


console.log(`Queue Empty status : ${q.isEmpty()} `);

Ayoubmajid-uuyv
Автор

Hi, thanks for series, one question here, u have started algo and DS playlist for JS separately , what should I learn first, algo or DS ..m can u pls suggest....

Learning-king
Автор

why do you console.log(queue.dequeue())? it doesn't return anything.

hiluftesfay