学习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数据类型 ...
随机推荐
- 查看 python安装目录
打开终端 输入: which python 打开终端 依此输入: python import sys print sys.path
- 每天一个JavaScript实例-apply和call的使用方法
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- POJ 2886 Who Gets the Most Candies?(树状数组+二分)
题目链接 注意题目中给的顺序是顺时针的,所以在数组中应该是倒着存的.左就是顺时针,右就是逆时针.各种调试之后,终于A了,很多种情况考虑情况. #include <cstring> #inc ...
- GitExtensions 官方手册(英文)
在线版本(最新版):https://git-extensions-documentation.readthedocs.io/en/latest/ PDF版本(版本号 2.48):http://pan. ...
- HDU 5178 pairs —— 思维 + 二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 4022 Bombing(map,multiset)
题意:n个基地放在2维平面,然后m个炸弹人,每个炸弹人可以炸一行或者一列,输出每个炸弹人炸掉的基地个数. 思路:用map<int,multiset<int> >对应起来一行或者 ...
- java中匹配中文的正则表达式
java中要匹配中文的正则表达式可以有两种写法:一是使用unicode中文码:二是直接使用汉字字符: 例: (1)String str = "晴"; String regexStr ...
- 【HDU 3613】Best Reward
[题目链接] 点击打开链接 [算法] 正反两遍EXKMP,即可 [代码] #include<bits/stdc++.h> using namespace std; #define MAXC ...
- 【Codeforces 664A】 Complicated GCD
[题目链接] 点击打开链接 [算法] gcd(a,a+1) = 1 所以当a = b时,答案为a,否则为1 [代码] #include<bits/stdc++.h> using names ...
- vue-resource 设置请求的参数以formData形式以及设置请求的过滤器
在main.js中添加下面的设置: Vue.http.options.emulateJSON = true;Vue.http.options.headers = {'Content-Type': 'a ...