ホームページ  >  記事  >  ウェブフロントエンド  >  Javascriptを使用したキューの実装(リンクリスト)

Javascriptを使用したキューの実装(リンクリスト)

WBOY
WBOYオリジナル
2024-08-14 19:10:19282ブラウズ

Queue Implementation Using Javascript (Linked List)

導入

自信がない場合、またはリンク リストとその種類、およびそのリストに対する操作の実行方法について詳しく理解したい場合は、シングル リンク リストとダブル リンク リストに関連する私の他の記事を参照してください

すべての操作で Javascript を使用した単一および二重リンク リストへのアプローチ: - 最終停止ソリューション

  1. この記事は、単一リンクリストの使用とキューデータ構造の作成についてです。

ご不明な点がございましたら、お気軽にご連絡ください

コードをお楽しみください。ハッピーコーディング。

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

class LinkedList {
    constructor() {
        this.head = this.tail = null;
        this.size = 0;
    }

    append(value){
        const newNode = new Node(value);

        if (this.head === null) {
            this.head = this.tail = newNode;
            this.size = 1;
            return ;
        }

        let currentNode = this.head;
        const pos = this.size-1;
        let counter = 0;

        while (currentNode) {
            if (counter++ === pos) {
                currentNode.next = newNode;
                this.tail = newNode;
                this.size++;
                break;
            }
            currentNode = currentNode.next;
        }
    }

    deleteFromHead() {
        if (this.head === null) {
            return ;
        }
        let currentNode = this.head;
        this.head = currentNode.next;
        this.size--;
        return this.head.value;
    }

    traversal() {
        let currentNode = this.head;

        while (currentNode) {
            console.log(currentNode.value);
            currentNode = currentNode.next;
        }
    }

    reverse(){
        let currentNode = this.head;
        let nextNode = null;
        let prevNode = null;

        while(currentNode) {
            nextNode = currentNode.next;
            currentNode.next = prevNode;
            if (prevNode === null) {
                this.tail = prevNode;
            }
            prevNode = currentNode;
            currentNode = nextNode;
        }

        this.head = prevNode;
        this.traversal();
    }


}


class Queue{
    queue;
    constructor() {
        this.queue = new LinkedList();
    }

    push(value) {
        this.queue.append(value)
    }

    pop() {
        this.queue.deleteFromHead();
    }

    peak() {
        console.log(this.queue.head.value)
    }

    traversal() {
        this.queue.traversal()
    }
    reverse() {
        this.queue.reverse();
    }
}


const q = new Queue();

q.push(20);
q.push(13);
q.push(3);
q.push(5);
q.push(9);

q.pop()
console.log(q);
q.traversal()
console.log('---------------Peak--------------')
q.peak()
console.log('-------------After Reverse ---------------');
q.reverse()

/*
Queue {
  queue: LinkedList {
    tail: Node { value: 9, next: null },
    head: Node { value: 13, next: [Node] },
    size: 4
  }
}
13
3
5
9
---------------Peak--------------
13
-------------After Reverse ---------------
9
5
3
13
*/

以上がJavascriptを使用したキューの実装(リンクリスト)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:レディス2次の記事:レディス2