学习数据结构目的是什么?
也许只是为了不矮科班出身同事一头.
也许只是为了应付常见的面试题.
也许项目中真的会遇到.
对于个人来说, 只是无聊, 敲点代码记录一下而已.
动机有了, 对策呢?
把学习 JavaScript 数据结构与算法一书中的代码敲一遍应该就没啥问题了. 书比较薄, 178 页, 加油!
书籍配套源码地址是:
https://github.com/loiane/javascript-datastructures-algorithms https://github.com/loiane/javascript-datastructures-algorithms ------
第一篇, 栈.
先进后出, 几个 api. 话说 js 中数组太强大, 栈实现起来太过简单.
javascript 代码
- function Stack() { var items = [];
- this.push = function(element) {
- items.push(element);
- };
- this.pop = function() {
- return items.pop();
- };
- this.peek = function() {
- return items[items.length - 1];
- };
- this.isEmpty = function() {
- return items.length == 0;
- };
- this.size = function() {
- return items.length;
- };
- this.clear = function() {
- items = [];
- };
- this.print = function() {
- console.log(items.toString());
- };
- }
- var stack = new Stack();
- console.log(stack.isEmpty());//true
- stack.push(5);
- stack.push(8);
- console.log(stack.peek());//8
- stack.push(11);
- console.log(stack.size());//3
- console.log(stack.isEmpty());//false
- stack.push(15);
- stack.pop();
- stack.pop();
- console.log(stack.size());//2
- stack.print();//5,8
应用 1:10 进制转换为 2 进制
逐步求余, 然后倒转过来就行.
javascript 代码
- function Stack() {
- var items = [];
- this.push = function(element) {
- items.push(element);
- };
- this.pop = function() {
- return items.pop();
- };
- this.peek = function() {
- return items[items.length - 1];
- };
- this.isEmpty = function() {
- return items.length == 0;
- };
- this.size = function() {
- return items.length;
- };
- this.clear = function() {
- items = [];
- };
- this.print = function() {
- console.log(items.toString());
- };
- }
- function divideBy2(decNumber) {
- var remStack = new Stack(),
- rem,
- binaryString = "";
- while (decNumber> 0) {
- rem = Math.floor(decNumber % 2);
- remStack.push(rem);
- decNumber = Math.floor(decNumber / 2);
- }
- while (!remStack.isEmpty()) {
- binaryString += remStack.pop().toString();
- }
- return binaryString;
- }
- console.log(divideBy2(233));//11101001
- console.log(divideBy2(10));//1010
- console.log(divideBy2(1000));//1111101000
- function Stack() {
- var items = [];
- this.push = function(element) {
- items.push(element);
- };
- this.pop = function() {
- return items.pop();
- };
- this.peek = function() {
- return items[items.length - 1];
- };
- this.isEmpty = function() {
- return items.length == 0;
- };
- this.size = function() {
- return items.length;
- };
- this.clear = function() {
- items = [];
- };
- this.print = function() {
- console.log(items.toString());
- };
- }
- function baseConverter(decNumber, base) {
- var remStack = new Stack(),
- rem,
- baseString = "",
- digits = '0123456789ABCDEF';
- while (decNumber> 0) {
- rem = Math.floor(decNumber % base);
- remStack.push(rem);
- decNumber = Math.floor(decNumber / base);
- }
- while (!remStack.isEmpty()) {
- baseString += digits[remStack.pop()];
- }
- return baseString;
- }
- console.log(baseConverter(100345, 2));//11000011111111001
- console.log(baseConverter(100345, 8));//303771
- console.log(baseConverter(100345, 16));//187F9
- console.log((100345).toString(2));//11000011111111001
- console.log((100345).toString(8));//303771
- console.log((100345).toString(16));//187F9
来源: http://www.qdfuns.com/article/17398/907b69843f6e8b069a19536888423d24.html