Skip to content

Commit d0db4d7

Browse files
author
Nicholas C. Zakas
committed
Updated README
1 parent ebe08e5 commit d0db4d7

File tree

7 files changed

+525
-1
lines changed

7 files changed

+525
-1
lines changed

README

+30-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
Collection of classic computer science paradigms, algorithms, and approaches written in JavaScript. All of the code is available under an MIT License.
1+
Collection of classic computer science paradigms, algorithms, and approaches written in JavaScript. All of the code is available under an MIT License.
2+
3+
Each example has an associated blog post:
4+
5+
Base64 Encoding
6+
http://www.nczonline.net/blog/2009/12/08/computer-science-in-javascript-base64-encoding/
7+
8+
Binary Search
9+
http://www.nczonline.net/blog/2009/09/01/computer-science-in-javascript-binary-search/
10+
11+
Binary Search Tree
12+
http://www.nczonline.net/blog/2009/06/09/computer-science-in-javascript-binary-search-tree-part-1/
13+
http://www.nczonline.net/blog/2009/06/16/computer-science-in-javascript-binary-search-tree-part-2/
14+
15+
Bubble Sort
16+
http://www.nczonline.net/blog/2009/05/26/computer-science-in-javascript-bubble-sort/
17+
18+
Credit Card Number Validation
19+
http://www.nczonline.net/blog/2009/08/04/computer-science-in-javascript-credit-card-number-validation/
20+
21+
Doubly Linked List
22+
http://www.nczonline.net/blog/2009/04/21/computer-science-in-javascript-doubly-linked-lists/
23+
24+
Linked List
25+
http://www.nczonline.net/blog/2009/04/13/computer-science-in-javascript-linked-list/
26+
27+
Selection Sort
28+
http://www.nczonline.net/blog/2009/09/08/computer-science-in-javascript-selection-sort/
29+
30+
Please note: Since this is the repository that goes along with my blog post series, only pull requests for bugs are accepted.

data-structures/DequeExample.htm

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<title>Deque Example</title>
6+
<script language="javascript" type="text/javascript" src="doublelinkedlist.js"></script>
7+
<script language="javascript" type="text/javascript" src="deque.js"></script>
8+
</head>
9+
<body>
10+
<h2>Deque Example</h2>
11+
<hr />
12+
<script>
13+
14+
//test creating the Deque
15+
var oDeque = new Deque;
16+
17+
//test the putFront() method
18+
oDeque.putFront("red");
19+
oDeque.putFront("orange");
20+
21+
//test the toString() method
22+
document.write("<p><b>Original Deque: </b><br />" + oDeque + "</p>");
23+
24+
//test the putBack() method
25+
oDeque.putBack("yellow");
26+
document.write("<p><b>Deque after adding to back: </b><br />" + oDeque + "</p>");
27+
28+
//test the getFront() method
29+
var vItem = oDeque.getFront();
30+
document.write("<p><b>Deque after getting first item: </b><br />" + oDeque + "<br /><b>Removed Item from Front: </b><br />" + vItem + "</p>");
31+
32+
//test the getBack() method
33+
var vItem = oDeque.getBack();
34+
document.write("<p><b>Deque after getting last item: </b><br />" + oDeque + "<br /><b>Removed Item from Back: </b><br />" + vItem + "</p>");
35+
36+
//test the isEmpty() method
37+
document.write("<p><b>Is Deque empty?: </b><br />" + oDeque.isEmpty() + "</p>");
38+
39+
//test the getFront() method
40+
var vItem3 = oDeque.getFront();
41+
document.write("<p><b>Deque after getting first item: </b><br />" + oDeque + "<br /><b>Removed Item from Front: </b><br />" + vItem3 + "</p>");
42+
43+
//test the isEmpty() method
44+
document.write("<p><b>Is Deque empty?: </b><br />" + oDeque.isEmpty() + "</p>");
45+
46+
</script>
47+
</body>
48+
</html>

