-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue2.js
More file actions
42 lines (41 loc) · 797 Bytes
/
queue2.js
File metadata and controls
42 lines (41 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//객체로 접근
class Queue {
constructor() {
this.storage = {}
this.front = 0
this.rear = 0
}
add(value) {
if (this.size() === 0) {
this.storage['0'] = value
} else {
this.rear += 1
this.storage[this.rear] = value
}
}
popLeft() {
let temp
if (this.front === this.rear) {
temp = this.storage[this.front]
delete this.storage[this.front]
this.front = 0
this.rear = 0
} else {
temp = this.storage[this.front]
delete this.storage[this.front]
this.front += 1
}
return temp
}
size() {
if (this.storage[this.rear] === undefined) {
return 0
} else {
return this.rear - this.front + 1
}
}
}
const test = new Queue()
test.add(1)
test.add(1)
console.log(test)