Natedizzy Natedizzy - 5 months ago 14
Node.js Question

Double linked list with 5 Nodes

I'm having trouble pointing to the last node.

The output is suppose to look like this:

Linked List with 5 nodes


  1. Node Value: Head Node / Next Node Value: Second Node / Last Node Value:
    null

  2. Node Value: Second Node / Next Node Value: Third Node / Last Node Value: Head Node

  3. Node Value: Third Node / Next Node Value: Fourth Node / Last Node Value: Second Node

  4. Node Value: Fourth Node / Next Node Value: Tail Node / Last Node Value: Third Node

  5. Node Value: Tail Node / Next Node Value: undefined / Last Node Value: Fourth Node



But, I keep getting this:

Linked List with 5 nodes


  1. Node Value: Head Node / Next Node Value: Second Node / Last Node Value:
    undefined

  2. Node Value: Second Node / Next Node Value: Third Node / Last Node Value:
    undefined

  3. Node Value: Third Node / Next Node Value: Fourth Node / Last Node Value:
    undefined

  4. Node Value: Fourth Node / Next Node Value: Tail Node / Last Node Value:
    undefined

  5. Node Value: Tail Node / Next Node Value: undefined / Last Node Value:
    null





var DoubleLinkedList = function() {

this.head = 0;
this.tail = 0;
this.length = 5;

var LinkedListNode = function(content) {
this.next = 0;
this.last = [];
this.content = content;
};

this.add = function(content) {
if (this.head == 0) {
this.head = new LinkedListNode(content);
return this.head;
}
if (this.tail == 0) {
this.tail = new LinkedListNode(content);
this.head.next = this.tail;
return this.tail;
};
this.tail.next = new LinkedListNode(content);
this.tail = this.tail.next;
this.tail.next = 0;
return this.tail;
};
}

DoubleLinkedList.prototype.length = function() {
var i = 0;
var node = this.head;

while (node != 0) {
i++;
node = node.next;
}
return i;
};

DoubleLinkedList.prototype.toString = function() {
var i = 1;
var str = 'Linked List with ' + this.length + ' nodes <br/>';
var node = this.head;


while (node != 0) {
str += i + ': Node Value: ' + node.content;
str += ' / Next Node Value: ' + node.next.content;
str += " / Last Node Value: " + node.last;

if (node.next == 0) str += ' null';
if (node.next != 0) str += node.last.content;
i++;
str += "<br>";
node = node.next;
}
return str;
};

var lln = new DoubleLinkedList();

lln.add(' Head Node');
lln.add(' Second Node');
lln.add(' Third Node');
lln.add(' Fourth Node')
lln.add(' Tail Node');

document.getElementById('output').innerHTML = lln.toString();

<p id='output'></p>




Answer

var DoubleLinkedList = function() {

  this.head = 0;
  this.tail = 0;
  this.length = 5;

  var LinkedListNode = function(content) {
    this.next = 0;
    this.last = 0;
    this.content = content;
  };

  this.add = function(content) {
    if (this.head == 0) {
      this.head = new LinkedListNode(content);
      return this.head;
    }
    if (this.tail == 0) {
      this.tail = new LinkedListNode(content);
      this.head.next = this.tail;
      this.tail.last = this.head;
      return this.tail;
    };
    this.tail.next = new LinkedListNode(content);
    this.tail.next.last = this.tail;
    this.tail = this.tail.next;
    this.tail.next = 0;
    return this.tail;
  };
}

DoubleLinkedList.prototype.length = function() {
  var i = 0;
  var node = this.head;

  while (node != 0) {
    i++;
    node = node.next;
  }
  return i;
};

DoubleLinkedList.prototype.toString = function() {
  var i = 1;
  var str = 'Linked List with ' + this.length + ' nodes <br/>';
  var node = this.head;


  while (node != 0) {
    str += i + ': Node Value: ' + node.content;
    str += ' / Next Node Value: ' + node.next.content;
    str += " / Last Node Value: ";

    if (node.last == 0) str += ' null';
    else str += node.last.content;
    i++;
    str += "<br>";
    node = node.next;
  }
  return str;
};

var lln = new DoubleLinkedList();

lln.add(' Head Node');
lln.add(' Second Node');
lln.add(' Third Node');
lln.add(' Fourth Node')
lln.add(' Tail Node');

document.getElementById('output').innerHTML = lln.toString();
<p id='output'></p>

You didn't set the last values in the add method and made some mistakes in the toString method.

Comments