jsos
college code for operating system fundamentals in js
git clone https://9o.is/git/jsos.git
queue.js
(943B)
1 /* ------------
2 Queue.js
3
4 A simple Queue, which is really just a dressed-up Javascript Array.
5 See the Javascript Array documentation at http://www.w3schools.com/jsref/jsref_obj_array.asp .
6 Look at the push and shift methods, as they are the least obvious here.
7
8 ------------ */
9
10 function Queue() {
11 this.q = new Array();
12
13 this.getSize = function(){
14 return this.q.length;
15 }
16
17 this.isEmpty = function() {
18 return (this.q.length == 0);
19 }
20
21 this.isNonEmpty = function() {
22 return (this.q.length > 0);
23 }
24
25 this.enqueue = function(element) {
26 this.q.push(element);
27 }
28
29 this.dequeue = function() {
30 var retVal = null;
31 if (this.q.length > 0) {
32 retVal = this.q.shift();
33 }
34 return retVal;
35 }
36
37 this.toString = function() {
38 retVal = "";
39 for (i in this.q) {
40 retVal += "[" + this.q[i] + "] ";
41 }
42 return retVal;
43 }
44 }