04队列

实现基本队列

class Queue {
constructor () {
this.items = []
}
enqueue(item) {
return this.items.push(item)
}
dequeque() {
return this.items.shift()
}
font() {
return this.items[0]
}
isEmpty() {
return this.items.length === 0
}
clear() {
return this.items = []
}
size() {
return this.items.length
}
}

实现具有优先级的队列


class QueueElement {
constructor(element, priority) {
this.element = element
this.priority = priority
}
} class priorityQueue extends Queue {
enqueue(element, priority) {
var item = new QueueElement(element, priority)
if (this.isEmpty()) { // 队列为空, 直接入队
this.items.push(item)
} else {
var added = false // 是否添加过的标志位
this.items.forEach((val, index, arr) => {
// 此处决定最小优先队列, 还是最大优先队列
// 如果我们添加的元素的优先级刚刚好大于遍历到的那个元素
// 就插入到这个元素的位置
// 也能保证在相同优先级下面, 的队尾
if (item.priority > val.priority && !added) {
arr.splice(index, 0, item)
added = true
}
})
// 如果优先级小于全部的元素, 就放到队尾
!added && this.items.push(item)
}
}
} var priQue = new priorityQueue
priQue.enqueue('a', 1)
priQue.enqueue('a', 1)
priQue.enqueue('b', 5)
priQue.enqueue('c', 3)
priQue.enqueue('d', 7)
debugger;

循环队列 模仿击鼓传花

function hotPotato(nameList) {
var queue = new Queue
nameList.forEach((val) => {
queue.enqueue(val)
})
while(queue.size() > 1) {
var num = Math.floor(Math.random() * 10 + 1)
debugger;
for(let i = 0; i < num; i++) {
queue.enqueue(queue.dequeque())
}
var outer = queue.dequeque()
console.log(`${outer}被淘汰了`)
}
var winner = queue.dequeque()
console.log(`${winner}胜利`)
return winner
} var nameList = ['a', 'b', 'c', 'd', 'e']
hotPotato(nameList)

05链表

  • 数组很容易根据指针找到固定元素
  • 链表寻找某个元素, 只能从头遍历

单项链表

class Node {
constructor(element) {
this.element = element
this.next = null
}
} class LinkDist {
constructor() {
this.length = 0
this.head = null
}
append(element) {
return append.call(this, element)
}
search(postion, cb) {
return search.call(this, postion, cb)
}
insert(position, element) {
return insert.call(this, position, element)
}
removeAt(postion) {
return removeAt.call(this, postion)
}
indexOf(element, cb) {
var cur = this.head, pre = null
while(cur.next) {
if (cur.element === element) {
cb.call(this, pre, cur)
}
pre = cur
cur = cur.next
}
return null
}
remove(element) {
this.indexOf(element, (pre, cur) => {
if (pre && cur) {
pre.next = cur.next
this.length--
} else if (cur) {
this.head = cur.next
this.length--
}
})
}
isEmpty() {
return this.length === 0
}
size() {
return this.length
}
toString() {
var res = '', cur = this.head, index = 0
while (index++ < this.length) {
res += cur.element
cur = cur.next
}
return res
}
getHead() {
return this.head
}
} function append(element) {
var node = new Node(element)
var cur = null
if (this.head === null) {
this.head = node
} else {
cur = this.head // 先指向当前的第一个元素
while(cur.next) { // 只要有next就往下迭代
cur = cur.next
}
cur.next = node // 没有next的时候, 保存下next指向node
}
this.length++
}
function search(position, cb) {
if (position > -1 && position < this.length) {
var cur = this.head, pre = null, index = 0
if (position === 0) {
cb.call(this, null, cur)
} else {
while (index++ < position) {
pre = cur
cur = cur.next
}
cb.call(this, pre, cur)
}
return cur
} else {
cb.call(this, null, null)
return null
}
}
function removeAt(position) {
return this.search(position, (pre, cur) => {
if (pre) {
pre.next = cur.next
this.length--
} else if (cur) {
this.head = cur.next
this.length--
} else {
throw new Error('未找到元素')
return false
}
})
}
function insert(position, element) {
this.search(position, (pre, cur) => {
if (pre && cur) {
// 除第一项以外的
pre.next = new Node(element)
pre.next.next = cur
} else if (cur) {
// 第一项
this.head = new Node(element)
this.head.next = cur
} else {
throw new Error('元素并不存在')
}
this.length++
})
} var list = new LinkDist
list.append(15)
list.append(10)
list.append(5)
list.insert(1, 9)
list.remove(10)
var res = list.toString()

