Muscardinus

Queue 구현 본문

DataStructure

Queue 구현

Muscardinus 2020. 12. 12. 22:30
728x90

Linked List Method

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

class Queue {
  constructor() {
    this.first = null;
    this.last = null;
    this.length = 0;
  }
  
  peek() {
    return this.fist;
  }
  
  enqueue(value) {
    const newNode = new Node(value);
    if (!this.length) {
      this.first = this.last = newNode;
    } else {
      this.last.next = newNode;
      this.last = newNode;
    }
    this.length++;
    return this;
  }
  
  dequeue() {
    if (!this.length) return null;
    if (this.length === 1) {
      this.first = this.last = null;
      this.length--;
    }
    this.first = this.first.next;
    this.length--;
    return this;
  }
}

 

 

728x90

'DataStructure' 카테고리의 다른 글

Undirected Graph 구현  (0) 2020.12.13
Binary Search Tree 구현  (0) 2020.12.13
Stack 구현  (0) 2020.12.12
Double Linked List 구현  (0) 2020.10.15
Linked List 구현  (0) 2020.10.15
Comments