티스토리 뷰

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

class Deque{
  constructor(){
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  push_front(item){
    const node = new Node(item);
   if(this.size()==0){
     this.head = node;
     this.tail = node;
   }else{
    this.head.prev = node;
    node.next=this.head;
    this.head = node;
   }
    this.length+=1; 
  }


  push_back(item){
    const node = new Node(item)
    if(this.size()==0){
      this.head = node;
      this.tail = node;
    }else{
      this.tail.next = node;
      node.prev = this.tail;
      this.tail = node; 
    }
    this.length +=1;
  }

  pop_front(){
    if(this.size()==0) return -1;
    const popItem = this.head;
    this.head = this.head.next;
    if(this.size()==1){
        this.head = null;
    }else{
        this.head.prev = null;
    }
    this.length -=1;
    return popItem.item;
  }

  pop_back(){
    if(this.size()==0) return -1;
    const popItem = this.tail;
    this.tail = this.tail.prev;
    if(this.size()==1){
        this.tail = null;
    }else{
        this.tail.next = null;
    }
    this.length -=1;
    return popItem.item;
  }


  size(){
    return this.length;
  }

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

  front(){
    if(this.empty()==1) return -1;
    return this.head.item; 
  }

  back(){
    if(this.empty()==1) return -1;
    return this.tail.item; 
  }
}
728x90

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

JavaScript) 최소 힙  (0) 2021.09.16
JavaScript) 최대 힙  (0) 2021.09.16
JavaScript) 큐 Queue  (0) 2021.09.13
JavaScript) 스택 Stack  (0) 2021.09.13
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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 29 30 31
글 보관함