-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicholas C. Zakas
committed
Jun 14, 2011
1 parent
ebe08e5
commit d0db4d7
Showing
7 changed files
with
525 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
Collection of classic computer science paradigms, algorithms, and approaches written in JavaScript. All of the code is available under an MIT License. | ||
Collection of classic computer science paradigms, algorithms, and approaches written in JavaScript. All of the code is available under an MIT License. | ||
|
||
Each example has an associated blog post: | ||
|
||
Base64 Encoding | ||
http://www.nczonline.net/blog/2009/12/08/computer-science-in-javascript-base64-encoding/ | ||
|
||
Binary Search | ||
http://www.nczonline.net/blog/2009/09/01/computer-science-in-javascript-binary-search/ | ||
|
||
Binary Search Tree | ||
http://www.nczonline.net/blog/2009/06/09/computer-science-in-javascript-binary-search-tree-part-1/ | ||
http://www.nczonline.net/blog/2009/06/16/computer-science-in-javascript-binary-search-tree-part-2/ | ||
|
||
Bubble Sort | ||
http://www.nczonline.net/blog/2009/05/26/computer-science-in-javascript-bubble-sort/ | ||
|
||
Credit Card Number Validation | ||
http://www.nczonline.net/blog/2009/08/04/computer-science-in-javascript-credit-card-number-validation/ | ||
|
||
Doubly Linked List | ||
http://www.nczonline.net/blog/2009/04/21/computer-science-in-javascript-doubly-linked-lists/ | ||
|
||
Linked List | ||
http://www.nczonline.net/blog/2009/04/13/computer-science-in-javascript-linked-list/ | ||
|
||
Selection Sort | ||
http://www.nczonline.net/blog/2009/09/08/computer-science-in-javascript-selection-sort/ | ||
|
||
Please note: Since this is the repository that goes along with my blog post series, only pull requests for bugs are accepted. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
|
||
<html> | ||
<head> | ||
<title>Deque Example</title> | ||
<script language="javascript" type="text/javascript" src="doublelinkedlist.js"></script> | ||
<script language="javascript" type="text/javascript" src="deque.js"></script> | ||
</head> | ||
<body> | ||
<h2>Deque Example</h2> | ||
<hr /> | ||
<script> | ||
|
||
//test creating the Deque | ||
var oDeque = new Deque; | ||
|
||
//test the putFront() method | ||
oDeque.putFront("red"); | ||
oDeque.putFront("orange"); | ||
|
||
//test the toString() method | ||
document.write("<p><b>Original Deque: </b><br />" + oDeque + "</p>"); | ||
|
||
//test the putBack() method | ||
oDeque.putBack("yellow"); | ||
document.write("<p><b>Deque after adding to back: </b><br />" + oDeque + "</p>"); | ||
|
||
//test the getFront() method | ||
var vItem = oDeque.getFront(); | ||
document.write("<p><b>Deque after getting first item: </b><br />" + oDeque + "<br /><b>Removed Item from Front: </b><br />" + vItem + "</p>"); | ||
|
||
//test the getBack() method | ||
var vItem = oDeque.getBack(); | ||
document.write("<p><b>Deque after getting last item: </b><br />" + oDeque + "<br /><b>Removed Item from Back: </b><br />" + vItem + "</p>"); | ||
|
||
//test the isEmpty() method | ||
document.write("<p><b>Is Deque empty?: </b><br />" + oDeque.isEmpty() + "</p>"); | ||
|
||
//test the getFront() method | ||
var vItem3 = oDeque.getFront(); | ||
document.write("<p><b>Deque after getting first item: </b><br />" + oDeque + "<br /><b>Removed Item from Front: </b><br />" + vItem3 + "</p>"); | ||
|
||
//test the isEmpty() method | ||
document.write("<p><b>Is Deque empty?: </b><br />" + oDeque.isEmpty() + "</p>"); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
|
||
<html> | ||
<head> | ||
<title>Queue Example</title> | ||
<script language="javascript" type="text/javascript" src="linkedlist.js"></script> | ||
<script language="javascript" type="text/javascript" src="queue.js"></script> | ||
</head> | ||
<body> | ||
<h2>Queue Example</h2> | ||
<hr /> | ||
<script> | ||
|
||
//test creating the queue | ||
var oQueue = new Queue; | ||
|
||
//test the put() method | ||
oQueue.put("red"); | ||
oQueue.put("orange"); | ||
oQueue.put("yellow"); | ||
|
||
//test the toString() method | ||
document.write("<p><b>Original Queue: </b><br />" + oQueue + "</p>"); | ||
|
||
//test the get() method | ||
var vItem = oQueue.get(); | ||
document.write("<p><b>Queue after getting first item: </b><br />" + oQueue + "<br /><b>Removed Item: </b><br />" + vItem + "</p>"); | ||
|
||
//test the get() method | ||
var vItem2 = oQueue.get(); | ||
document.write("<p><b>Queue after getting first item: </b><br />" + oQueue + "<br /><b>Removed Item: </b><br />" + vItem2 + "</p>"); | ||
|
||
//test the isEmpty() method | ||
document.write("<p><b>Is Queue empty?: </b><br />" + oQueue.isEmpty() + "</p>"); | ||
|
||
//test the get() method | ||
var vItem3 = oQueue.get(); | ||
document.write("<p><b>Queue after getting first item: </b><br />" + oQueue + "<br /><b>Removed Item: </b><br />" + vItem3 + "</p>"); | ||
|
||
//test the isEmpty() method | ||
document.write("<p><b>Is Queue empty?: </b><br />" + oQueue.isEmpty() + "</p>"); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | ||
|
||
<html> | ||
<head> | ||
<title>Stack Example</title> | ||
<script language="javascript" type="text/javascript" src="linkedlist.js"></script> | ||
<script language="javascript" type="text/javascript" src="stack.js"></script> | ||
</head> | ||
<body> | ||
<h2>Stack Example</h2> | ||
<hr /> | ||
<script> | ||
|
||
//test creating the stack | ||
var oStack = new Stack; | ||
|
||
//test the push() method | ||
oStack.push("red"); | ||
oStack.push("orange"); | ||
oStack.push("yellow"); | ||
|
||
//test the toString() method | ||
document.write("<p><b>Original Stack: </b><br />" + oStack + "</p>"); | ||
|
||
//test the pop() method | ||
var vItem = oStack.pop(); | ||
document.write("<p><b>Stack after popping: </b><br />" + oStack + "<br /><b>Popped Item: </b><br />" + vItem + "</p>"); | ||
|
||
//test the isEmpty() method | ||
document.write("<p><b>Is Stack empty?: </b><br />" + oStack.isEmpty() + "</p>"); | ||
oStack.pop(); | ||
oStack.pop(); | ||
document.write("<p><b>Stack after popping two more items: </b><br />" + oStack + "</p>"); | ||
document.write("<p><b>Is Stack empty?: </b><br />" + oStack.isEmpty() + "</p>"); | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
//----------------------------------------------------------------- | ||
// Class Deque | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// A classic Deque interface. | ||
//----------------------------------------------------------------- | ||
function Deque() { | ||
this.items = new DoubleLinkedList | ||
} | ||
|
||
//----------------------------------------------------------------- | ||
// Method Deque.getBack() | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// This method gets off the back item in the Deque and returns it. | ||
// | ||
// Arguments | ||
// (none) | ||
// | ||
// Returns | ||
// The back item. | ||
//----------------------------------------------------------------- | ||
Deque.prototype.getBack = function() { | ||
|
||
var vItem = null; | ||
|
||
if (!this.isEmpty()) { | ||
|
||
vItem = this.items.item(this.items.length - 1); | ||
|
||
this.items.remove(this.items.length - 1); | ||
} | ||
|
||
return vItem; | ||
} | ||
|
||
|
||
//----------------------------------------------------------------- | ||
// Method Deque.getFront() | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// This method gets the first item in the Deque and returns it. | ||
// | ||
// Arguments | ||
// (none) | ||
// | ||
// Returns | ||
// The first item. | ||
//----------------------------------------------------------------- | ||
Deque.prototype.getFront = function() { | ||
|
||
var vItem = null; | ||
|
||
if (!this.isEmpty()) { | ||
|
||
vItem = this.items.item(0); | ||
|
||
this.items.remove(0); | ||
} | ||
|
||
return vItem; | ||
} | ||
|
||
//----------------------------------------------------------------- | ||
// Method Deque.isEmpty() | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// This method determines if the Deque is empty. | ||
// | ||
// Arguments | ||
// (none) | ||
// | ||
// Returns | ||
// True if the Deque is empty, false if not. | ||
//----------------------------------------------------------------- | ||
Deque.prototype.isEmpty = function() { | ||
return this.items.length == 0; | ||
} | ||
|
||
//----------------------------------------------------------------- | ||
// Method Deque.putBack() | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// This method puts the given argument at the back of the Deque. | ||
// | ||
// Arguments | ||
// vItem (variant) - the object to put into the Deque. | ||
// | ||
// Returns | ||
// (nothing) | ||
//----------------------------------------------------------------- | ||
Deque.prototype.putBack = function (vItem) { | ||
this.items.add(vItem); | ||
return vItem; | ||
} | ||
|
||
//----------------------------------------------------------------- | ||
// Method Deque.putFront() | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// This method puts the given argument at the front of the Deque. | ||
// | ||
// Arguments | ||
// oElement (Object) - the object to put into the Deque. | ||
// | ||
// Returns | ||
// (nothing) | ||
//----------------------------------------------------------------- | ||
Deque.prototype.putFront = function (vData) { | ||
var oNode = new DoubleLinkedListNode(vData); | ||
|
||
if (this.isEmpty()) { | ||
this.items.__first__ = oNode; | ||
this.items.__last__ = oNode; | ||
} else { | ||
var oPtr = this.items.__first__; | ||
this.items.__first__ = oNode; | ||
oNode.next = oPtr; | ||
oPtr.prev = oNode; | ||
} | ||
|
||
this.items.length++; | ||
} | ||
|
||
//----------------------------------------------------------------- | ||
// Method Deque.toString() | ||
//----------------------------------------------------------------- | ||
// Author(s) | ||
// Nicholas C. Zakas (NCZ), 9/5/02 | ||
// | ||
// Description | ||
// This method returns the contents of the Deque as a string. | ||
// | ||
// Arguments | ||
// (none) | ||
// | ||
// Returns | ||
// A String representing the contents of the Deque. | ||
//----------------------------------------------------------------- | ||
Deque.prototype.toString = function() { | ||
return this.items.toString(); | ||
} |
Oops, something went wrong.