-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadt-queue.js
More file actions
83 lines (65 loc) · 2.21 KB
/
adt-queue.js
File metadata and controls
83 lines (65 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Queue implementation class
*/
/*---------------------------------------------------
The Queue constructor function
---------------------------------------------------*/
var Queue = function() {
// Define the _datastore property to store the data.
// In this case the _datastore is an array
Object.defineProperty(this, '_datastore', {
value: [],
writable: true,
enumerable: false,
configurable: false
});
};
/*---------------------------------------------------
This method tests whether the queue is empty
or not
---------------------------------------------------*/
Queue.prototype.isEmpty = function() {
return this._datastore.length === 0;
};
/*---------------------------------------------------
This method performs the standard ADT Queue
enqueue operation. That is, it adds an item to
the back of the queue.
---------------------------------------------------*/
Queue.prototype.enqueue = function(item) {
this._datastore.push(item);
};
/*---------------------------------------------------
This method performs the standard ADT Queue
dequeue operation. That is, it returns the item
at the front of the queue and removes it from
the queue.
---------------------------------------------------*/
Queue.prototype.dequeue = function(item) {
if (this._datastore.length === 0) {
throw new Error('adt-queue.dequeue(): Tried to dequeue an empty queue!');
}
return this._datastore.shift();
};
/*---------------------------------------------------
This method returns the item at the front of the
queue, but unlike the method 'dequeue', does not
remove the item from the queue.
---------------------------------------------------*/
Queue.prototype.front = function(item) {
if (this._datastore.length === 0) {
throw new Error('adt-queue.front(): Tried to get the front of an empty queue!');
}
return this._datastore[0];
};
/*---------------------------------------------------
This method returns the number of items in the
queue
---------------------------------------------------*/
Queue.prototype.size = function(item) {
return this._datastore.length;
};
/*---------------------------------------------------
Return the module
---------------------------------------------------*/
module.exports = Queue;