도영스 공간
스택과 큐 본문
반응형
let stack = [];
stack.push('1');
stack.push('2');
stack.push('3');
console.log(stack.pop()); // 3
console.log(stack.pop()); // 2
console.log(stack.pop()); // 1
Stack
1. stack 사이즈를 알 수 있는 function
=> return stack.length();
2. stack이 공백인지 알 수 있는 것
=> return stack.length() === 0;
3. stack의 제일 마지막 index에 있는
값을 가져오는 것.(pop이랑 다르게 그 값은
없어지지 않음)
=> return stack[stack.length()-1];
let queue = [];
queue.push('1');
queue.push('2');
queue.push('3');
[1, 2, 3, ''] <= queue[i] == queue[front], i is the frist index of an array
<= queue[queue.length-1] == queue[rear]
console.log(queue.shift());
// 1, [2, 3] <= queue[front] == 2, queue[rear] == 3
console.log(queue.shift());
// 2, [3] <= queue[front] == 3, queue[rear] == 3
console.log(queue.shift())
// 3, [] <= queue[front] == 'error'
1. queue의 size
=> return queue.rear - 1;
2. queue가 공백인지 알 수 있는 방법
=> return front === rear
3. queue의 배열이 full이다.
queue의 크기는 10으로 가정했다.
그렇다면... queue 내에 값의 개수가 10개로 꽉 채워졌는지
어떻게 알 수 있을까...? (front, rear를 사용해서, %)
=> (rear + 1) % 10 == front
4. queue의 맨 첫번째 값을 가져오는 것
=> return queue[front]
728x90
반응형
'TIL > 2022 TIL' 카테고리의 다른 글
리액트 모달창 세로 스크롤 막기 (1) | 2022.07.30 |
---|---|
xxx 타입스크립트(Element형식에 checked 속성이 없습니다. ) (1) | 2022.07.28 |
REST API(Representational State Transfer) (1) | 2022.07.21 |
Next.js환경에서 카카오 맵 api 불러오기 (0) | 2022.07.20 |
Recursion 재귀함수 (0) | 2022.07.12 |
Comments