Skip to content

Commit 5cc6aa1

Browse files
committedJun 24, 2014
added chapter 04
1 parent e265366 commit 5cc6aa1

8 files changed

+158
-0
lines changed
 

‎chapter04/01-Queue.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Queue.js"></script>
9+
</body>
10+
</html>

‎chapter04/01-Queue.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Queue() {
2+
3+
this.items = [];
4+
5+
this.enqueue = function(element){
6+
this.items.push(element);
7+
};
8+
9+
this.dequeue = function(){
10+
return this.items.shift();
11+
};
12+
13+
this.front = function(){
14+
return this.items[0];
15+
};
16+
17+
this.isEmpty = function(){
18+
return this.items.length == 0;
19+
};
20+
21+
this.size = function(){
22+
return this.items.length;
23+
};
24+
25+
this.print = function(){
26+
console.log(this.items.toString());
27+
};
28+
}

‎chapter04/02-UsingQueues.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Queue.js"></script>
9+
<script type="text/javascript" src="02-UsingQueues.js"></script>
10+
</body>
11+
</html>

‎chapter04/02-UsingQueues.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var queue = new Queue();
2+
console.log(queue.isEmpty()); //outputs true
3+
queue.enqueue("John");
4+
queue.enqueue("Jack");
5+
queue.print();
6+
queue.enqueue("Camila");
7+
queue.print();
8+
console.log(queue.size()); //outputs 3
9+
console.log(queue.isEmpty()); //outputs false
10+
queue.dequeue();
11+
queue.dequeue();
12+
queue.print();

‎chapter04/03-PriorityQueue.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="03-PriorityQueue.js"></script>
9+
</body>
10+
</html>

‎chapter04/03-PriorityQueue.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function PriorityQueue() {
2+
3+
this.items = [];
4+
5+
function QueueElement (element, priority){
6+
this.element = element;
7+
this.priority = priority;
8+
}
9+
10+
this.enqueue = function(element, priority){
11+
var queueElement = new QueueElement(element, priority);
12+
13+
if (this.isEmpty()){
14+
this.items.push(queueElement);
15+
} else {
16+
for (var i=0; i<this.items.length; i++){
17+
if (queueElement.priority < this.items[i].priority){
18+
this.items.splice(i,0,queueElement);
19+
break;
20+
}
21+
}
22+
}
23+
};
24+
25+
this.dequeue = function(){
26+
return this.items.shift();
27+
};
28+
29+
this.front = function(){
30+
return this.items[0];
31+
};
32+
33+
this.isEmpty = function(){
34+
return this.items.length == 0;
35+
};
36+
37+
this.size = function(){
38+
return this.items.length;
39+
};
40+
41+
this.print = function(){
42+
for (var i=0; i<this.items.length; i++){
43+
console.log(this.items[i].element + ' - ' + this.items[i].priority);
44+
}
45+
};
46+
}
47+
48+
var priorityQueue = new PriorityQueue();
49+
priorityQueue.enqueue("John", 2);
50+
priorityQueue.enqueue("Jack", 1);
51+
priorityQueue.enqueue("Camila", 1);
52+
priorityQueue.print();
53+

‎chapter04/04-HotPotato.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Queue.js"></script>
9+
<script type="text/javascript" src="04-HotPotato.js"></script>
10+
</body>
11+
</html>

‎chapter04/04-HotPotato.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function hotPotato (nameList, num){
2+
3+
var queue = new Queue();
4+
5+
for (var i=0; i<nameList.length; i++){
6+
queue.enqueue(nameList[i]);
7+
}
8+
9+
var eliminated = '';
10+
while (queue.size() > 1){
11+
for (var i=0; i<num; i++){
12+
queue.enqueue(queue.dequeue());
13+
}
14+
eliminated = queue.dequeue();
15+
console.log(eliminated + ' was eliminated from the Hot Potato game.');
16+
}
17+
18+
return queue.dequeue();
19+
}
20+
21+
var names = ['John','Jack','Camila','Ingrid','Carl'];
22+
var winner = hotPotato(names, 7);
23+
console.log('The winner is: ' + winner);

0 commit comments

Comments
 (0)