UNPKG

ds-algo-study

Version:

Just experimenting with publishing a package

163 lines (150 loc) 8.86 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="./prism.css"> <script async defer src="./prism.js"></script> </head> <body>; <h1 id="linked-lists">Linked Lists</h1> <h3 id="projected-time">Projected Time</h3> <p>About 1 hour 20mins</p> <h3 id="prerequisites">Prerequisites</h3> <ul> <li><a href="/data-structures/intro-to-data-structures.md">Data Structures Overview</a></li> <li><a href="/javascript/javascript-2-arrays-functions.md">JavaScript Arrays, Functions</a></li> <li><a href="/javascript/javascript-7-oop.md">JavaScript Objects</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain">Prototypical Inheritance</a></li> </ul> <h3 id="motivation">Motivation</h3> <p>Though you will rarely (if ever) be asked to implement a data structure from scratch, having solid knowledge and understanding of the various data structures and ideal use cases can help you ace a technical interview (and get into your dream tech job).</p> <h3 id="objectives">Objectives</h3> <p><strong>Participants will be able to:</strong></p> <ul> <li>Implement various types of linked lists.</li> <li>Understand which portions of linked lists are already implemented in JavaScript.</li> <li>Implement their own linked lists under the correct circumstances.</li> </ul> <h3 id="specific-things-to-learn">Specific Things To Learn</h3> <ul> <li>What is a linked list</li> <li>What are the basic characteristics of a linked list <ul> <li>Why would a linked list be used instead of an array?</li> <li>What other data structures are similar to linked lists?</li> <li>What is the difference between a singly-linked list and a doubly linked list</li> <li>Why would a singly linked list be used instead of a doubly linked list?</li> </ul></li> <li>How to recognize linked lists when you see them</li> <li>How to know when to use linked lists to solve a problem.</li> <li>Linked lists in different versions of JavaScript.</li> </ul> <h3 id="lesson">Lesson</h3> <p>Create a file named “node.js” and create a Node class like the one below but give each Node a ‘text’ attribute.</p> <p><code> // Declare a Node() function that we will use to instantiate new Node objects. function Node(data) { this.data = data; this.next = null; }</p> <p>// Declare a SinglyLinkedList() function that we will use as a basis for our singly-linked list. function SinglyLinkedList() { this._length = 0; this.head = null; }</p> <p>// Use JavaScript prototyping to give the SinglyLinkedList class new public methods. SinglyLinkedList.prototype.add = function(value) { let node = new Node(value), currentNode = this.head;</p> <pre><code>// If the list is empty (has no head value) if (!currentNode) { this.head = node; this._length++; return node; } // Loop over all nodes that have a value in their &quot;next&quot; property. // This loop ends when it reaches a node that has no value in the &quot;next&quot; property. // We use this to determine the &quot;last&quot; node of the singly linked list. while (currentNode.next) { currentNode = currentNode.next; } // We can now add our node to the end of the list by storing it in the &quot;next&quot; of the node we determined was last in the list. currentNode.next = node; // We need to increment the length of the list now that we&#39;ve added a new node. this._length++; return node;</code></pre> <p>};</p> <p>SinglyLinkedList.prototype.findByPosition = function(position) { let currentNode = this.head, length = this._length, count = 1, message = {failure: ‘Failure: non-existent node in this list.’};</p> <pre><code>// Catch the possibility that a position that doesn&#39;t exist was provided. if (length === 0 || position &lt; 1 || position &gt; length) { throw new Error(message.failure); } // Loop over all nodes until the node before the desired position while (count &lt; position) { // Pull the &quot;next&quot; node object from the node based on the count currentNode = currentNode.next; count++; } // Because our loop stopped at the position before, our &quot;currentNode&quot; value is correctly set. return currentNode;</code></pre> <p>};</p> <p>SinglyLinkedList.prototype.remove = function(position) { let currentNode = this.head, length = this._length, count = 0, message = {failure: ‘Failure: non-existent node in this list.’}, beforeNodeToDelete = null, nodeToDelete = null, deletedNode = null;</p> <pre><code>// Catch the possibility that a position that doesn&#39;t exist was provided. if (position &lt; 0 || position &gt; length) { throw new Error(message.failure); } // Only run when the first node is being removed. if (position === 1) { this.head = currentNode.next; deletedNode = currentNode; currentNode = null; this._length--; return deletedNode; } // Remaining logic that is run when any node is being removed. while (count &lt; position) { beforeNodeToDelete = currentNode; nodeToDelete = currentNode.next; count++; } beforeNodeToDelete.next = nodeToDelete.next; deletedNode = nodeToDelete; nodeToDelete = null; this._length--; return deletedNode;</code></pre> <p>}; </code></p> <hr /> <p><em>Make sure to mention these things:</em></p> <ul> <li>Common data structures in interviews (hash tables, binary search trees, etc.)</li> <li>Most blockchains are built on some implementation of the Merkle tree data structure patented by Ralph Merkle (check out his site -&gt; <a href="http://merkle.com/">merkle.com</a> for more info if you’re into cryptography) <ul> <li>Different ways of applying and/or updating attributes</li> </ul></li> <li>Constructors</li> <li>Different ways of applying attributes</li> <li>How to define methods on a class in ES6</li> <li>Traversing a LinkedList</li> <li>How to remove Nodes</li> </ul> <h3 id="common-mistakes-misconceptions">Common Mistakes / Misconceptions</h3> <p>While traversing a singly-linked list, it is imperative that you stop BEFORE the actual node that you want to remove, as there is no going backwards to the “previous” node.</p> <p>Adding/removing items is usually faster than more complex data structures.</p> <p>Searching/iteration can be slower/cumbersome since every node only references the “next” node in the list.</p> <p>The DOM is a kind of Linked List. Our HTML elements are contained within parent elements and there is a last and first element to every HTML document.</p> <p>Other (tradeoffs when using linked lists)[https://en.wikipedia.org/wiki/Linked_list#Tradeoffs] as detailed by Wikipedia.</p> <h3 id="guided-practice">Guided Practice</h3> <p>Create a method to add a node to the end of the Linked List and a method to delete a node with the text attribute matching the given string.</p> <h3 id="independent-practice">Independent Practice</h3> <p>Create a method to add a new node after the node with the text attribute matching the given string.</p> <h3 id="challenge">Challenge</h3> <p>See <a href="../testing-and-tdd/testing-and-tdd.md">Testing and TDD</a> for a refresher on how to use Mocha and Chai to write tests.</p> <p>Create a file called “LinkedList_test.js” and write tests for each of your methods using Mocha and Chai be sure to include: <code> const LinkedList = require(‘./linkedlist.js’); </code></p> <h3 id="check-for-understanding">Check for Understanding</h3> <p>Form small groups in the cohort and discuss:</p> <ul> <li>Summarize what you have learnt about linked lists. What are the basic features of linked lists?</li> <li>What are some of the common misconceptions in using linked lists?</li> <li>Draw single, double, multiple, and circular linked list diagrams, and compare with a fellow group member.</li> </ul> <p>(http://blog.millermedeiros.com/linked-lists/)</p> <h3 id="supplemental-materials">Supplemental Materials</h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">MDN web docs | Classes</a></li> <li><a href="https://www.thatjsdude.com/interview/linkedList.html#singlyLinkedList">THat JS Dude | JS: Interview Questions part -4: Stack, Queue, Linked List</a></li> <li><a href="https://www.geeksforgeeks.org/linked-list-set-1-introduction/">Geeks for Geeks | Linked List | Set 1 (Introduction)</a></li> <li><a href="https://gist.github.com/klugjo/a9e9ef98fe879bc2b19b5a2e5947204c">ES6 Implementation</a></li> <li><a href="https://medium.com/dailyjs/instantiation-patterns-in-javascript-8fdcf69e8f9b">Instantiation Patterns in JavaScript</a></li> </ul> </body></html>