Skip to content

Commit

Permalink
Merge pull request #1 from PachiSystems/feature/sortedAdd
Browse files Browse the repository at this point in the history
Changed this._last to be this._tail (in line with terminology for linked...
  • Loading branch information
PachiSystems committed Jul 22, 2013
2 parents 13f4555 + 579c99f commit 27d75cc
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 41 deletions.
69 changes: 58 additions & 11 deletions AMDLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* LinkedList for JavaScript.
* Eventually this will completely emulate a regular Java linked list.
* Copyright 2013, Brian Milton
* Version: 0.37.2 (18th July 2013)
* Version: 0.38.1 (22nd July 2013)
*/
define("LinkedList", function() {

Expand All @@ -11,7 +11,7 @@ define("LinkedList", function() {
function LinkedList() {
this._length = 0;
this._head = null;
this._last = null;
this._tail = null;
}

/*
Expand Down Expand Up @@ -60,14 +60,14 @@ define("LinkedList", function() {
if (this._head === null) {

this._head = node;
this._last = node;
this._tail = node;

} else {

// Add as the last item in the list.
current = this._last;
current = this._tail;
current.next = node;
this._last = node;
this._tail = node;

}

Expand Down Expand Up @@ -114,7 +114,7 @@ define("LinkedList", function() {
*/
LinkedList.prototype.clear = function() {
var head = this._head;
var tail = this._last;
var tail = this._tail;
current.data = null;
current.next = null;
tail.data = null;
Expand Down Expand Up @@ -193,7 +193,7 @@ define("LinkedList", function() {
* @return {element}
*/
LinkedList.prototype.getLast = function() {
return this._last.data;
return this._tail.data;
};

/*
Expand Down Expand Up @@ -293,7 +293,7 @@ define("LinkedList", function() {
* @return {element}
*/
LinkedList.prototype.peekLast = function() {
return this._last.data;
return this._tail.data;
};

/*
Expand Down Expand Up @@ -370,7 +370,7 @@ define("LinkedList", function() {

// Special case: Remove last item
if (index == this._length - 1){
this._last = previous;
this._tail = previous;
}
}

Expand Down Expand Up @@ -425,7 +425,7 @@ define("LinkedList", function() {
}
// Last reached.
previous.next = null;
this._last = previous;
this._tail = previous;
this._length--;
MODCOUNT++;
return current.data;
Expand Down Expand Up @@ -516,14 +516,61 @@ define("LinkedList", function() {
current = current.next;
} while (current !== null);
// End of the list reached.
current = this._last;
current = this._tail;
}
//console.log("Done in "+numops+" operations");
MODCOUNT++;
return true;
}
};

/*
* Adds an element to the list while maintaining a sorted order.
* @return {void}
*/
LinkedList.prototype.sortedAdd = function(element,sortFunction) {
var node = {data:element,next:null};
if(this._length === 0){
this._head = node;
this._tail = node;
MODCOUNT++;
this._length++;
return;
}
var current = this._head;
var direction = direction || "asc";
var previous = null;
var i=0;
while(i++ < this._length) {
if (sortFunction(current.data,node.data)) {

if(previous === null) {
// Special case: Insert at head.
node.next = this._head;
this._head = node;
MODCOUNT++;
this._length++;
return;
}

node.next = current;
previous.next = node;
MODCOUNT++;
this._length++;
return;
}
previous = current;
current = current.next;
}
// Didn't match any. Insert at tail.
current = this._tail;
current.next = node;
this._tail = node;
MODCOUNT++;
this._length++;
return;
}

/*
* Returns an array containing all of the elements in the list in proper sequence (from head to tail).
* @returns {object}
Expand Down
69 changes: 58 additions & 11 deletions LinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* LinkedList for JavaScript.
* Eventually this will completely emulate a regular Java linked list.
* Copyright 2013, Brian Milton
* Version: 0.37.2 (18th July 2013)
* Version: 0.38.1 (22nd July 2013)
*/
(function() {

Expand All @@ -11,7 +11,7 @@
function LinkedList() {
this._length = 0;
this._head = null;
this._last = null;
this._tail = null;
}

/*
Expand Down Expand Up @@ -60,14 +60,14 @@
if (this._head === null) {

this._head = node;
this._last = node;
this._tail = node;

} else {

// Add as the last item in the list.
current = this._last;
current = this._tail;
current.next = node;
this._last = node;
this._tail = node;

}

Expand Down Expand Up @@ -114,7 +114,7 @@
*/
LinkedList.prototype.clear = function() {
var head = this._head;
var tail = this._last;
var tail = this._tail;
current.data = null;
current.next = null;
tail.data = null;
Expand Down Expand Up @@ -193,7 +193,7 @@
* @return {element}
*/
LinkedList.prototype.getLast = function() {
return this._last.data;
return this._tail.data;
};

/*
Expand Down Expand Up @@ -293,7 +293,7 @@
* @return {element}
*/
LinkedList.prototype.peekLast = function() {
return this._last.data;
return this._tail.data;
};

/*
Expand Down Expand Up @@ -370,7 +370,7 @@

// Special case: Remove last item
if (index == this._length - 1){
this._last = previous;
this._tail = previous;
}
}

Expand Down Expand Up @@ -425,7 +425,7 @@
}
// Last reached.
previous.next = null;
this._last = previous;
this._tail = previous;
this._length--;
MODCOUNT++;
return current.data;
Expand Down Expand Up @@ -516,14 +516,61 @@
current = current.next;
} while (current !== null);
// End of the list reached.
current = this._last;
current = this._tail;
}
//console.log("Done in "+numops+" operations");
MODCOUNT++;
return true;
}
};

/*
* Adds an element to the list while maintaining a sorted order.
* @return {void}
*/
LinkedList.prototype.sortedAdd = function(element,sortFunction) {
var node = {data:element,next:null};
if(this._length === 0){
this._head = node;
this._tail = node;
MODCOUNT++;
this._length++;
return;
}
var current = this._head;
var direction = direction || "asc";
var previous = null;
var i=0;
while(i++ < this._length) {
if (sortFunction(current.data,node.data)) {

if(previous === null) {
// Special case: Insert at head.
node.next = this._head;
this._head = node;
MODCOUNT++;
this._length++;
return;
}

node.next = current;
previous.next = node;
MODCOUNT++;
this._length++;
return;
}
previous = current;
current = current.next;
}
// Didn't match any. Insert at tail.
current = this._tail;
current.next = node;
this._tail = node;
MODCOUNT++;
this._length++;
return;
}

/*
* Returns an array containing all of the elements in the list in proper sequence (from head to tail).
* @returns {object}
Expand Down
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ JSLinkedList

Implementation of LinkedList in JavaScript. Eventually will emulate JavaSE 7's LinkedList type.

## Current version: 0.37.2
## Current version: 0.38.1

Version 1 will be considered ready when all of the items of the Method Summary are implemented.

Expand All @@ -26,11 +26,6 @@ Enhanced features are frozen at this time. But please make a feature request to
<td>Constructs an empty list</td>
<td>0.37.0</td>
</tr>
<tr>
<td>LinkedList(Collection)</td>
<td>Constructs a list contianing Elements of the specified collection in the order they are returned by the collection's itterator.</td>
<td></td>
</tr>
</table>

## Method Summary
Expand Down Expand Up @@ -58,7 +53,7 @@ Checked items indicate working functionality. Unchecked items are not implemente
</tr>
<tr>
<td>bool</td>
<td>allAll(Collection)</td>
<td>addAll(Collection)</td>
<td>Appends all of the elmeents in the specified collection to the end of the list, in the order that they are returned by the specified collection's itterator.</td>
<td></td>
</tr>
Expand Down Expand Up @@ -98,12 +93,6 @@ Checked items indicate working functionality. Unchecked items are not implemente
<td>Returns true if the list contains the specified object.</td>
<td>0.37.0</td>
</tr>
<tr>
<td>Iterator</td>
<td>descendingIterator()</td>
<td>Returns an iterator over the Elements in the deque in reverse sequential order.</td>
<td></td>
</tr>
<tr>
<td>Element</td>
<td>Element()</td>
Expand Down Expand Up @@ -140,12 +129,6 @@ Checked items indicate working functionality. Unchecked items are not implemente
<td>Returns the index of the last occurance of the specified object in the list, or -1 if this list does not contain the object.</td>
<td>0.37.0</td>
</tr>
<tr>
<td>ListIterator</td>
<td>listIterator(index)</td>
<td>Returns a list-iterator of the Elements in the list (in proper sequence), starting at the specified position in the list.</td>
<td></td>
</tr>
<tr>
<td>bool</td>
<td>offer(Element)</td>
Expand Down Expand Up @@ -307,4 +290,10 @@ Checked items indicate working functionality. Unchecked items are not implemente
<td>Sorts the list based on the function passed in the same manner as JavaScript's Array.prototype.sort.</td>
<td>0.37.0</td>
</tr>
<tr>
<td>void</td>
<td>sortedAdd(Element,Function)</td>
<td>Adds an Element into the list in a sorted position based upon the function passed in the same manner as JavaScript's Array.prototype.sort.</td>
<td>0.38.0</td>
</tr>
</table>

0 comments on commit 27d75cc

Please sign in to comment.