We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
队列(Queue)是一种运算受限的线性表,特点:先进先出。(FIFO:First In First Out)
受限之处:
enqueue(element)
dequeue()
front()
isEmpty()
size()
toString()
class Queue { constructor() { this.items = []; } enqueue(item) { this.items.push(item) } dequeue() { return this.items.shift() } front() { return this.items[0] } isEmpty() { return this.item.length === 0 } // size() 查看队列中元素的个数 size() { return this.items.length } // toString() 将队列中的元素以字符串形式返回 toString() { let result = '' for (let item of this.items) { result += item + ' ' } return result } }
击鼓传花
分析:传入一组数据集合和设定的数字 number,循环遍历数组内元素,遍历到的元素为指定数字 number 时将该元素删除,直至数组剩下一个元素。
function queueGame(nameList, number) { const queue = new Queue(); for (const name of nameList) { queue.enqueue(name) } while (queue.size() > 1) { for (let i = 0; i < number - 1; i++) { queue.enqueue(queue.dequeue()); } queue.dequeue(); } const endName = queue.front() return nameList.indexOf(endName) }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
队列(Queue)是一种运算受限的线性表,特点:先进先出。(FIFO:First In First Out)
受限之处:
队列在程序中的应用
队列常见的操作
enqueue(element)
向队列尾部添加一个(或多个)新的项。dequeue()
移除队列的第一(即排在队列最前面的)项,并返回被移除的元素。front()
返回队列中的第一个元素——最先被添加,也将是最先被移除的元素。队列不做任何变动(不移除元素,只返回元素信息与 Map 类的 peek 方法非常类似)。isEmpty()
如果队列中不包含任何元素,返回 true,否则返回 false。size()
返回队列包含的元素个数,与数组的 length 属性类似。toString()
将队列中的内容,转成字符串形式。代码实现
击鼓传花
分析:传入一组数据集合和设定的数字 number,循环遍历数组内元素,遍历到的元素为指定数字 number 时将该元素删除,直至数组剩下一个元素。
The text was updated successfully, but these errors were encountered: