Skip to content

Commit 70c6509

Browse files
committed
add: queue 구현 코드
1 parent 32e3d77 commit 70c6509

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/kyu9341/queue/queue.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Node {
2+
constructor(data) {
3+
this.data = data;
4+
this.next = null;
5+
}
6+
}
7+
8+
class Queue {
9+
constructor() {
10+
this.head = null;
11+
this.tail = null;
12+
this.size = 0;
13+
}
14+
15+
push(data) {
16+
const node = new Node(data);
17+
if (this.isEmpty()) {
18+
this.head = node;
19+
this.head.next = this.tail;
20+
} else this.tail.next = node;
21+
22+
this.tail = node;
23+
this.size += 1;
24+
}
25+
26+
pop() {
27+
if (this.isEmpty()) return null;
28+
if (this.head === this.tail) this.tail = null;
29+
30+
const headData = this.head.data;
31+
this.head = this.head.next;
32+
this.size -= 1;
33+
34+
return headData;
35+
}
36+
37+
isEmpty() {
38+
return this.size === 0;
39+
}
40+
41+
getArray() {
42+
const array = [];
43+
if (this.isEmpty()) return array;
44+
45+
let currentNode = this.head;
46+
while (currentNode) {
47+
array.push(currentNode.data);
48+
currentNode = currentNode.next;
49+
}
50+
51+
return array;
52+
}
53+
}

0 commit comments

Comments
 (0)