data-structures/QueueExample.htm

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<title>Queue Example</title>
6+
<script language="javascript" type="text/javascript" src="linkedlist.js"></script>
7+
<script language="javascript" type="text/javascript" src="queue.js"></script>
8+
</head>
9+
<body>
10+
<h2>Queue Example</h2>
11+
<hr />
12+
<script>
13+
14+
//test creating the queue
15+
var oQueue = new Queue;
16+
17+
//test the put() method
18+
oQueue.put("red");
19+
oQueue.put("orange");
20+
oQueue.put("yellow");
21+
22+
//test the toString() method
23+
document.write("<p><b>Original Queue: </b><br />" + oQueue + "</p>");
24+
25+
//test the get() method
26+
var vItem = oQueue.get();
27+
document.write("<p><b>Queue after getting first item: </b><br />" + oQueue + "<br /><b>Removed Item: </b><br />" + vItem + "</p>");
28+
29+
//test the get() method
30+
var vItem2 = oQueue.get();
31+
document.write("<p><b>Queue after getting first item: </b><br />" + oQueue + "<br /><b>Removed Item: </b><br />" + vItem2 + "</p>");
32+
33+
//test the isEmpty() method
34+
document.write("<p><b>Is Queue empty?: </b><br />" + oQueue.isEmpty() + "</p>");
35+
36+
//test the get() method
37+
var vItem3 = oQueue.get();
38+
document.write("<p><b>Queue after getting first item: </b><br />" + oQueue + "<br /><b>Removed Item: </b><br />" + vItem3 + "</p>");
39+
40+
//test the isEmpty() method
41+
document.write("<p><b>Is Queue empty?: </b><br />" + oQueue.isEmpty() + "</p>");
42+
43+
</script>
44+
</body>
45+
</html>

data-structures/StackExample.htm

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<title>Stack Example</title>
6+
<script language="javascript" type="text/javascript" src="linkedlist.js"></script>
7+
<script language="javascript" type="text/javascript" src="stack.js"></script>
8+
</head>
9+
<body>
10+
<h2>Stack Example</h2>
11+
<hr />
12+
<script>
13+
14+
//test creating the stack
15+
var oStack = new Stack;
16+
17+
//test the push() method
18+
oStack.push("red");
19+
oStack.push("orange");
20+
oStack.push("yellow");
21+
22+
//test the toString() method
23+
document.write("<p><b>Original Stack: </b><br />" + oStack + "</p>");
24+
25+
//test the pop() method
26+
var vItem = oStack.pop();
27+
document.write("<p><b>Stack after popping: </b><br />" + oStack + "<br /><b>Popped Item: </b><br />" + vItem + "</p>");
28+
29+
//test the isEmpty() method
30+
document.write("<p><b>Is Stack empty?: </b><br />" + oStack.isEmpty() + "</p>");
31+
oStack.pop();
32+
oStack.pop();
33+
document.write("<p><b>Stack after popping two more items: </b><br />" + oStack + "</p>");
34+
document.write("<p><b>Is Stack empty?: </b><br />" + oStack.isEmpty() + "</p>");
35+
36+
</script>
37+
</body>
38+
</html>

