티스토리 뷰

class Node{
  constructor(item){
    this.item = item;
    this.next = null;
  }
}

class Stack{
  constructor(){
    this.topOfStack = null;
    this.length = 0;
  }

  push(item){
    const node = new Node(item);
    if(this.topOfStack!=null){
      node.next = this.topOfStack; 
    }
      this.topOfStack = node;
      this.length+=1;
  }

  pop(){
    if(this.length==0)return -1;
    const popItem = this.topOfStack;
    this.topOfStack = popItem.next;
    this.length-=1;

    return popItem.item
  }

  size(){
    return this.length;
  }

  empty(){
    if(this.length==0) return 1;
    else return 0;
  }

  top(){
    if(this.length==0)return -1;
    return this.topOfStack.item; 
  }

}
728x90

'자료구조 알고리즘 > JavaScript 자료구조 알고리즘' 카테고리의 다른 글

JavaScript) 최소 힙  (0) 2021.09.16
JavaScript) 최대 힙  (0) 2021.09.16
JavaScript) 덱 Deque  (0) 2021.09.13
JavaScript) 큐 Queue  (0) 2021.09.13
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함