-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10845.js
99 lines (87 loc) · 1.73 KB
/
10845.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
1. 연결 리스트를 이용한 큐
2. 배열을 이용한 가짜 큐
1번 방법 사용
*/
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
isEmpty() {
return this.length === 0 ? 1 : 0;
}
push(value) {
const newNode = new Node(value);
if (this.isEmpty()) {
this.first = newNode;
} else {
this.last.next = newNode;
}
this.last = newNode;
this.length += 1;
}
pop() {
if (this.isEmpty()) return -1;
const { value } = this.first;
this.length -= 1;
if (this.isEmpty()) {
this.last = null;
}
this.first = this.first.next;
return value;
}
size() {
return this.length;
}
front() {
if (this.isEmpty()) return -1;
return this.first.value;
}
back() {
if (this.isEmpty()) return -1;
return this.last.value;
}
}
// input
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const commands = require('fs').readFileSync(INPUT_FILE).toString().trim()
.split('\n')
.slice(1)
.map((line) => line.split(' '));
// process
const queue = new Queue();
const sol = [];
commands.forEach(([command, value]) => {
switch (command) {
case 'push':
queue.push(value);
break;
case 'pop':
sol.push(queue.pop());
break;
case 'size':
sol.push(queue.size());
break;
case 'empty':
sol.push(queue.isEmpty());
break;
case 'front':
sol.push(queue.front());
break;
case 'back':
sol.push(queue.back());
break;
default:
throw new Error('Undefined Command');
}
});
// output
console.log(sol.join('\n'));