|
| 1 | +<head> |
| 2 | + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ></link> |
| 3 | + <style> |
| 4 | + pre, code { |
| 5 | + white-space: pre-line; |
| 6 | + min-width: 400px; |
| 7 | + } |
| 8 | + </style> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | + <h1>Node Class API</h1> |
| 12 | + <table class="table"> |
| 13 | + <thead> |
| 14 | + <tr> |
| 15 | + <td>Function</td> |
| 16 | + <td>Arguments</td> |
| 17 | + <td>Returns</td> |
| 18 | + <td>Directions</td> |
| 19 | + <td>Example</td> |
| 20 | + </tr> |
| 21 | + </thead> |
| 22 | + <tbody> |
| 23 | + <tr> |
| 24 | + <td>constructor</td> |
| 25 | + <td>(Data, Node)</td> |
| 26 | + <td>Node</td> |
| 27 | + <td> |
| 28 | + Creates a class instance to represent a node. The node should |
| 29 | + have two properties, 'data' and 'next'. Accept both |
| 30 | + of these as arguments to the 'Node' constructor, then |
| 31 | + assign them to the instance as properties 'data' and 'next'. |
| 32 | + If 'next' is not provided to the constructor, then default its |
| 33 | + value to be 'null'. |
| 34 | + </td> |
| 35 | + <td> |
| 36 | + <pre> |
| 37 | + const n = new Node('Hi'); |
| 38 | + n.data // 'Hi' |
| 39 | + n.next // null |
| 40 | + const n2 = new Node('There', n); |
| 41 | + n.next // returns n |
| 42 | + </pre> |
| 43 | + </td> |
| 44 | + </tr> |
| 45 | + </tbody> |
| 46 | + </table> |
| 47 | + |
| 48 | + <h1>LinkedList Class API</h1> |
| 49 | + <table class="table"> |
| 50 | + <thead> |
| 51 | + <tr> |
| 52 | + <td>Function</td> |
| 53 | + <td>Arguments</td> |
| 54 | + <td>Returns</td> |
| 55 | + <td>Directions</td> |
| 56 | + <td>Example</td> |
| 57 | + </tr> |
| 58 | + </thead> |
| 59 | + <tbody> |
| 60 | + <tr> |
| 61 | + <td>constructor</td> |
| 62 | + <td>-</td> |
| 63 | + <td>(LinkedList)</td> |
| 64 | + <td> |
| 65 | + Create a class to represent a linked list. When created, |
| 66 | + a linked list should have *no* head node associated with it. |
| 67 | + The LinkedList instance will have one property, 'head', which |
| 68 | + is a reference to the first node of the linked list. By default |
| 69 | + 'head' should be 'null'. |
| 70 | + </td> |
| 71 | + <td> |
| 72 | + <pre> |
| 73 | + const list = new LinkedList(); |
| 74 | + list.head // null |
| 75 | + </pre> |
| 76 | + </td> |
| 77 | + </tr> |
| 78 | + <tr> |
| 79 | + <td>insertFirst</td> |
| 80 | + <td>(data)</td> |
| 81 | + <td>-</td> |
| 82 | + <td> |
| 83 | + Creates a new Node from argument 'data' and assigns the resulting |
| 84 | + node to the 'head' property. Make sure to handle the case in which |
| 85 | + the linked list already has a node assigned to the 'head' property. |
| 86 | + </td> |
| 87 | + <td> |
| 88 | + <pre> |
| 89 | + const list = new LinkedList(); |
| 90 | + list.insertFirst('Hi There'); // List has one node |
| 91 | + </pre> |
| 92 | + </td> |
| 93 | + </tr> |
| 94 | + <tr> |
| 95 | + <td>size</td> |
| 96 | + <td>-</td> |
| 97 | + <td>(integer)</td> |
| 98 | + <td> |
| 99 | + Returns the number of nodes in the linked list. |
| 100 | + </td> |
| 101 | + <td> |
| 102 | + <pre> |
| 103 | + const list = new LinkedList(); |
| 104 | + list.insertFirst('a'); |
| 105 | + list.insertFirst('b'); |
| 106 | + list.insertFirst('c'); |
| 107 | + list.size(); // returns 3 |
| 108 | + </pre> |
| 109 | + </td> |
| 110 | + </tr> |
| 111 | + <tr> |
| 112 | + <td>getFirst</td> |
| 113 | + <td>-</td> |
| 114 | + <td>(Node)</td> |
| 115 | + <td> |
| 116 | + Returns the first node of the linked list. |
| 117 | + </td> |
| 118 | + <td> |
| 119 | + <pre> |
| 120 | + const list = new LinkedList(); |
| 121 | + list.insertFirst('a'); |
| 122 | + list.insertFirst('b'); |
| 123 | + list.getFirst(); // returns Node instance with data 'a' |
| 124 | + </pre> |
| 125 | + </td> |
| 126 | + </tr> |
| 127 | + <tr> |
| 128 | + <td> |
| 129 | + getLast |
| 130 | + </td> |
| 131 | + <td> |
| 132 | + - |
| 133 | + </td> |
| 134 | + <td> |
| 135 | + (Node) |
| 136 | + </td> |
| 137 | + <td> |
| 138 | + Returns the last node of the linked list |
| 139 | + </td> |
| 140 | + <td> |
| 141 | + <pre> |
| 142 | + const list = new LinkedList(); |
| 143 | + list.insertFirst('a'); |
| 144 | + list.insertFirst('b'); |
| 145 | + list.getLast(); // returns node with data 'a' |
| 146 | + </pre> |
| 147 | + </td> |
| 148 | + </tr> |
| 149 | + <tr> |
| 150 | + <td> |
| 151 | + clear |
| 152 | + </td> |
| 153 | + <td> |
| 154 | + - |
| 155 | + </td> |
| 156 | + <td> |
| 157 | + - |
| 158 | + </td> |
| 159 | + <td> |
| 160 | + Empties the linked list of any nodes. |
| 161 | + </td> |
| 162 | + <td> |
| 163 | + <pre> |
| 164 | + const list = new LinkedList(); |
| 165 | + list.insertFirst('a'); |
| 166 | + list.insertFirst('b'); |
| 167 | + list.clear(); |
| 168 | + list.size(); // returns 0 |
| 169 | + </pre> |
| 170 | + </td> |
| 171 | + </tr> |
| 172 | + <tr> |
| 173 | + <td> |
| 174 | + removeFirst |
| 175 | + </td> |
| 176 | + <td> |
| 177 | + - |
| 178 | + </td> |
| 179 | + <td> |
| 180 | + - |
| 181 | + </td> |
| 182 | + <td> |
| 183 | + Removes only the first node of the linked list. The list's head should |
| 184 | + now be the second element. |
| 185 | + </td> |
| 186 | + <td> |
| 187 | + <pre> |
| 188 | + const list = new LinkedList(); |
| 189 | + list.insertFirst('a'); |
| 190 | + list.insertFirst('b'); |
| 191 | + list.removeFirst(); |
| 192 | + list.getFirst(); // returns node with data 'a' |
| 193 | + </pre> |
| 194 | + </td> |
| 195 | + </tr> |
| 196 | + <tr> |
| 197 | + <td> |
| 198 | + removeLast |
| 199 | + </td> |
| 200 | + <td> |
| 201 | + - |
| 202 | + </td> |
| 203 | + <td> |
| 204 | + - |
| 205 | + </td> |
| 206 | + <td> |
| 207 | + Removes the last node of the chain |
| 208 | + </td> |
| 209 | + <td> |
| 210 | + <pre> |
| 211 | + const list = new LinkedList(); |
| 212 | + list.insertFirst('a'); |
| 213 | + list.insertFirst('b'); |
| 214 | + list.removeLast(); |
| 215 | + list.size(); // returns 1 |
| 216 | + list.getLast(); // returns node with data of 'b' |
| 217 | + </pre> |
| 218 | + </td> |
| 219 | + </tr> |
| 220 | + <tr> |
| 221 | + <td> |
| 222 | + insertLast |
| 223 | + </td> |
| 224 | + <td> |
| 225 | + (Data) |
| 226 | + </td> |
| 227 | + <td> |
| 228 | + - |
| 229 | + </td> |
| 230 | + <td> |
| 231 | + Inserts a new node with provided data at the end of the chain |
| 232 | + </td> |
| 233 | + <td> |
| 234 | + <pre> |
| 235 | + const list = new LinkedList(); |
| 236 | + list.insertFirst('a'); |
| 237 | + list.insertFirst('b'); |
| 238 | + list.insertLast('c'); |
| 239 | + list.getLast(); // returns node with data 'C' |
| 240 | + </pre> |
| 241 | + </td> |
| 242 | + </tr> |
| 243 | + <tr> |
| 244 | + <td> |
| 245 | + getAt |
| 246 | + </td> |
| 247 | + <td> |
| 248 | + (integer) |
| 249 | + </td> |
| 250 | + <td> |
| 251 | + (Node) |
| 252 | + </td> |
| 253 | + <td> |
| 254 | + Returns the node at the provided index |
| 255 | + </td> |
| 256 | + <td> |
| 257 | + <pre> |
| 258 | + const list = new List(); |
| 259 | + list.insertFirst('a'); |
| 260 | + list.insertFirst('b'); |
| 261 | + list.insertFirst('c'); |
| 262 | + list.getAt(1); // returns node with data 'b' |
| 263 | + </pre> |
| 264 | + </td> |
| 265 | + </tr> |
| 266 | + <tr> |
| 267 | + <td> |
| 268 | + removeAt |
| 269 | + </td> |
| 270 | + <td> |
| 271 | + (integer) |
| 272 | + </td> |
| 273 | + <td> |
| 274 | + - |
| 275 | + </td> |
| 276 | + <td> |
| 277 | + Removes node at the provided index |
| 278 | + </td> |
| 279 | + <td> |
| 280 | + <pre> |
| 281 | + const list = new List(); |
| 282 | + list.insertFirst('a'); |
| 283 | + list.insertFirst('b'); |
| 284 | + list.insertFirst('c'); |
| 285 | + list.removeAt(1); |
| 286 | + list.getAt(1); // returns node with data 'a' |
| 287 | + </pre> |
| 288 | + </td> |
| 289 | + </tr> |
| 290 | + <tr> |
| 291 | + <td> |
| 292 | + insertAt |
| 293 | + </td> |
| 294 | + <td> |
| 295 | + (Data, integer) |
| 296 | + </td> |
| 297 | + <td> |
| 298 | + - |
| 299 | + </td> |
| 300 | + <td> |
| 301 | + Create an insert a new node at provided index. |
| 302 | + If index is out of bounds, add the node to the end |
| 303 | + of the list. |
| 304 | + </td> |
| 305 | + <td> |
| 306 | + <pre> |
| 307 | + const list = new List(); |
| 308 | + list.insertFirst('a'); |
| 309 | + list.insertFirst('b'); |
| 310 | + list.insertFirst('c'); |
| 311 | + list.insertAt('Hi', 1) |
| 312 | + list.getAt(1); // returns node with data 'Hi' |
| 313 | + </pre> |
| 314 | + </td> |
| 315 | + </tr> |
| 316 | + <tr> |
| 317 | + <td> |
| 318 | + forEach |
| 319 | + </td> |
| 320 | + <td> |
| 321 | + (function) |
| 322 | + </td> |
| 323 | + <td> |
| 324 | + - |
| 325 | + </td> |
| 326 | + <td> |
| 327 | + Calls the provided function with every node of the chain and the index |
| 328 | + of the node. |
| 329 | + </td> |
| 330 | + <td> |
| 331 | + <pre> |
| 332 | + const list = new List(); |
| 333 | + |
| 334 | + list.insertLast(1); |
| 335 | + list.insertLast(2); |
| 336 | + list.insertLast(3); |
| 337 | + list.insertLast(4); |
| 338 | + |
| 339 | + list.forEach((node, index) => { |
| 340 | + node.data += 10; |
| 341 | + }); |
| 342 | + list.getAt(0); // Returns node with data '11' |
| 343 | + </pre> |
| 344 | + </td> |
| 345 | + </tr> |
| 346 | + <tr> |
| 347 | + <td> |
| 348 | + for...of Loop |
| 349 | + </td> |
| 350 | + <td> |
| 351 | + - |
| 352 | + </td> |
| 353 | + <td> |
| 354 | + - |
| 355 | + </td> |
| 356 | + <td> |
| 357 | + Linked list should be compatible as the subject of a 'for...of' loop |
| 358 | + </td> |
| 359 | + <td> |
| 360 | + <pre> |
| 361 | + const list = new List(); |
| 362 | + |
| 363 | + list.insertLast(1); |
| 364 | + list.insertLast(2); |
| 365 | + list.insertLast(3); |
| 366 | + list.insertLast(4); |
| 367 | + |
| 368 | + for (let node of list) { |
| 369 | + node.data += 10; |
| 370 | + } |
| 371 | + |
| 372 | + node.getAt(1); // returns node with data 11 |
| 373 | + </pre> |
| 374 | + </td> |
| 375 | + </tr> |
| 376 | + </tbody> |
| 377 | + </table> |
| 378 | +</body> |
0 commit comments