freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/data-structures/create-a-priority-queue-cla...

5.1 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d8255367417b2b2512c74 Create a Priority Queue Class 1 创建优先级队列类

Description

在此挑战中,您将创建一个优先级队列。优先级队列是一种特殊类型的队列,其中项目可能具有指定其优先级的附加信息。这可以简单地用整数表示。项目优先级将覆盖确定序列项目已出列的放置顺序。如果具有较高优先级的项目在具有较低优先级的项目之后排队,则较高优先级项目将在所有其他项目之前出列。例如,让我们假设我们有一个包含三个项目的优先级队列: [['kitten', 2], ['dog', 2], ['rabbit', 2]]这里第二个值(整数)表示项目优先级。如果我们将优先级为1 ['human', 1]排入队列(假设优先级较低,则优先级较低),那么它将成为第一个出列的项目。该集合将是这样的: [['human', 1], ['kitten', 2], ['dog', 2], ['rabbit', 2]] 。我们已经开始在代码编辑器中编写PriorityQueue 。您需要添加一个enqueue方法来添加具有优先级的项目,一个用于删除项目的dequeue方法,一个用于返回队列中项目数量的size方法,一个用于返回队列front元素的front方法,以及最后一个isEmpty方法,如果队列为空则返回true ,否则返回false 。入enqueue应接受上面显示格式的项目( ['human', 1] ),其中1表示优先级。 dequeue应该只返回当前项目,而不是其优先级。

Instructions

Tests

tests:
  - text: 您的<code>Queue</code>类应该有一个<code>enqueue</code>方法。
    testString: 'assert((function(){var test = new PriorityQueue();  return (typeof test.enqueue === "function")}()), "Your <code>Queue</code> class should have a <code>enqueue</code> method.");'
  - text: 您的<code>Queue</code>类应该有一个<code>dequeue</code>方法。
    testString: 'assert((function(){var test = new PriorityQueue();  return (typeof test.dequeue === "function")}()), "Your <code>Queue</code> class should have a <code>dequeue</code> method.");'
  - text: 您的<code>Queue</code>类应该有一个<code>size</code>方法。
    testString: 'assert((function(){var test = new PriorityQueue();  return (typeof test.size === "function")}()), "Your <code>Queue</code> class should have a <code>size</code> method.");'
  - text: 您的<code>Queue</code>类应该有一个<code>isEmpty</code>方法。
    testString: 'assert((function(){var test = new PriorityQueue();  return (typeof test.isEmpty === "function")}()), "Your <code>Queue</code> class should have an <code>isEmpty</code> method.");'
  - text: 当项目入队和出列时您的PriorityQueue应使用<code>size</code>方法正确跟踪当前项目数。
    testString: 'assert((function(){var test = new PriorityQueue(); test.enqueue(["David Brown", 2]); test.enqueue(["Jon Snow", 1]); var size1 = test.size(); test.dequeue(); var size2 = test.size(); test.enqueue(["A", 3]); test.enqueue(["B", 3]); test.enqueue(["C", 3]); return (size1 === 2 && size2 === 1 && test.size() === 4)}()), "Your PriorityQueue should correctly keep track of the current number of items using the <code>size</code> method as items are enqueued and dequeued.");'
  - text: 当队列为空时, <code>isEmpty</code>方法应该返回<code>true</code> 。
    testString: 'assert((function(){var test = new PriorityQueue(); test.enqueue(["A", 1]); test.enqueue(["B", 1]); test.dequeue(); var first = test.isEmpty(); test.dequeue(); return (!first && test.isEmpty()); }()), "The <code>isEmpty</code> method should return <code>true</code> when the queue is empty.");'
  - text: 优先级队列应该在具有较低优先级的项之前返回具有较高优先级的项,否则以先进先出顺序返回项。
    testString: 'assert((function(){var test = new PriorityQueue(); test.enqueue(["A", 5]); test.enqueue(["B", 5]); test.enqueue(["C", 5]); test.enqueue(["D", 3]); test.enqueue(["E", 1]); test.enqueue(["F", 7]); var result = []; result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); return result.join("") === "EDABCF";}()), "The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.");'

Challenge Seed

function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line

    // Only change code above this line
}

Solution

// solution required