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
| function linkNode(\_key, \_value) { this.Key = _key; this.Value = _value; this.next = null; } function Link() { this.root = new linkNode(null, null); this.end = this.root; } Link.prototype = { count: 0, value: function (_key) { var i = this.root; while (Boolean(i = i.next)) { if (i.Key == _key) return i.Value; } }, add: function (\_key, \_value) { var i = this.root; while (Boolean(i = i.next)) { if (i.Key == _key) return i.Value = _value; } var node = new linkNode(\_key, \_value); if (this.count == 0) this.root.next = node; else this.end.next = node; this.end = node; this.count++; return _value; }, insert: function (_key, node) { var i = this.root; while (Boolean(i = i.next)) { if (i.Key == _key) { var tmp = i.next; i.next = node; node.next = tmp; break; } } }, insertBefore: function (_key, node) { var i = this.root; while (Boolean(i = i.next)) { if (i.next.Key == _key) { var tmp = i.next; i.next = node; node.next = tmp; break; } } }, remove: function (_key) { var i = this.root; do { if (i.next.Key == _key) { if (i.next.next == null) this.end = i; i.next = i.next.next; this.count--; return; } } while (Boolean(i = i.next)) }, removeAt : function (n) { if (n <= 0) { return; } var preNode = this.getNodeByIndex(n - 1); preNode.next = preNode.next.next; }, removeAll: function () { this.root = new linkNode(null, null); this.end = this.root; this.count = 0; }, exists: function (_key) { var i = this.root; while (Boolean(i = i.next)) if (i.Key == _key) return true; return false; }, getJSON: function () { var me = this; var getChild = function (node) { var str = ""; str += "{\\"Key\\":\\"" + node.Key + "\\",\\"Value\\":" + me.Obj2str(node.Value); if (node.next != null) str += ",\\"next\\":" + getChild(node.next); else str += ",\\"next\\":\\"null\\""; str += "}"; return str; }; var link = "{\\"root\\":{\\"Key\\":\\"null\\",\\"Value\\":\\"null\\",\\"next\\":"; if (this.count == 0) { return "{\\"root\\":{\\"Key\\":\\"null\\",\\"Value\\":\\"null\\",\\"next\\":\\"null\\"},\\"end\\":{\\"Key\\":\\"null\\",\\"Value\\":\\"null\\",\\"next\\":\\"null\\"},\\"count\\":\\"0\\"}"; } link += getChild(this.root.next) + "}"; link += ",\\"end\\":{\\"Key\\":\\"" + this.end.Key + "\\",\\"Value\\":" + me.Obj2str(this.end.Value) + ",\\"next\\":\\"null\\""; link += "},\\"count\\":\\"" + this.count + "\\"}"; return link; }, getArrayJSON: function () { var link = "{'link':["; var i = this.root; while (Boolean(i = i.next)) { link += this.Obj2str(i.Value) + ","; } link = link.substr(0, link.length - 1); link += "]}"; return link; }, getNodeByIndex: function (n) { var p = this.head; var i = 0; while (p.next != null && i < n) { p = p.next; i++; } return p; }, getNodeByValue: function (v) { var p = this.head; while (p.next != null) { p = p.next; if (p.data == v) { return p; } } return null; }, print: function () { var p = this.head; while (p.next != null) { p = p.next; print(p.data + " "); } println(""); }, sort: function (fn) { if (fn != null) { var i = this.root; while (Boolean(i = i.next)) { var j = this.root; while (Boolean(j = j.next)) { if (j.next != null) { if (fn.call(this, j)) { var Key = j.Key; var Value = j.Value; j.Key = j.next.Key; j.Value = j.next.Value; j.next.Key = Key; j.next.Value = Value; } } } this.end = i; } } } }; function print(msg) { document.write(msg); }
function println(msg) { print(msg + "<br/>"); }
function hasSameValueNode(singleLink) { var i = singleLink.head; while (i.next != null) { i = i.next; var j = i; while (j.next != null) { j = j.next; if (i.data == j.data) { return true; } } } return false; }
function reverseSingleLink(singleLink) { var arr = new Array(); var p = singleLink.head; while (p.next != null) { p = p.next; arr.push(p.data); } var newLink = new SingleLink(); for (var i = arr.length - 1; i >= 0; i--) { newLink.insert(arr[i]); } return newLink; }
|