2

I'm trying to figure out the linked list data structure using Javascript. But there is a part that I'm not able to understand.

function LinkedList() {
  var Node = function(element) {
    this.element = element;
    this.next = null;
  }

  var length = 0;
  var head = null;

  this.append = function(element) {
    var node = new Node(element),
      current;

    if (head === null) {
      head = node;
    } else {
      current = head;

      //loop the list until find last item
      while (current.next) {
        current = current.next
      }

      //get last item and assign next to node to make the link
      current.next = node
    }
    length++;
  }

  this.removeAt = function(position) {
    //check for out of bounds values
    if (position > -1 && position < length) {
      var current = head,
        previous,
        index = 0;

      if (position === 0) {
        head = current.next;
      } else {
        while (index++ < position) {
          debugger;
          previous = current;
          current = current.next;
        }

        previous.next = current.next;
      }

      length--;

      return current.element;

    } else {
      return null;
    }
  }

  this.toString = function() {
    var current = head,
      string = '';

    while (current) {
      string = current.element;
      current = current.next;
    }

    return string;
  }
}

var list = new LinkedList();
list.append(15);
list.append(10);
list.append(11);
list.removeAt(1);

I don't understand how the variable current loses its reference to the node when you run the removeAt method.

4
  • 1
    When you say "... current loses its reference to the node..." are you asking how/why the node is deleted? Commented Jul 13, 2016 at 18:35
  • current is a variable local to your removeAt method, so what exactly is the problem you see? I checked your code, and it seems all OK, except maybe that the toString method only returns the content of the tail node. Could you add code that demonstrates the issue you see? Something that produces a result you did not expect or desire? Commented Jul 13, 2016 at 18:54
  • @nurdyguy yes that's the part I don't get Commented Jul 13, 2016 at 20:25
  • @marquezgon When you take the previous node and set his next to current.next using previous.next = current.next the result is that previous now links directly to the node after current. Thus you have removed current from the list. Then at the end of the function the current variable goes out of scope and is effectively deleted. Commented Jul 13, 2016 at 22:29

1 Answer 1

2

If you want to create the link list you can use the below algorithm:-

var linkedList = function(){
  this.head=null;
  this.tail=null;
  this.size=0;
}
linkedList.prototype.add=function(obj){
 if(!this.head){
   this.head=obj;
   this.tail=obj;
   this.size++;
   return
 }
 this.tail.next = obj;
 this.tail=obj;
 this.size++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.