4.9 KiB
4.9 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d8255367417b2b2512c75 | Create a Circular Queue | 1 | 创建循环队列 |
Description
[1,2,3,4,5]这里的读写都在
^读@ 0
^写@ 0
0
。现在队列获得3个新记录a
, b
和c
。我们的队列现在看起来像: [a,b,c,4,5]当读头读取时,它可以删除值或保留它们:
^读@ 0
^写@ 3
[null,null,null,4,5]一旦写入到达数组的末尾,它就会循环回到开头:
^阅读@ 3
^写@ 3
[f,null,null,d,e]这种方法需要恒定的内存量,但允许处理更大尺寸的文件。说明:在此挑战中,我们将实现循环队列。循环队列应提供
^阅读@ 3
^写@ 1
enqueue
和dequeue
方法,允许您读取和写入队列。类本身也应该接受一个整数,您可以使用该整数在创建队列时指定队列的大小。我们已经在代码编辑器中为您编写了此类的起始版本。将项目排入队列时,写入指针应向前推进,并在到达队列末尾时循环回到开头。同样,当您使项目出列时,读指针应向前推进。不应允许写指针移过读指针(我们的类不会让你覆盖你还没有读过的数据),并且读指针不能超过你写的数据。此外,如果成功,则enqueue
方法应返回您入enqueue
的项,否则返回null
。类似地,当你使一个项目出列时,它应该被返回,如果你不能出列,你应该返回null
。 Instructions
Tests
tests:
- text: <code>enqueue</code>方法将项添加到循环队列。
testString: 'assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })(), "The <code>enqueue</code> method adds items to the circular queue.");'
- text: 您不能通过读指针将项排入队列。
testString: 'assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); test.enqueue(13); test.enqueue(25); test.enqueue(59); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })(), "You cannot enqueue items past the read pointer.");'
- text: <code>dequeue</code>方法使队列中的项目出列。
testString: 'assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591; })(), "The <code>dequeue</code> method dequeues items from the queue.");'
- text: 项目出列后,其队列中的位置应重置为<code>null</code> 。
testString: 'assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(672); test.dequeue(); test.dequeue(); var print = test.print(); return print[0] === null && print[1] === null && print[2] === 672; })(), "After an item is dequeued its position in the queue should be reset to <code>null</code>.");'
- text: 尝试通过写指针出列队列返回<code>null</code>并且不会使写指针前进。
testString: 'assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591 && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.enqueue(100) === 100 && test.dequeue() === 100; })(), "Trying to dequeue past the write pointer returns <code>null</code> and does not advance the write pointer.");'
Challenge Seed
class CircularQueue {
constructor(size) {
this.queue = [];
this.read = 0;
this.write = 0;
this.max = size - 1;
while (size > 0) {
this.queue.push(null);
size--;
}
}
print() {
return this.queue;
}
enqueue(item) {
// Only change code below this line
// Only change code above this line
}
dequeue() {
// Only change code below this line
// Only change code above this line
}
}
Solution
// solution required