>  기사  >  웹 프론트엔드  >  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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:레디스 2다음 기사:레디스 2