-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingTwoStacks.js
More file actions
83 lines (74 loc) · 1.44 KB
/
QueueUsingTwoStacks.js
File metadata and controls
83 lines (74 loc) · 1.44 KB
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
class Stack {
constructor() {
this.stack = [];
}
push(data) {
this.stack.push(data);
}
pop() {
return this.stack.pop();
}
}
class Queue {
constructor() {
this.stack1 = new Stack();
this.stack2 = new Stack();
}
enqueue(data) {
this.stack1.push(data);
}
dequeue() {
if (this.stack2.stack.length <= 0) {
while (this.stack1.stack.length > 0) {
this.stack2.push(this.stack1.pop());
}
}
this.stack2.pop();
}
getFront() {
return this.stack2.stack.length > 0 ?
this.stack2.stack[this.stack2.stack.length - 1] :
this.stack1.stack[0];
}
}
function processData(input) {
const queue = new Queue();
//개행 기준으로 나눠서 query 구분
const queries = input.split("\n");
//맨 앞을 쿼리 갯수로 받아옴.
queries.shift();
//queries를 순회하면서 각 쿼리를 파싱함
queries.forEach(query => {
//파싱 -> 쿼리[0]이 1인지, 2인지, 3인지에 따라 실행할 함수 만들기
const queryArr = query.split(" ");
switch (queryArr[0]) {
case "1":
queue.enqueue(queryArr[1]);
break;
case "2":
queue.dequeue();
break;
case "3":
console.log(queue.getFront());
break;
default:
break;
}
//1일 때 enqueue
//2일 때 dequeue
//3일 때 front print
})
}
let input;
input = `10
1 42
2
1 14
3
1 28
3
1 60
1 78
2
2`;
processData(input);