データ構造:キュー
6767 ワード
イントロ
と、我々は
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;
}
}
}
更なる読書📖
質問❔
peek
, これは開始ノードを返します.次部分➡️
我々は今まで構築したデータ構造を比較する.
興味深いものをお見逃しなく.subscribe !
Reference
この問題について(データ構造:キュー), 我々は、より多くの情報をここで見つけました https://dev.to/miku86/javascript-data-structures-queue-recap-2fbcテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol