javascript - 자료구조 (계속 작성)

김동하·2020년 10월 21일
0

stack

출처: 마음의 고향 위키피디아

스택은 한 쪽 끝에서만 자료를 넣거나 빼는 선형적 자료구조. 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

https://jeong-pro.tistory.com/m/124

https://medium.com/@songjaeyoung92/%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-javascript-stack-%EC%9D%B4%EB%9E%80-31f9bbb84897

https://jeong-pro.tistory.com/m/124

profile
프론트엔드 개발

0개의 댓글