Linked List

by Felipe Alfaro

JavaScript

function Node (data) {
    this.data = data;
    this.next = null;
    this.previous = null;
}

Node.prototype = {toString: function() {return this.data;}}

function LinkedList (){
    this.count = 0;
    this.first = null;
    this.last = null;
}

LinkedList.prototype = {
    insert: function (node) {
      if (!this.count)
        this.first = this.last = node;
      else
      	this.last.next = this.last = node;
      this.count++;
    },
    
    
    move: function (num) {
    	var current;
        if (!num) return this.current;
        this.current = this.current.next;
        return this.move(num -1);
    },
    delete: function (num) {
        this.current = this.first;
        this.move(this.count - num).next = null;
        this.count -= num;
    },
    toArray: function () {
      var list = [],
      		current = this.first;
      for (var i = 0; i < this.count; i++, current = current.next)
        list.push(current.data);
      return list;
    }
};

var list = new LinkedList();

list.insert(new Node('first'));
list.insert(new Node('second'));
list.insert(new Node('third'));
list.insert(new Node('fourth'));

console.log(list.toArray());