class Queue {
constructor () {
this.queue = [];
this.front = 0;
this.rear = 0;
}
enqueue(value
this.queue[this.rear++] = value;
}
dequeue(){
cosnt value = this.queue[this.front];
delete this.queue[this.front];
this.front += 1
return value
}
peek(){
return this.queue[this.front]
}
size(){
return this.rear - this.front
}
}
Linked List
class Node {
constructor(value){
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.size= 0;
}
enqueue(newValue) {
const newNode = new Node(newValue);
if (this.head === null) {
this.head = this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.size += 1;
}
dequeue() {
const value = this.head.value;
this.head = this.head.next;
this.size -= 1;
return value
}
peek() {
return this.head.value
}
}
shift 사용하지말것
자바스크립트에서 shift는 선형시간이 소요되기 때문에 x
const queue = [1,2,3];
queue.push(4);
const value = queue.shift() // O(n)!
console.log(value)