data-structures/deque.js

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//-----------------------------------------------------------------
2+
// Class Deque
3+
//-----------------------------------------------------------------
4+
// Author(s)
5+
// Nicholas C. Zakas (NCZ), 9/5/02
6+
//
7+
// Description
8+
// A classic Deque interface.
9+
//-----------------------------------------------------------------
10+
function Deque() {
11+
this.items = new DoubleLinkedList
12+
}
13+
14+
//-----------------------------------------------------------------
15+
// Method Deque.getBack()
16+
//-----------------------------------------------------------------
17+
// Author(s)
18+
// Nicholas C. Zakas (NCZ), 9/5/02
19+
//
20+
// Description
21+
// This method gets off the back item in the Deque and returns it.
22+
//
23+
// Arguments
24+
// (none)
25+
//
26+
// Returns
27+
// The back item.
28+
//-----------------------------------------------------------------
29+
Deque.prototype.getBack = function() {
30+
31+
var vItem = null;
32+
33+
if (!this.isEmpty()) {
34+
35+
vItem = this.items.item(this.items.length - 1);
36+
37+
this.items.remove(this.items.length - 1);
38+
}
39+
40+
return vItem;
41+
}
42+
43+
44+
//-----------------------------------------------------------------
45+
// Method Deque.getFront()
46+
//-----------------------------------------------------------------
47+
// Author(s)
48+
// Nicholas C. Zakas (NCZ), 9/5/02
49+
//
50+
// Description
51+
// This method gets the first item in the Deque and returns it.
52+
//
53+
// Arguments
54+
// (none)
55+
//
56+
// Returns
57+
// The first item.
58+
//-----------------------------------------------------------------
59+
Deque.prototype.getFront = function() {
60+
61+
var vItem = null;
62+
63+
if (!this.isEmpty()) {
64+
65+
vItem = this.items.item(0);
66+
67+
this.items.remove(0);
68+
}
69+
70+
return vItem;
71+
}
72+
73+
//-----------------------------------------------------------------
74+
// Method Deque.isEmpty()
75+
//-----------------------------------------------------------------
76+
// Author(s)
77+
// Nicholas C. Zakas (NCZ), 9/5/02
78+
//
79+
// Description
80+
// This method determines if the Deque is empty.
81+
//
82+
// Arguments
83+
// (none)
84+
//
85+
// Returns
86+
// True if the Deque is empty, false if not.
87+
//-----------------------------------------------------------------
88+
Deque.prototype.isEmpty = function() {
89+
return this.items.length == 0;
90+
}
91+
92+
//-----------------------------------------------------------------
93+
// Method Deque.putBack()
94+
//-----------------------------------------------------------------
95+
// Author(s)
96+
// Nicholas C. Zakas (NCZ), 9/5/02
97+
//
98+
// Description
99+
// This method puts the given argument at the back of the Deque.
100+
//
101+
// Arguments
102+
// vItem (variant) - the object to put into the Deque.
103+
//
104+
// Returns
105+
// (nothing)
106+
//-----------------------------------------------------------------
107+
Deque.prototype.putBack = function (vItem) {
108+
this.items.add(vItem);
109+
return vItem;
110+
}
111+
112+
//-----------------------------------------------------------------
113+
// Method Deque.putFront()
114+
//-----------------------------------------------------------------
115+
// Author(s)
116+
// Nicholas C. Zakas (NCZ), 9/5/02
117+
//
118+
// Description
119+
// This method puts the given argument at the front of the Deque.
120+
//
121+
// Arguments
122+
// oElement (Object) - the object to put into the Deque.
123+
//
124+
// Returns
125+
// (nothing)
126+
//-----------------------------------------------------------------
127+
Deque.prototype.putFront = function (vData) {
128+
var oNode = new DoubleLinkedListNode(vData);
129+
130+
if (this.isEmpty()) {
131+
this.items.__first__ = oNode;
132+
this.items.__last__ = oNode;
133+
} else {
134+
var oPtr = this.items.__first__;
135+
this.items.__first__ = oNode;
136+
oNode.next = oPtr;
137+
oPtr.prev = oNode;
138+
}
139+
140+
this.items.length++;
141+
}
142+
143+
//-----------------------------------------------------------------
144+
// Method Deque.toString()
145+
//-----------------------------------------------------------------
146+
// Author(s)
147+
// Nicholas C. Zakas (NCZ), 9/5/02
148+
//
149+
// Description
150+
// This method returns the contents of the Deque as a string.
151+
//
152+
// Arguments
153+
// (none)
154+
//
155+
// Returns
156+
// A String representing the contents of the Deque.
157+
//-----------------------------------------------------------------
158+
Deque.prototype.toString = function() {
159+
return this.items.toString();
160+
}

0 commit comments

Comments
 (0)