写在前面
作为前端开发者而言,可能不会像后端开发那样遇到很多的算法和数据结构问题,但是不论是做前端、 服务端还是客户端, 任何一个程序员都会开始面对更加复杂的问题, 这个时候算法和数据结构知识就变得不可或缺,它是编程能力中很重要的一部分。
如今的前端技术发展飞快,再也不像以前那样只负责视图层了,更多的交互和数据逻辑需要前端来做,这个时候对于算法和数据结构就有着更高的要求。此外,前端还可以去做node端,做数据可视化,做WebGL,三维建模,VR/AR等,这些新兴的领域对算法和数据有着更高的要求,所以想要成为一名高水平的前端工程师,就需要在数据和算法方面做更多的积累。
最近读完了一本书,是O’Reilly出本社出版的《数据结构与算法JavaScript描述》,这本书使用javascript描述了常见的各种数据结构与算法,可谓是js界的《算法导论》,前端工程师如果想要提升算法数据结构方面的基础,个人觉得这本书是非常好的选择,下面是我学习过程中的一些收获和总结。
PS:这里我把学习的一些总结都写在了代码的注释里,这也是我个人学东西的一个习惯,一边学一边跑代码,把重要的一些总结写在注释里。这样很有助于技术学习的沉淀,之后查起来也很方便,所以请留意注释中的内容。
一 开发环境
这里使用的是火狐官方的一个工具js shell,安装之后可以使用js命令来在命令行中执行js脚本,执行的环境是Google V8引擎。
下载:
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell
将命令安装到全局就能使用,我用的是mac,安装的时候把二进制程序和依赖的包拷到/usr/local/bin,就可以在命令行中使用js命令了。
可以使用readline()从命令行中获取输入,使用print()输出结果。
比如,一个简答的a+b的程序 a+b.js:
var line;while(line = readline()){ line = line.split(' '); print(parseInt(line[0]) + parseInt(line[1]));}
命令行中运行(文件路径注意写对,是绝对路径)
js /myproject/algorithms/a+b.js
就可以在命令行中输入参数,回车得到结果了,是不是很像用 C++ 命令行解算法题,没错,就是这么方便。
当然也可以使用 Node.js 环境来执行,具体参考Node.js官方文档即可。
二 对象和面向对象编程
js中5种数据类型,并没有定义更多的数据类型,但是运用js中一切皆对象的思想,可以自己很方便的去构造出各种复杂的数据类型对象。
这里讨论到的数据结构都被实现为对象。 JavaScript 提供了多种方式来创建和使用对象。 这里通过如下方式创建:
定义包含属性和方法声明的构造函数, 并在构造函数后紧跟方法
的定义。
下面是一个检查银行账户对象的构造函数:
function Checking(amount) {this.balance = amount; this.deposit = deposit; this.withdraw = withdraw; this.toString = toString; }
this 关键字用来将方法和属性绑定到一个对象的实例上。 下面我们看看对于前面声明过的方法是如何定义的:
function deposit(amount) {this.balance += amount;} function withdraw(amount) {if (amount <= this.balance) {this.balance -= amount;} if (amount > this.balance) {print("Insufficient funds");}} function toString() {return "Balance: " + this.balance;}
使用时这样:
var account = new Checking(500);account.deposit(1000);print(account.toString()); account.withdraw(750);print(account.toString()); account.withdraw(800); print(account.toString());
这就是一个定义数据对象的方法,下面构建各种数据结构也是使用这样的方式(除过数组,因为js本身定义了数组类型;其次,其他的数据结构的定义有很多事使用数据作为基础数据结构进行数据存储的)。
三 数组
1.字符串分割为数组split与数组元素拼接转字符串join
var sentence = "the quick brown fox jumped over the lazy dog";var words = sentence.split(" ");for (var i = 0; i < words.length; ++i) { print("word " + i + ": " + words[i]);}
2.indexOf-查找数组是否存在某元素及下标
var names = ["David","Cynthia","Raymond","Clayton","Jennifer"];putstr("Enter a name to search for: ");var name = readline();var position = names.indexOf(name);if (position >= 0) { print("Found " + name + " at position " + position);}else { print(name + " not found in array.");}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3.数组中间添加和删除修改元素splice
直接上代码,总结见注释:
var nums = [1,2,3,7,8,9];var newElements = [4,5,6];nums.splice(3,0,newElements);print(nums); var nums = [1,2,3,7,8,9];nums.splice(3,0,4,5,6);print(nums);var nums = [1,2,3,100,200,300,400,4,5];nums.splice(3,4);print(nums);
- 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
4.不生成新数组的迭代器方法-forEach每个元素都操作-every所有都满足-some有一个满足-reduce累计操作
直接上代码,总结见注释:
function squareFunc(num) { print(num, num * num); }var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];nums.forEach(squareFunc);function isEven(num) { return num % 2 == 0;}var nums = [2, 4, 6, 8, 10];var even = nums.every(isEven);if (even) { print("all numbers are even");} else { print("not all numbers are even");}function add(runningTotal, currentValue) { return runningTotal + currentValue;}var nums = [1,2,3,4,5,6,7,8,9,10];var sum = nums.reduce(add); print(sum); function concat(accumulatedString, item) { return accumulatedString + item;}var words = ["the ", "quick ","brown ", "fox "];var sentence = words.reduce(concat);print(sentence); function concat(accumulatedString, item) { return accumulatedString + item;}var words = ["the ", "quick ","brown ", "fox "];var sentence = words.reduceRight(concat);print(sentence);
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
5.生成新数组的迭代器方法-map每个元素都执行某操作结果组成的数组-filter数组中满足某条件的元素组成的数组
直接上代码,总结见注释:
function curve(grade) { return grade += 5;}var grades = [77, 65, 81, 92, 83];var newgrades = grades.map(curve);print(newgrades); function first(word) { return word[0];}var words = ["for", "your", "information"];var acronym = words.map(first);print(acronym)print(acronym.join("")); function isEven(num) { return num % 2 == 0;}function isOdd(num) { return num % 2 != 0;}var nums = [];for (var i = 0; i < 20; ++i) { nums[i] = i + 1;}var evens = nums.filter(isEven);print("Even numbers: ");print(evens);var odds = nums.filter(isOdd);print("Odd numbers: ");print(odds);function passing(num) { return num >= 60;}var grades = [];for (var i = 0; i < 20; ++i) { grades[i] = Math.floor(Math.random() * 101);}var passGrades = grades.filter(passing);print("All grades:");print(grades);print("Passing grades: ");print(passGrades);function afterc(str) { if (str.indexOf("cie") > -1) { return true; } return false;}var words = ["recieve","deceive","percieve","deceit","concieve"];var misspelled = words.filter(afterc);print(misspelled);
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
6.二维数组和多维数组
直接上代码,总结见注释:
Array.matrix = function(numrows, numcols, initial) { var arr = []; for (var i = 0; i < numrows; ++i) { var columns = []; for (var j = 0; j < numcols; ++j) { columns[j] = initial; } arr[i] = columns; } return arr;}var nums = Array.matrix(5,5,0);print(nums[1][1]); var names = Array.matrix(3,3,"");names[1][2] = "Joe";print(names[1][2]); var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];print(grades[2][2]); var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];var total = 0;var average = 0.0;for (var row = 0; row < grades.length; ++row) { for (var col = 0; col < grades[row].length; ++col) { total += grades[row][col]; } average = total / grades[row].length; print("Student " + parseInt(row+1) + " average: " + average.toFixed(2)); total = 0; average = 0.0;}var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];var total = 0;var average = 0.0;for (var col = 0; col < grades.length; ++col) { for (var row = 0; row < grades[col].length; ++row) { total += grades[row][col]; } a verage = total / grades[col].length; print("Test " + parseInt(col+1) + " average: " + average.toFixed(2)); total = 0; average = 0.0;}var grades = [[89, 77],[76, 82, 81],[91, 94, 89, 99]];var total = 0;var average = 0.0;for (var row = 0; row < grades.length; ++row) { for (var col = 0; col < grades[row].length; ++col) { total += grades[row][col]; } average = total / grades[row].length; print("Student " + parseInt(row+1) + " average: " + average.toFixed(2)); total = 0; average = 0.0;}function Point(x,y) { this.x = x; this.y = y;}function displayPts(arr) { for (var i = 0; i < arr.length; ++i) { print(arr[i].x + ", " + arr[i].y); }}var p1 = new Point(1,2);var p2 = new Point(3,5);var p3 = new Point(2,8);var p4 = new Point(4,4);var points = [p1,p2,p3,p4];for (var i = 0; i < points.length; ++i) { print("Point " + parseInt(i+1) + ": " + points[i].x + ", " + points[i].y);}var p5 = new Point(12,-3);points.push(p5);print("After push: ");displayPts(points);points.shift();print("After shift: ");displayPts(points);function weekTemps() { this.dataStore = []; this.add = add; this.average = average;}function add(temp) { this.dataStore.push(temp); }function average() { var total = 0; for (var i = 0; i < this.dataStore.length; ++i) { total += this.dataStore[i]; } return total / this.dataStore.length;}var thisWeek = new weekTemps();thisWeek.add(52);thisWeek.add(55);thisWeek.add(61);thisWeek.add(65);thisWeek.add(55);thisWeek.add(50);thisWeek.add(52);thisWeek.add(49);print(thisWeek.average());
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
四 列表-js实现
直接上代码,总结见注释:
function List() { this.listSize = 0; this.pos = 0; this.dataStore = []; this.clear = clear; this.find = find; this.listToString = listToString; this.insert = insert; this.append = append; this.remove = remove; this.front = front; this.end = end; this.prev = prev; this.next = next; this.length = length; this.currPos = currPos; this.moveTo = moveTo; this.getElement = getElement; this.length = length; this.contains = contains;}function append(element) { this.dataStore[this.listSize++] = element;}function find(element) { for (var i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] == element) { return i; } } return -1;}function remove(element) { var foundAt = this.find(element); if (foundAt > -1) { this.dataStore.splice(foundAt, 1); --this.listSize; return true; } return false;}function length() { return this.listSize;}function listToString() { return this.dataStore;}function insert(element, after) { var insertPos = this.find(after); if (insertPos > -1) { this.dataStore.splice(insertPos + 1, 0, element); ++this.listSize; return true; } return false;}function clear() { delete this.dataStore; this.dataStore = []; this.listSize = this.pos = 0;}function contains(element) { for (var i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] == element) { return true; } } return false;}function front() { this.pos = 0;}function end() { this.pos = this.listSize - 1;}function prev() { if (this.pos > 0) { --this.pos; }}function next() { if (this.pos < this.listSize - 1) { ++this.pos; }}function currPos() { return this.pos;}function moveTo(position) { this.pos = position;}function getElement() { return this.dataStore[this.pos];}var names = new List();names.append("Clayton");names.append("Raymond");names.append("Cynthia");names.append("Jennifer");names.append("Bryan");names.append("Danny");names.front();print(names.getElement()); names.next();print(names.getElement()); names.next();names.next();names.prev();print(names.getElement()); for (names.front(); names.currPos() < names.length(); names.pos++) { print(names.getElement()); }names.pos -= 1;for(names.end(); names.currPos() >= 0; names.pos--) { print(names.getElement());}names.pos += 1;
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
五 栈-js实现
入栈push-出栈pop-访问栈顶元素peek-清空栈clear
直接上代码,总结见注释:
function Stack() { this.dataStore = []; this.top = 0; this.push = push; this.pop = pop; this.peek = peek; this.length = length; this.clear = clear;}function push(element) { this.dataStore[this.top++] = element;}function pop() { return this.dataStore[--this.top];}function peek() { return this.dataStore[this.top-1];}function length(){ return this.top;}function clear() { this.top = 0;}var s = new Stack();s.push("David");s.push("Raymond");s.push("Bryan");print("length: " + s.length());print(s.peek());var popped = s.pop();print("The popped element is: " + popped);print(s.peek());s.push("Cynthia");print(s.peek());s.clear();print("length: " + s.length());print(s.peek());s.push("Clayton");print(s.peek());function mulBase(num, base) { var s = new Stack(); do { s.push(num % base); num = Math.floor(num /= base); } while (num > 0); var converted = ""; while (s.length() > 0) { converted += s.pop(); } return converted;}function mulBase(num, base) { var s = new Stack(); do { s.push(num % base); num = Math.floor(num /= base); } while (num > 0); var converted = ""; while (s.length() > 0) { converted += s.pop(); } return converted;}var num = 32;var base = 2;var newNum = mulBase(num, base);print(num + " converted to base " + base + " is " + newNum);num = 125;base = 8;var newNum = mulBase(num, base);print(num + " converted to base " + base + " is " + newNum);function isPalindrome(word) { var s = new Stack(); for (var i = 0; i < word.length; ++i) { s.push(word[i]); } var rword = ""; while (s.length() > 0) { rword += s.pop(); } if (word == rword) { return true; } else { return false; }}var word = "hello";if (isPalindrome(word)) { print(word + " is a palindrome.");}else { print(word + " is not a palindrome.");}word = "racecar"if (isPalindrome(word)) { print(word + " is a palindrome.");}else { print(word + " is not a palindrome.");}function factorial(n) { if (n === 0) { return 1; } else { return n * factorial(n-1); }}print(factorial(5));function fact(n) { var s = new Stack(); while (n > 1) { s.push(n--); } var product = 1; while (s.length() > 0) { product *= s.pop(); } return product;}print(fact(5));
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
六 队列-js实现
enqueue向队尾添加一个元素-dequeue删除队首元素-front读取队首元素-back读取队尾元素queueToString显示队列内的所有元素-empty判断队列是否为空
直接上代码,总结见注释:
function Queue() { this.dataStore = []; this.enqueue = enqueue; this.dequeue = dequeue; this.front = front; this.back = back; this.queueToString = queueToString; this.empty = empty;}function enqueue(element) { this.dataStore.push(element);}function dequeue() { return this.dataStore.shift();}function front() { return this.dataStore[0];}function back() { return this.dataStore[this.dataStore.length-1];}function queueToString() { var retStr = ""; for (var i = 0; i < this.dataStore.length; ++i) { retStr += this.dataStore[i] + "\n"; } return retStr;}function empty() { if (this.dataStore.length == 0) { return true; } else { return false; }}var q = new Queue();q.enqueue("Meredith");q.enqueue("Cynthia");q.enqueue("Jennifer");print(q.queueToString());q.dequeue();print(q.queueToString());print("Front of queue: " + q.front());print("Back of queue: " + q.back());
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
七 双向链表-js实现
比单向链表功能更强一些-find查找节点值-insert插入节点-remove删除节点-display打印所有节点-findLast找链表最后一个节点-dispReverse反序显示双向链表中所有元素
直接上代码,总结见注释:
function Node(element) { this.element = element; this.next = null; this.previous = null;}function LList() { this.head = new Node("head"); this.find = find; this.insert = insert; this.display = display; this.remove = remove; this.findLast = findLast; this.dispReverse = dispReverse;}function insert(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; newNode.previous = current; current.next = newNode;}function remove(item) { var currNode = this.find(item); if (!(currNode.next == null)) { currNode.previous.next = currNode.next; currNode.next.previous = currNode.previous; currNode.next = null; currNode.previous = null; }}function findLast() { var currNode = this.head; while (!(currNode.next == null)) { currNode = currNode.next; } return currNode;}function dispReverse() { var currNode = this.head; currNode = this.findLast(); while (!(currNode.previous == null)) { print(currNode.element); currNode = currNode.previous; }}function display() { var currNode = this.head; while (!(currNode.next == null)) { print(currNode.next.element); currNode = currNode.next; }}function find(item) { var currNode = this.head; while (currNode.element != item) { currNode = currNode.next; } return currNode;}function insert(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; newNode.previous = current; current.next = newNode;}var cities = new LList();cities.insert("Conway", "head");cities.insert("Russellville", "Conway");cities.insert("Carlisle", "Russellville");cities.insert("Alma", "Carlisle");cities.display();print();cities.remove("Carlisle");cities.display();print();cities.dispReverse();
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
八 字典-js实现
按键值对进行存储查询-add新增键值对-find以键作为参数返值-remove删一个键值对-showAll显示所有键值对-count得到元素个数-clear清空所有键值对-showAllBySortKey按键排序后显示所有键值对
直接上代码,总结见注释:
function Dictionary() { this.add = add; this.datastore = new Array(); this.find = find; this.remove = remove; this.showAll = showAll; this.count = count; this.clear = clear; this.showAllBySortKey = showAllBySortKey;}function add(key, value) { this.datastore[key] = value;}function find(key) { return this.datastore[key];}function remove(key) { delete this.datastore[key];}function showAll() { for(var key in this.datastore) { print(key + " -> " + this.datastore[key]); }}function count() { var n = 0; for(var key in Object.keys(this.datastore)) { ++n; } return n;}function clear() { for (var key in Object.keys(this.datastore)) { delete this.datastore[key]; }}function showAllBySortKey() { for(var i in Object.keys(this.datastore).sort()) { var key = Object.keys(this.datastore).sort()[i]; print(key + " -> " + this.datastore[key]); }}var pbook = new Dictionary();pbook.add("Mike","123");pbook.add("David", "345");pbook.add("Cynthia", "456");print("David's extension: " + pbook.find("David"));pbook.showAll();pbook.showAllBySortKey();pbook.clear();print("Number of entries: " + pbook.count());pbook.showAll();
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
九 散列-js实现
散列表HashTable-插入删除和取用数据非常快-查找特定值效率低.js
直接上代码,总结见注释:
function HashTable() { this.table = new Array(137); this.simpleHash = simpleHash; this.betterHash = betterHash; this.showDistro = showDistro; this.put = put; }function put(data) { var pos = this.betterHash(data); this.table[pos] = data;}function simpleHash(data) { var total = 0; for (var i = 0; i < data.length; ++i) { total += data.charCodeAt(i); } return total % this.table.length;}function betterHash(string) { const H = 37; var total = 0; for (var i = 0; i < string.length; ++i) { total += H * total + string.charCodeAt(i); } total = total % this.table.length; if (total < 0) { total += this.table.length-1; } return parseInt(total);}function showDistro() { var n = 0; for (var i = 0; i < this.table.length; ++i) { if (this.table[i] != undefined) { print(i + ": " + this.table[i]); } }}var someNames = ["David", "Jennifer", "Donnie", "Raymond", "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"];var hTable = new HashTable();for (var i = 0; i < someNames.length; ++i) { hTable.put(someNames[i]);}hTable.showDistro();
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
十 树-js实现
树-二叉查找树BST实现-insert插入新节点-inOrder中序遍历-preOrder先序遍历-postOrder后序遍历-getMin查找最小值-getMax查找最大值-find查找给定值
直接上代码,总结见注释:
function Node(data, left, right) { this.data = data; this.left = left; this.right = right; this.show = show;}function show() { return this.data;}function BST() { this.root = null; this.insert = insert; this.inOrder = inOrder; this.preOrder = preOrder; this.postOrder = postOrder; this.getMax = getMax; this.getMin = getMin; this.find = find;}function insert(data) { var n = new Node(data, null, null); if (this.root == null) { this.root = n; } else { var current = this.root; var parent; while (true) { parent = current; if (data < current.data) { current = current.left; if (current == null) { parent.left = n; break; } } else { current = current.right; if (current == null) { parent.right = n; break; } } } }}function inOrder(node) { if (!(node == null)) { inOrder(node.left); putstr(node.show() + " "); inOrder(node.right); }}function preOrder(node) { if (!(node == null)) { putstr(node.show() + " "); preOrder(node.left); preOrder(node.right); }}function postOrder(node) { if (!(node == null)) { postOrder(node.left); postOrder(node.right); putstr(node.show() + " "); }}var nums = new BST();nums.insert(23);nums.insert(45);nums.insert(16);nums.insert(37);nums.insert(3);nums.insert(99);nums.insert(22);print("Inorder traversal: ");inOrder(nums.root);print("Preorder traversal: ");preOrder(nums.root);print("Postorder traversal: ");postOrder(nums.root);function getMin() { var current = this.root; while (!(current.left == null)) { current = current.left; } return current.data;}function getMax() { var current = this.root; while (!(current.right == null)) { current = current.right; } return current.data;}function find(data) { var current = this.root; while (current != null) { if (current.data == data) { return current; } else if (data < current.data) { current = current.left; } else { current = current.right; } } return null;}var nums = new BST();nums.insert(23);nums.insert(45);nums.insert(16);nums.insert(37);nums.insert(3);nums.insert(99);nums.insert(22);inOrder(nums.root);print("\n");putstr("Enter a value to search for: ");var value = parseInt(readline());var found = nums.find(value);if (found != null) { print("Found " + value + " in the BST.");}else { print(value + " was not found in the BST.");}function remove(data) { root = removeNode(this.root, data);}function removeNode(node, data) { if (node == null) { return null; } if (data == node.data) { if (node.left == null && node.right == null) { return null; } if (node.left == null) { return node.right; } if (node.right == null) { return node.left; } var tempNode = getSmallest(node.right); node.data = tempNode.data; node.right = removeNode(node.right, tempNode.data); return node; } else if (data < node.data) { node.left = removeNode(node.left, data); return node; } else { node.right = removeNode(node.right, data); return node; }}
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
十一 图-js实现
直接上代码,总结见注释:
function Vertex(label) { this.label = label;}function Graph(v) { this.vertices = v; this.edges = 0; this.adj = []; for (var i = 0; i < this.vertices; ++i) { this.adj[i] = []; this.adj[i].push(""); } this.marked = []; for (var i = 0; i < this.vertices; ++i ) { this.marked[i] = false; } this.edgeTo = []; this.addEdge = addEdge; this.showGraph = showGraph; this.dfs = dfs; this.bfs = bfs;}function addEdge(v, w) { this.adj[v].push(w); this.adj[w].push(v); this.edges++;}function showGraph() { for (var i = 0; i < this.vertices; ++i) { putstr(i + "->"); for (var j = 0; j < this.vertices; ++j) { if (this.adj[i][j] != undefined) putstr(this.adj[i][j] + ' '); } print(); }}g = new Graph(5);g.addEdge(0,1);g.addEdge(0,2);g.addEdge(1,3);g.addEdge(2,4);g.showGraph();function dfs(v) { this.marked[v] = true; if (this.adj[v] != undefined) print("Visited vertex: " + v); for (var w in this.adj[v]) { if (!this.marked[w]) { this.dfs(w); } }}g = new Graph(5);g.addEdge(0, 1);g.addEdge(0,2);g.addEdge(1,3);g.addEdge(2,4);g.showGraph();g.dfs(0);g = new Graph(5);g.addEdge(0, 1);g.addEdge(0, 2);g.addEdge(1, 3);g.addEdge(2, 4);g.showGraph();g.bfs(0);
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
另外还有一些基本的算法像排序,检索,动态规划,贪心,只要知道了如何用js去实现数据结构和算法,其他的都是共通的。建议配合这本《数据结构与算法JavaScript描述》和《算法导论》一起读,然后把《算法导论》中的重要算法全部用js实现一遍,相信对编程能力会有很大的提升。