출처: 마음의 고향 위키피디아
스택은 한 쪽 끝에서만 자료를 넣거나 빼는 선형적 자료구조. LIFO(Last In First Out), 전문 용어로는 선입선출
수도 코드
스택 생성{
비어있는 top;
비어있는 size;
}
스택.push(data){
data를 top에 입력;
top++;
}
스택.pop(){
top에 있는 데이터 return;
데이터 삭제 후;
top--;
}
const Stack = function(){
this.top = null;
this.size = 0;
}
const Node = function(data){
this.data = data;
this.pre = null
}
Stack.prototype.push = function(data){
let node = new Node(data)
node.pre = this.top
this.top = node
this.size += 1;
return this.top;
}
Stack.prototype.pop = function(){
temp = this.top;
this.top = this.top.pre
this.size -=1
return temp
}
const stack = new Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.pop()
console.log(stack)
class Stack{
constructor(){
this.top = -1;
this.dataStore = [];
}
push(element){
this.top++;
this.dataStore[this.top] = element;
}
pop(){
if(this.top>-1){
let val = this.dataStore[this.top];
this.top--;
return val;
}
return null;
}
peek(){
return this.dataStore[this.top];
}
get length(){
return this.top+1;
}
clear(){
this.top = 0;
this.dataStore = [];
}
}
참고 :
https://hokeydokey.tistory.com/30?category=783107
https://helloworldjavascript.net/pages/282-data-structures.html
https://velog.io/@ryu/JavaScript-%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-Stack-%EC%8A%A4%ED%83%9D