Code coverage report for lib/dataStructures/linkedList.js

Statements: 91.89% (34 / 37)      Branches: 81.82% (18 / 22)      Functions: 100% (4 / 4)      Lines: 100% (34 / 34)      Ignored: none     

All files » lib/dataStructures/ » linkedList.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561 491 491     1 159 159 159     1 491   491   491 165   326     491 491     1 142 142   142 142   142 105 105     142 142 77   142 114 114 60     142 5   142       1
var node = function(data) {
	this.next = null;
	this.data = data;
};
 
var linkedList = function() {
	this.start = null;
	this.end = null;
	this.length= 0;
};
 
linkedList.prototype.add = function(data) {
	Iif (data === undefined) { throw "data must be valid to add"; }
 
	var newNode = new node(data);
 
	if (this.start === null) {
		this.start = newNode
	} else {
		this.end.next = newNode;
	}
 
	this.length++;
	this.end = newNode;
};
 
linkedList.prototype.remove = function(data) {
	Iif (data === undefined) { throw "data must be valid to add"; }
	Iif (this.start === null) { return; }
 
	var previous = null;
	var current = this.start;
 
	while( (current !== null) && (current.data !== data )) {
		previous = current;
		current = current.next;
	}
 
	Eif ( current !== null ) {
		if (previous === null) {
			this.start = this.start.next;
		}
		if (current.next === null) {
		  this.end = previous;
		  if(this.end !== null) {
		    this.end.next = null;
		  }
		}
		if ((previous !== null) && (current.next !== null) ) {
			previous.next = current.next;
		}
		this.length--;
	}
};
 
module.exports = linkedList;