-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathqueue.js
More file actions
68 lines (61 loc) · 1.48 KB
/
queue.js
File metadata and controls
68 lines (61 loc) · 1.48 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
// Queue is a linear data structure like a Stack that only deletes the oldest
// added data
var Queue = function() {
var self = {
oldestIndex: 1,
newestIndex: 1,
storage: {},
size: function(){
return self.newestIndex - self.oldestIndex;
},
enqueue: function(data){
self.storage[self.newestIndex] = data;
self.newestIndex++;
},
dequeue: function(){
if (self.size() > 0){
console.log(self.storage[self.oldestIndex]);
delete self.storage[self.oldestIndex];
++self.oldestIndex;
} else {
return;
}
},
inspect: function(){
return {
oldestIndex: self.oldestIndex,
newestIndex: self.newestIndex,
storage: self.storage
};
}
};
return self;
};
// Queue.prototype.size = function(){
// return this.newestIndex - this.oldestIndex;
// };
//
// Queue.prototype.enqueue = function(data){
// this.storage[this.newestIndex] = data;
// this.newestIndex++;
// };
//
// Queue.prototype.dequeue = function(){
// if (this.size() > 0){
// console.log(this.storage[this.oldestIndex]);
// delete this.storage[this.oldestIndex];
// ++this.oldestIndex;
// } else {
// return;
// }
// };
var something = Object.create(new Queue());
something.enqueue(4);
something.enqueue(5);
something.dequeue();
console.log(something.inspect());
console.log(something.size());
something.dequeue();
console.log(something.size());
something.dequeue();
console.log(something);