-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemory.js
80 lines (62 loc) · 1.5 KB
/
memory.js
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
function Memory() {}
Memory.prototype.push = function(data) {};
Memory.prototype.pull = function() {};
Memory.prototype.canPull = function(quantity) {};
Memory.prototype.copy = function() {};
Memory.prototype.flip = function() {};
function Stack() {
this.data = [];
}
Stack.prototype.push = function(data) {
this.data.push(data);
};
Stack.prototype.pull = function() {
return this.data.pop();
};
Stack.prototype.canPull = function(quantity) {
return this.data.length >= quantity;
};
Stack.prototype.copy = function() {
if (!this.canPull(1)) return false;
var data = this.pull();
this.data.push(data);
this.data.push(data);
return true;
};
Stack.prototype.flip = function() {
if (!this.canPull(2)) return false;
var a = this.pull();
var b = this.pull();
this.data.push(a);
this.data.push(b);
return true;
};
function Queue() {
this.data = [];
}
Queue.prototype.push = function(data) {
this.data.push(data);
};
Queue.prototype.pull = function() {
return this.data.shift();
};
Queue.prototype.canPull = function(quantity) {
return this.data.length >= quantity;
};
Queue.prototype.copy = function() {
if (!this.canPull(1)) return false;
var data = this.data[0];
this.data.unshift(data);
return true;
};
Queue.prototype.flip = function() {
if (!this.canPull(2)) return false;
var a = this.pull();
var b = this.pull();
this.data.unshift(a);
this.data.unshift(b);
return true;
};
module.exports.Memory = Memory;
module.exports.Stack = Stack;
module.exports.Queue = Queue;