一、前 言 数据结构是计算机科学中一门很重要的学科,可以说想要了解或者学习算法,数据结构必需要了解。我不是计算机毕业,数据结构大学的时候印象里好像是开过这么课的,但是好像是选修课,没有好好学。现在既然生活中吃了计算机有关的饭碗,那这门课还是有必要仔细学习。下面的数据结构内容来源于algorithms
1.链表-B 链表表示的是元素的线性集合状态,在计算机科学中对链表的定义不是由它们在内存中的物理位置作定义,相反,每个元素指向的是下一个元素从而组成的数据结构,这些元素一起表示一个序列。所以链表的一个缺点是访问时间是线性的,难以管道化。
我们先了解下单向链表,单项链表由两个部分组成,链表本身和链表节点。先看下链表节点:
1 2 [10 |•] → [20 |•] → [30 |•] → NULL ↑head
• 表示指针
NULL 表示链表结束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 export default class LinkedListNodePL {constructor (value, next = null ) {if (next !== null && !(next instanceof LinkedListNodePL )) { throw new TypeError ('next must be null or LinkedListNodePL instance' ); } this .value = value; this .next = next; }toString (callback ) { if (typeof callback === 'function' ) { return callback (this .value ); } if (this .value === null ) return 'null' ; if (this .value === undefined ) return 'undefined' ; if (typeof this .value === 'object' ) return JSON .stringify (this .value ); return String (this .value ); }setValue (value ) { this .value = value; return this ; }setNext (next ) { this .next = next; return this ; }static create (value, next = null ) { return new LinkedListNodePL (value, next); } }
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 import Comparator from '../../utils/comparator/Comparator' ;import LinkedListNodePL from './LinkedListNodePL' ;export default class LinkedListPL {constructor (compatatorFunction ) {this .head = null ; this .tail = null ; this .compare = new Comparator (compatatorFunction); }append (value ) {const newNode = new LinkedListNodePL (value);if (!this .head ) {this .head = newNode;this .tail = newNode;return this ; }this .tail .next = newNode;this .tail = newNode;return this ; }insert (value, rawIndex ) {const index = rawIndex < 0 ? 0 : rawIndex;if (index === 0 ) {this .prepend (value); } else {let count = 1 , currentNode = this .head ;const newNode = new LinkedListNodePL (value); while (currentNode) {if (count === index) break ; currentNode = currentNode.next ; count += 1 ; }if (currentNode) { newNode.next = currentNode.next ; currentNode.next = newNode; } else {if (this .tail ) {this .tail .next = newNode;this .tail = newNode; } else {this .head = newNode;this .tail = newNode; }}}}prepend (value ) {const newNode = new LinkedListNodePL (value, this .head );this .head = newNode;if (!this .tail ) {this .tail = newNode; }return this ; }toString (callback ) {return this .toArray ().map ((node ) => node.toString (callback)).toString (); } toArray ( ) {const nodes = [];let currentNode = this .head ;while (currentNode) { nodes.push (currentNode); currentNode = currentNode.next ; }return nodes; }}delete (value ) {if (!this .head ) return null ;let deletedNode = null ;while (this .head && this .compare .equal (this .head .value , value)) { deletedNode = this .head ; this .head = this .head .next ; } let currentNode = this .head ; if (currentNode !== null ) { while (currentNode.next ) { if (this .compare .equal (currentNode.next .value , value)) { deletedNode = currentNode.next ; currentNode.next = currentNode.next .next ; } else { currentNode = currentNode.next ; }}} if (this .compare .equal (this .tail .value , value)) { this .tail = currentNode; } return deletedNode; }
2.双向琏表-B 3.队列-B 4.栈-B 5.哈希表(散列)-B 6.堆-B 7.优先队列-B 8.字典树-A 9.树-A 10.二叉查找树-A 11.AVL 树-A 12.红黑树-A 13.线段树-A 14.树状数组-A 15.图-A 16.并查集-A 17.布隆过滤器-A