title: Doubly Linked List in javascript
body: <p>I was building linked list in javascript.
  I dont understand one part.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function Node(element) {
   this.element = element;
   this.next = null;
   this.previous = null;
  }
  
  function LList() {
   this.head = new Node("head");
   
   this.find = find;
   this.findLast = findLast;
  
   this.remove = remove;
   this.insert = insert;
   this.display = display;
   this.dispReverse = dispReverse;
    
  }
  
  function find(item) {
   var currNode = this.head;
   while(currNode.element != item) {
    currNode = currNode.next;
   }
  
   return currNode;
  }
  
  
  function display(list) {
   var currNode = this.head.next;
   while (currNode != null) {
    console.log(currNode.element);
    currNode = currNode.next;
   }
  }
  
  
  function insert(newElement, item) {
   var newNode = new Node(newElement);
   var current = this.find(item);
   newNode.next = current.next;
   newNode.previous = current;
   current.next = newNode;
  
  
   // Why I dont need this part?
      // Since new node got inserted, on my thoughts,
      // the next node of the current node should point the new node as a previous one
      // current.next.previous = newNode;
   
  }
  
  function remove(item) {
   var currNode = this.find(item);
   if (currNode.next != null) {
    currNode.previous.next = currNode.next;
    currNode.next.previous = currNode.previous;
    currNode.next = null;
    currNode.previous = null;
   }
  }
  
  function findLast() {
   var currNode = this.head;
   while (currNode.next != null) {
    currNode = currNode.next;
   }
  
   return currNode;
  }
  
  function dispReverse() {
  
   var currNode = this.head;
   currNode = this.findLast();
  
   while(currNode.previous != null) {
    console.log(currNode.element);
    currNode = currNode.previous;
   }
  }
  
  var cities = new LList(); 
  cities.insert("Conway", "head"); 
  cities.insert("Russellville", "Conway"); 
  cities.insert("Carlisle", "Russellville"); 
  cities.insert("Alma", "Carlisle"); 
  cities.display();
  
  cities.remove("Carlisle");
  cities.display();
  cities.dispReverse();
  
  
  /*
  Output should look like this: 
  
  Conway
  Russellville
  Carlisle
  Alma
  
  Conway
  Russellville
  Alma
  
  Alma
  Russellville
  Conway
  */</code></pre>
  </div>
  </div>
  </p>
  
  <p>Problem is the insert function!
  <br>Let's say if I have A B C node already.
  <br>And I want to insert K after B.
  <br>
  <br>Currently, the next and previous of B are C and A for each.
  <br>The previous element of C is B.
  <br></p>
  
  <p><br>Once I put K after B,
  <br>A B K C
  <br>(1) the next element of K will be C
  <br>(2) the previous element of K will be B.
  <br>(3) The next element of B is K
  <br>(4) previous element of C is K.
  <br>
  <br>On the code I wrote in Insert function, each line of the codes below should process the upper statements.
  <br>(1) newNode.next = current.next;
  <br>(2) newNode.previous = current;
  <br>(3) current.next = newNode;
  <br>(4) current.next.previous = newNode;
  <br>
  <br>But when I run this whole code including (4), error has occurred.
  <br>I don understand why...
  <br>Without the (4) line of codes, it works.
  <br>
  <br>Is there anyone who can help me understand this?</p>