データ構造:キュー



イントロ
と、我々はdequeue メソッド.
私はあなたがキューの概念について何かを学んで、自分でそれを実装するために最善を尽くしたことを願っています.

キューについての考え💭
を用いてキューを実装した.
キューデータ構造は非常に重要な概念です.なぜなら、私たちは常にそれを使用するからです.
キューは"First In, First Out"-Principle , キューに入る最初のノードを意味するのは、後でキューから出る最初のノードである.
実際の生活の中での例:お店でお支払いしたい人、プリンタの仕事.
  • アクセスO(N)
  • 検索O(N)
  • インサートO(1)
  • 削除:O(1)

  • 最終実施📝
    キューには以下のメソッドがあります:
  • enqueue , キューの最後にノードを追加するには
  • dequeue , キューの先頭からノードを削除するには
  • class Node {
      constructor(value) {
        this.value = value;
        this.next = null;
      }
    }
    
    class Queue {
      constructor() {
        this.length = 0;
        this.start = null;
        this.end = null;
      }
    
      enqueue(value) {
        const newNode = new Node(value);
    
        if (!this.length) {
          this.start = newNode;
          this.end = newNode;
        } else {
          this.end.next = newNode;
          this.end = newNode;
        }
    
        this.length += 1;
        return newNode;
      }
    
      dequeue() {
        if (!this.length) {
          return null;
        } else {
          const nodeToRemove = this.start;
          this.start = this.start.next;
          nodeToRemove.next = null;
    
          if (this.length === 1) {
            this.end = null;
          }
    
          this.length -= 1;
          return nodeToRemove;
        }
      }
    }
    

    更なる読書📖
  • Wikipedia: Queue
  • Wikibooks: Queue
  • Wikipedia: FIFO

  • 質問❔
  • 新しい方法を実装できますかpeek , これは開始ノードを返します.

  • 次部分➡️
    我々は今まで構築したデータ構造を比較する.
    興味深いものをお見逃しなく.subscribe !