큐 구현
// 큐
const q = [];
const push = (item) => q.push(item);
const pop = () => q.pop();
q.push(1);
console.log(q); // [ 1 ]
q.push(2);
console.log(q); // [ 1, 2 ]
q.push(3);
console.log(q); // [ 1, 2, 3 ]
q.pop();
q.pop();
q.push(4);
console.log(q); // [ 1, 4 ]
스택 구현
// 스택
const stack = [];
const push = (item) => stack.push(item);
const shift = () => stack.shift();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack); // [ 1, 2, 3 ]
stack.shift();
stack.shift();
console.log(stack); // [ 3 ]
stack.push(4);
stack.push(5);
stack.shift();
stack.shift();
console.log(stack); // [ 5 ]
'Web development > Algorithm' 카테고리의 다른 글
[LeetCode] 189. Rotate Array (Javascript) (0) | 2020.07.20 |
---|---|
[HackerRank 30 Days of Code] Day 5 ~ Day 7 (0) | 2020.07.13 |
베스트앨범 (0) | 2020.07.08 |
[HackerRank 30 Days of Code] Day 3, Day 4 (0) | 2020.07.07 |
[HackerRank 30 Days of Code] Day 0 ~ Day 2 (0) | 2020.07.07 |
댓글