队列
一、概念

队列(Queue)
受限的线性结构,==先进先出==(FIFO First In First Out)
只允许在表的==前端进行删除==操作,在表的==后端进行插入==操作
队列结构的实现
- 基于数组实现
- 基于链表实现(链表:也是一种数据结构,JavaScript中没有自带的链表结构)
二、属性和方法
队列的方法(6个):
- enqueue(): 向队列尾部添加一个或多个新的项
- dequeue(): 移出队列的第一项,并返回被移出的元素
- front(): 返回队列中第一个元素
- isEmpty(): 如果队列中不包含任何元素,返回true,否则返回false
- size(): 返回队列包含的元素个数
- toString(): 将队列中的内容,转换成字符串形式
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function Queue() { this.items = [];
Queue.prototype.enqueue = function (el) { this.items.push(el); }
Queue.prototype.dequeue = function () { return this.items.shift(); }
Queue.prototype.front = function () { return this.items[0] }
Queue.prototype.isEmpty = function () { return this.items.length === 0 }
Queue.prototype.size = function () { return this.items.length; }
Queue.prototype.toString = function () { return this.items.toString(); } }
|
三、算法实例
击鼓传花游戏
原游戏规则:
- 班级中玩一个游戏,所有学生围成一圈,从某位同学手里开始向旁边的同学传一束花。
- 这个时候某个人(比如班长)在击鼓,鼓声停下的一颗,花落在谁手里,谁就出来表演节目。
修改游戏规则:
- 几个朋友一起玩—个游戏,围成一圈,开始数数,数到某个数字的人自动淘汰。
- 最后剩下的这个人会获得胜利,请问最后剩下的是原来在哪一个位置上的人?
封装一个基于队列的函数
- 参数: 所有参与人的姓名,基于的数字。
- 结果: 最终剩下的一人的姓名。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| function passGame(nameList, num) {
const queue = new Queue();
for (const item of nameList) { queue.enqueue(item) }
while(queue.size() > 1) {
for (let i = 0; i < num - 1; i++) { queue.enqueue(queue.dequeue()); }
queue.dequeue(); }
const lastName = queue.front(); console.log('剩下的人:', lastName)
return nameList.indexOf(lastName); }
const index = passGame(['红', '橙', '黄', '绿', '青', '蓝', '紫'], 3); console.log('剩下人的索引:', index)
|
-
Post title: 队列
-
Post author: Chao
-
Create time: 2019-08-22 14:45:00
-
Post link: 2019/08/22/队列/
-
Copyright notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
$tools-item-width = 2.2rem
$tools-item-font-size = 1.1rem
$tools-item-border-radius = 0.1rem
.side-tools-container {
position relative
.tools-item {
width $tools-item-width
height $tools-item-width
margin-bottom 0.2rem
color var(--text-color-3)
font-size $tools-item-font-size
background var(--background-color-1)
border-right none
border-radius $tools-item-border-radius
box-shadow 0.1rem 0.1rem 0.2rem var(--shadow-color)
cursor pointer
i {
color var(--text-color-3)
}
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
i {
color var(--background-color-1)
}
}
+keep-tablet() {
width $tools-item-width * 0.9
height $tools-item-width * 0.9
margin-bottom 0.2rem
font-size $tools-item-font-size * 0.9
}
&.rss {
a {
width 100%
height 100%
border-radius $tools-item-border-radius
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
}
}
}
}
.side-tools-list {
transform translateX(100%)
opacity 0
transition-t("transform, opacity", "0, 0", "0.2, 0.2", "linear, linear")
&.show {
transform translateX(0)
opacity 1
}
}
.exposed-tools-list {
if (hexo-config('style.scroll.percent') == true) {
.tool-scroll-to-top {
display none
&.show {
display flex
}
&:hover {
.percent {
display none
}
.arrow-up {
display flex
}
}
.arrow-up {
display none
}
.percent {
display flex
font-size 1rem
}
}
}
}
}
$icon-size = 1.2rem
$search-header-height = 3rem
.search-pop-overlay {
position fixed
top 0
left 0
z-index $z-index-8
display flex
width 100%
height 100%
background rgba(0, 0, 0, 0)
visibility hidden
transition-t("visibility, background", "0, 0", "0.3, 0.3", "ease, ease")
&.active {
background rgba(0, 0, 0, 0.35)
visibility visible
.search-popup {
transform scale(1)
}
}
.search-popup {
z-index $z-index-6
width 70%
height 80%
margin auto
background var(--background-color-1)
border-radius 0.4rem
transform scale(0)
transition-t("transform", "0", "0.3", "ease")
+keep-tablet() {
width 80%
}
+keep-mobile() {
width 90%
}
.search-header {
display flex
align-items center
height $search-header-height
padding 0 1rem
background var(--text-color-6)
border-top-left-radius 0.2rem
border-top-right-radius 0.2rem
.search-input-field-pre {
margin-right 0.2rem
color var(--text-color-3)
font-size 1.3rem
cursor pointer
}
.search-input-container {
flex-grow 1
padding 0.2rem
.search-input {
width 100%
color var(--text-color-3)
font-size 1.2rem
background transparent
border 0
outline 0
&::-webkit-search-cancel-button {
display none
}
&::-webkit-input-placeholder {
color var(--text-color-4)
font-size 1rem
}
}
}
.close-popup-btn {
color var(--text-color-3)
font-size $icon-size
cursor pointer
&:hover {
color var(--text-color-1)
}
}
}
#search-result {
position relative
display flex
box-sizing border-box
height 'calc(100% - %s)' % $search-header-height
padding 0.3rem 1.5rem
overflow auto
.search-result-list {
width 100%
height 100%
font-size 1rem
li {
box-sizing border-box
margin 0.8rem 0
padding 0.8rem 0
border-bottom 0.1rem dashed var(--border-color)
&:last-child {
border-bottom none
}
.search-result-title {
position relative
display flex
align-items center
margin-bottom 0.8rem
padding-left 1rem
font-weight bold
&::after {
position absolute
top 50%
left 0
width 0.4rem
height 0.4rem
background var(--text-color-3)
border-radius 50%
transform translateY(-50%)
content ''
}
}
.search-result {
margin 0
padding-left 1rem
line-height 2rem
word-wrap break-word
}
a {
&:hover {
color var(--text-color-3)
}
}
.search-keyword {
color var(--primary-color)
font-weight bold
border-bottom 0.1rem dashed var(--primary-color)
}
}
}
#no-result {
margin auto
color var(--text-color-4)
}
}
}
}