GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Data-Structures/linkedList.js at master · MadRiver44/Data-Structures Expand file tree/ Copy pathlinkedList.jsMore file actions 103 lines (90 loc) · 2.52 KB / Copy pathlinkedList.jsFile metadata and controls103 lines (90 loc) · 2.52 KB 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 // Linked List /* insert/ delete with reference to the node: O(1) */ /* insert/ delete eithout reference to node: O(n) */ /* search: O(n) */ function Node(value) { this.next = null; this.value = value; } function LinkedList(headValue) { if (headValue === undefined) console.log('Must provide a headValue for Node'); this.head = new Node(headValue); this.tail = this.head; } LinkedList.prototype.forEach = function(callback) { var node = this.head; while (node) { callback(node.value); node = node.next; } }; LinkedList.prototype.print = function() { var result = []; this.forEach(function(value) { result.push(value); }); return result.join(', '); }; LinkedList.prototype.insertAfter = function(node, value) { // get reference to former next var oldNext = node.next; // create a new node var newNext = new Node(value); // store it as the newNext node.next = newNext; // set next for the new Node to be the old next newNext.next = oldNext; // if reference node is tail, set tail to newNext; if (this.tail === node) this.tail = newNext; return newNext; }; LinkedList.prototype.removeAfter = function(node) { // store reference to removed node var removedNode = node.next; // if node is tail , then there is nothing to remove if (!removedNode) return 'Nothing to remove'; // get reference to node after removed node var newNext = removedNode.next; // set newNext as the next node node.next = newNext; // remove reference to removed node from linked list remodeNode.next = null; // if removed node is tails, set tail to node if (removedNode === this.tail) this.tail = node; return removedNode; }; LinkedList.prototype.insertHead = function(value) { var newHead = new Node(value); var oldHead = this.head; this.head = newHead; newHead.next = oldHead; return this.head; }; LinkedList.prototype.removeHead = function() { var oldHead = this.head; var newHead = oldHead.next; this.head = newHead; oldHead.next = null; return oldHead; }; LinkedList.prototype.findNode = function(value) { var node = this.head; while (node) { if (node.value === value) return node; node = node.next; } return 'No node with ' + value + ' found'; }; LinkedList.prototype.appendToTail = function(value) { var newTail = new Node(value); /* without myList.tail property O(n) var node - this.head; while (node.nwxt) { node = node.next; } node.next = newTail; */ // with myLiust.tail property O(1) this.tail.next = newTail; this.tail = newTail; return newTail; }; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |