学习JavaScript数据结构与算法 (二)
- 学习JavaScript数据结构与算法 的笔记
- 包含第四章队列, 第五章链表
本人所有文章首发在博客园: http://www.cnblogs.com/zhangrunhao/
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数据结构与算法 (二)的更多相关文章
- 重读《学习JavaScript数据结构与算法-第三版》- 第3章 数组(一)
定场诗 大将生来胆气豪,腰横秋水雁翎刀. 风吹鼍鼓山河动,电闪旌旗日月高. 天上麒麟原有种,穴中蝼蚁岂能逃. 太平待诏归来日,朕与先生解战袍. 此处应该有掌声... 前言 读<学习JavaScr ...
- 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)
定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...
- 学习JavaScript数据结构与算法 (一)
学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...
- 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述
定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...
- 重读《学习JavaScript数据结构与算法-第三版》- 第4章 栈
定场诗 金山竹影几千秋,云索高飞水自流: 万里长江飘玉带,一轮银月滚金球. 远自湖北三千里,近到江南十六州: 美景一时观不透,天缘有分画中游. 前言 本章是重读<学习JavaScript数据结构 ...
- 重读《学习JavaScript数据结构与算法-第三版》- 第5章 队列
定场诗 马瘦毛长蹄子肥,儿子偷爹不算贼,瞎大爷娶个瞎大奶奶,老两口过了多半辈,谁也没看见谁! 前言 本章为重读<学习JavaScript数据结构与算法-第三版>的系列文章,主要讲述队列数据 ...
- 学习JavaScript数据结构与算法---前端进阶系列
学习建议 1.视频学习---认知 建议:在中国慕课上找"数据结构"相关的视频教程.中国大学MOOC 推荐清华大学.北京大学.浙江大学的教程,可先试看,然后根据自身的情况选择视频进行 ...
- 学习Javascript数据结构与算法(第2版)笔记(1)
第 1 章 JavaScript简介 使用 Node.js 搭建 Web 服务器 npm install http-server -g http-server JavaScript 的类型有数字.字符 ...
- 学习JavaScript数据结构与算法 2/15
第一章 JavaScript简介 js不同于C/C++,C#,JAVA,不是强类型语言. 通常,代码质量可以用全局变量和函数的数量来考量(数量越多越糟).因此,尽可能避免使用全局变量. JS数据类型 ...
随机推荐
- (转载)display:inline、block、inline-block的区别
display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...
- linux以及git和maven常用命令
maven常用命令: clean install -Dmaven.test.skip -Ptest-lx (注意:test-lx是pom文件名) 其他 https://www.cnblogs.co ...
- base64 hash sha
/*! * Crypto-JS v1.1.0 * http://code.google.com/p/crypto-js/ * Copyright (c) 2009, Jeff Mott. All ri ...
- Yii2 behaviors中verbs access的一些理解
public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'verbs' => [ 'clas ...
- elastica安装
https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html
- ⭐驱动之module_init/module_exit与系统启动关系
在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...
- linux驱动开发之九鼎板载蜂鸣器驱动测试【转】
本文转载自:http://whylinux.blog.51cto.com/10900429/1932491 字符设备驱动用的fileopretion结构体. 1.板载蜂鸣器的驱动测试 我手里有一个BS ...
- YTU 2457: 很简单的一道题
2457: 很简单的一道题 时间限制: 1 Sec 内存限制: 128 MB 提交: 261 解决: 80 [提交][状态][讨论版] 题目描述 有一个简单的函数数学公式,如下 输入 重复输入多组 ...
- wukong引擎源码分析之索引——part 2 持久化 直接set(key,docID数组)在kv存储里
前面说过,接收indexerRequest的代码在index_worker.go里: func (engine *Engine) indexerAddDocumentWorker(shard int) ...
- codeforces 459E E. Pashmak and Graph(dp+sort)
题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...