💡Stack
📌Stack 개념
- Stack 은 쌓다, 쌓이다, 포개지다 와 같은 의미를 가진다.
- 접시를 쌓고 치우는 형태의 비슷한 자료구조
- LIFO(Last-In-First-Out): 가장 마지막에 들어온 데이터가 가장 먼저 삭제됨
- FILO(First-In-Last-Out): 가장 처음에 들어온 데이터가 가장 마지막에 삭제됨
- 데이터간 순서 관계를 유지할 수 있다.
- 맨 뒤 데이터 추가
- 맨 뒤 데이터 삭제
- 맨 뒤 데이터 접근
📌Stack 구현
class Stack {
constructor() {
this.storage = {};
this.top = 0;
}
size() {
return this.top;
}
push(element) {
this.storage[this.top] = element;
this.top += 1;
}
pop() {
if(this.size() <= 0) {
return;
}
const result = this.storage[this.top - 1];
delete this.storage[this.top - 1];
this.top -= 1;
return result;
}
}
💡Queue
📌Queue 개념
- Queue 는 줄을 서서 기다리다, 대기 행렬이라는 의미를 가지고 있다.
- 자료구조 Stack 과는 반대되는 개념이다.
- 마트 계산대 줄 혹은 톨게이트 통과 형태와 비슷한 자료구조이다.
- FIFO(First-In-First-Out): 가장 처음에 들어온 데이터가 가장 처음에 삭제됨
- LILO(Last-In-Last-Out): 가장 마지막에 들어온 데이터가 가장 마지막에 삭제됨
- 데이터간 순서 관계를 유지할 수 있다.
- 맨 뒤 데이터 추가
- 맨 앞 데이터 삭제
- 맨 앞 데이터 접근
📌Queue 구현
class Queue {
constructor() {
this.storage = {};
this.front = 0;
this.rear = 0;
}
size() {
return this.rear - this.front;
}
enqueue(element) {
this.storage[this.rear] = element;
this.rear += 1;
}
dequeue() {
if (this.size() === 0) {
return;
}
const result = this.storage[this.front];
delete this.storage[this.front];
this.front += 1;
return result;
}
}