EX)
//스택을 만드는 클래스
class Makestack {
constructor() {
this.storage = {};
this.top = 0; //스택을 분별하기 위한 키값
}
push(element) {
this.storage[this.top] = element;
this.top++; //스택이 추가되면 카운트가 하나씩 없어짐
}
pop() {
if (Object.keys(this.storage).length === 0) {
return 'none stack'
}
const result = this.storage[Object.keys(this.storage).length-1];
delete this.storage[Object.keys(this.storage).length-1];
this.top--;//스택이 삭제되면 카운트도 -1되어 다음 스택의 카운트에 영향을 주지 않아야함
return result;
}
}
EX)
//큐을 만드는 클래스
class MakeQueue {
constructor(){
this.storage =[]
}
inputdata(element){
this.storage.push(element)//맨끝에 데이터가 추가된다.
}
outputdata(){
this.storage.shift//가장 앞의 데이터가 빠져나간다.
}
}