|
| 1 | +/* |
| 2 | + * Linked List implementation in JavaScript |
| 3 | + * Copyright (c) 2009 Nicholas C. Zakas |
| 4 | + * See LICENSE for details on license. |
| 5 | + */ |
| 6 | + |
| 7 | +//================================================================= |
| 8 | +// LinkedListNode Implementation |
| 9 | +//================================================================= |
| 10 | + |
| 11 | +//----------------------------------------------------------------- |
| 12 | +// Class LinkedListNode |
| 13 | +//----------------------------------------------------------------- |
| 14 | +// Author(s) |
| 15 | +// Nicholas C. Zakas (NCZ), 9/5/02 |
| 16 | +// |
| 17 | +// Description |
| 18 | +// A linked list data node. |
| 19 | +// |
| 20 | +// Arguments |
| 21 | +// vData (Variant) - the data to store in the node. |
| 22 | +//----------------------------------------------------------------- |
| 23 | +function LinkedListNode(vData) { |
| 24 | + this.data = vData; //the data for this node |
| 25 | + this.next = null; //pointer to next node in the list |
| 26 | +} |
| 27 | + |
| 28 | +//================================================================= |
| 29 | +// LinkedList Implementation |
| 30 | +//================================================================= |
| 31 | + |
| 32 | +/** |
| 33 | + * A linked list implementation in JavaScript. |
| 34 | + * @class LinkedList |
| 35 | + * @constructor |
| 36 | + */ |
| 37 | +function LinkedList() { |
| 38 | + |
| 39 | + /** |
| 40 | + * The number of items in the list. |
| 41 | + * @property _length |
| 42 | + * @type int |
| 43 | + * @private |
| 44 | + */ |
| 45 | + this._length = 0; |
| 46 | + |
| 47 | + /** |
| 48 | + * Pointer to first item in the list. |
| 49 | + * @property _list |
| 50 | + * @type Object |
| 51 | + * @private |
| 52 | + */ |
| 53 | + this._list = null; |
| 54 | +} |
| 55 | + |
| 56 | +LinkedList.prototype = { |
| 57 | + |
| 58 | + //restore constructor |
| 59 | + constructor: LinkedList, |
| 60 | + |
| 61 | + /** |
| 62 | + * Appends some data to the end of the list. This method traverses |
| 63 | + * the existing list and places the value at the end in a new item. |
| 64 | + * @param {variant} data The data to add to the list. |
| 65 | + * @return {Void} |
| 66 | + * @method add |
| 67 | + */ |
| 68 | + add: function (data){ |
| 69 | + |
| 70 | + //create a new item object, place data in |
| 71 | + var item = { |
| 72 | + data: data, |
| 73 | + next: null |
| 74 | + }, |
| 75 | + |
| 76 | + //used to traverse the structure |
| 77 | + current, |
| 78 | + previous; |
| 79 | + |
| 80 | + //special case: no items in the list yet |
| 81 | + if (this._list === null){ |
| 82 | + this._list = item; |
| 83 | + } else { |
| 84 | + previous = this._list; |
| 85 | + current = this._list.next; |
| 86 | + |
| 87 | + while(current){ |
| 88 | + previous = current; |
| 89 | + current = current.next; |
| 90 | + } |
| 91 | + |
| 92 | + previous.next = item; |
| 93 | + } |
| 94 | + |
| 95 | + //don't forget to update the count |
| 96 | + this._length++; |
| 97 | + |
| 98 | + }, |
| 99 | + |
| 100 | + /** |
| 101 | + * Retrieves the data in the given position in the list. |
| 102 | + * @param {int} index The zero-based index of the item whose value |
| 103 | + * should be returned. |
| 104 | + * @return {variant} The value in the "data" portion of the given item |
| 105 | + * or null if the item doesn't exist. |
| 106 | + * @method item |
| 107 | + */ |
| 108 | + item: function(index){ |
| 109 | + |
| 110 | + //check for out-of-bounds values |
| 111 | + if (index > -1 && index < this._length){ |
| 112 | + var current = this._list, |
| 113 | + i = 0; |
| 114 | + |
| 115 | + while(i++ < index){ |
| 116 | + current = current.next; |
| 117 | + } |
| 118 | + |
| 119 | + return current.data; |
| 120 | + } else { |
| 121 | + return null; |
| 122 | + } |
| 123 | + }, |
| 124 | + |
| 125 | + /** |
| 126 | + * Removes the item from the given location in the list. |
| 127 | + * @param {int} index The zero-based index of the item to remove. |
| 128 | + * @return {variant} The data in the given position in the list or null if |
| 129 | + * the item doesn't exist. |
| 130 | + * @method remove |
| 131 | + */ |
| 132 | + remove: function(index){ |
| 133 | + |
| 134 | + //check for out-of-bounds values |
| 135 | + if (index > -1 && index < this._length){ |
| 136 | + |
| 137 | + var current = this._list, |
| 138 | + previous, |
| 139 | + i = 0; |
| 140 | + |
| 141 | + //special case: removing first item |
| 142 | + if (index === 0){ |
| 143 | + this._list = current.next; |
| 144 | + } else { |
| 145 | + |
| 146 | + //find the right location |
| 147 | + while(i++ < index){ |
| 148 | + previous = current; |
| 149 | + current = current.next; |
| 150 | + } |
| 151 | + |
| 152 | + //skip over the item to remove |
| 153 | + previous.next = current.next; |
| 154 | + } |
| 155 | + |
| 156 | + //decrement the length |
| 157 | + this._length--; |
| 158 | + |
| 159 | + //return the value |
| 160 | + return current.data; |
| 161 | + |
| 162 | + } else { |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + }, |
| 167 | + |
| 168 | + /** |
| 169 | + * Returns the number of items in the list. |
| 170 | + * @return {int} The number of items in the list. |
| 171 | + * @method size |
| 172 | + */ |
| 173 | + size: function(){ |
| 174 | + return this._length; |
| 175 | + }, |
| 176 | + |
| 177 | + /** |
| 178 | + * Converts the list into an array. |
| 179 | + * @return {Array} An array containing all of the data in the list. |
| 180 | + * @method toArray |
| 181 | + */ |
| 182 | + toArray: function(){ |
| 183 | + var result = [], |
| 184 | + current = this._list; |
| 185 | + |
| 186 | + while(current){ |
| 187 | + result.push(current.data); |
| 188 | + current = current.next; |
| 189 | + } |
| 190 | + |
| 191 | + return result; |
| 192 | + }, |
| 193 | + |
| 194 | + /** |
| 195 | + * Converts the list into a string representation. |
| 196 | + * @return {String} A string representation of the list. |
| 197 | + * @method toString |
| 198 | + */ |
| 199 | + toString: function(){ |
| 200 | + return this.toArray().toString(); |
| 201 | + } |
| 202 | +}; |
0 commit comments