Skip to content

Instantly share code, notes, and snippets.

@ifndefdeadmau5
Created October 13, 2016 10:48
Show Gist options
  • Save ifndefdeadmau5/ebd816c123b0a2a5724a470de7187e8c to your computer and use it in GitHub Desktop.
Save ifndefdeadmau5/ebd816c123b0a2a5724a470de7187e8c to your computer and use it in GitHub Desktop.
Queue with array
// Queue 구현
// Stack 구현
// Stack 2개로 Queue 구현
class Queue {
constructor() {
this.size = 5;
this.array = [];
this.front = 0;
this.rear = 0;
}
push(val) {
if(this.isFull())
return false;
if(this.rear === this.size)
this.rear = 0;
else {
this.array[this.rear] = val;
this.rear++;
}
}
pop() {
if(this.isEmpty())
return false;
if(this.front === this.size)
this.front = 0;
else {
this.array[this.rear] = 0;
this.front++;
}
}
isEmpty() {
return this.front === this.rear;
}
isFull() {
if(this.front < this.rear)
return (this.rear - this.front) === this.size;
else
return this.front + 1 === this.rear;
}
peek() {
return this.array[this.front];
}
}
let myQ = new Queue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment