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. 【2】按照Django官网,创建一个web app 创建app/创建相应的数据库表

    1. Creating app $ python manage.py startapp polls That'll create a directory polls, which is laid ou ...

  2. (linux)container_of()宏

      在学习Linux驱动的过程中,遇到一个宏叫做container_of. 该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /**  * container_of - ...

  3. ES6 中的let 声明变量

    1.let是声明的是块级变量,不会污染全局,一般条件与循环中会用到: 2.let  不可以变量提升: 3.let不遵循作用域,一个作用域内如果有该变量,就不会到全局去找,也不可以在一个作用域重复声明一 ...

  4. 一步一步学Silverlight 2系列(21):如何在Silverlight中调用JavaScript

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. Android零碎知识点,之后会一直更新的哦!

    view的getCompoundDrawables()方法,调用这个方法返回的是控件的左上右下四个位置的Drawable,并且返回的类型是数据 setBounds(x,y,width,height); ...

  6. [Selenium] Android HTML5 中 Web Storage

    在 HTML5 中,Web Storage 这个新特性可让用户将数据存储在本地的浏览器中.在早期的浏览器中可通过 cookies 来完成这个任务,但 Web Storage 会更加安全和高效,且 We ...

  7. Crontab Build_setting的定期检查

    一.脚本功能 (1)检查所有的builting_setting.h是否能够编译通过,并将编译结果写入 编译结果.h文件中. (2)将编译结果通过邮箱发送给相关负责人. (3)系统定期执行任务,检查bu ...

  8. django上课笔记3-ORM补充-CSRF (跨站请求伪造)

    一.ORM补充 ORM操作三大难点: 正向操作反向操作连表 其它基本操作(包含F Q extra) 性能相关的操作 class UserInfo(models.Model): uid = models ...

  9. 023--python os、sys、json、pickle、xml模块

    一.os模块 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 >>> os.getcwd() 'C:\\Python36' os.chdir(&quo ...

  10. POJ3186【区间DP】

    题意: 每次只能取两端,然后第 i 次取要val[ i ]*i,求一个最大值 一切都是错觉[读者省略此段] 这道题目一开始想的就是记忆化搜索,然后太天真了?好像是,一开始用一维dp[ i ]直接代表一 ...