单项链表反转

学习JavaScript数据结构与算法 (二)的更多相关文章

  1. 重读《学习JavaScript数据结构与算法-第三版》- 第3章 数组(一)

    定场诗 大将生来胆气豪,腰横秋水雁翎刀. 风吹鼍鼓山河动,电闪旌旗日月高. 天上麒麟原有种,穴中蝼蚁岂能逃. 太平待诏归来日,朕与先生解战袍. 此处应该有掌声... 前言 读<学习JavaScr ...

  2. 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)

    定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...

  3. 学习JavaScript数据结构与算法 (一)

    学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...

  4. 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述

    定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...

  5. 重读《学习JavaScript数据结构与算法-第三版》- 第4章 栈

    定场诗 金山竹影几千秋,云索高飞水自流: 万里长江飘玉带,一轮银月滚金球. 远自湖北三千里,近到江南十六州: 美景一时观不透,天缘有分画中游. 前言 本章是重读<学习JavaScript数据结构 ...

  6. 重读《学习JavaScript数据结构与算法-第三版》- 第5章 队列

    定场诗 马瘦毛长蹄子肥,儿子偷爹不算贼,瞎大爷娶个瞎大奶奶,老两口过了多半辈,谁也没看见谁! 前言 本章为重读<学习JavaScript数据结构与算法-第三版>的系列文章,主要讲述队列数据 ...

  7. 学习JavaScript数据结构与算法---前端进阶系列

    学习建议 1.视频学习---认知 建议:在中国慕课上找"数据结构"相关的视频教程.中国大学MOOC 推荐清华大学.北京大学.浙江大学的教程,可先试看,然后根据自身的情况选择视频进行 ...

  8. 学习Javascript数据结构与算法(第2版)笔记(1)

    第 1 章 JavaScript简介 使用 Node.js 搭建 Web 服务器 npm install http-server -g http-server JavaScript 的类型有数字.字符 ...

  9. 学习JavaScript数据结构与算法 2/15

    第一章 JavaScript简介 js不同于C/C++,C#,JAVA,不是强类型语言. 通常,代码质量可以用全局变量和函数的数量来考量(数量越多越糟).因此,尽可能避免使用全局变量. JS数据类型 ...

随机推荐

  1. [攻防实战]CTF大赛准备(手动注入sql)

    一.IIS write漏洞利用 先用工具扫描,再上传小马,使用菜刀连接即可. 思考点: 如何获知是一台IIS站点? 本例中上传的一句话木马是什么意思? <%eval request(" ...

  2. 超低功耗、无需网关,CSR智能家居蓝牙控制照明方案

    本文转载至 http://blog.csdn.net/justinjing0612/article/details/39250997 [导读] iOS 8 Beta2终于让智能家居HomeKit功能露 ...

  3. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

  4. jvm 调优(1)概念

    数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...

  5. 超全!整理常用的iOS第三方资源(转)

    超全!整理常用的iOS第三方资源 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地 ...

  6. 关于苹果iPhone手机对页面margin属性无效的解决方法一(如有错误,请留言批评)

    这个问题,是在给商城网站底部footer设置margin属性的时候发现的,先把出现问题的截图发出来看一下 ​安卓手机,打开正常 ​iphone6 p 打开出现的问题(无视margin-bottom:6 ...

  7. Silverlight中使用MVVM(4)

    Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 Silverlight中使用MVVM(4)—演练 ...

  8. xcode添加背景音乐/音效

    xcode添加音效:http://www.cnblogs.com/jiayongqiang/p/5625886.html 背景音乐: ios播放音乐时会用到一个叫做AVAudioPlayer的类,这个 ...

  9. POJ2689:Prime Distance(大数区间素数筛)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  10. 用js实现的一个可拖动标签的例子

    先贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...