-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueStack.js
More file actions
40 lines (31 loc) · 728 Bytes
/
queueStack.js
File metadata and controls
40 lines (31 loc) · 728 Bytes
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
// Write a stack. Once you’re done, implement a queue using two stacks. Do not create a storage array for your queue.
var Stack = function() {
var storage = [];
this.push = function(item){
storage.push(item);
};
this.pop = function(){
return storage.pop();
};
this.size = function(){
return storage.length;
};
};
var Queue = function() {
var inbox = new Stack();
var outbox = new Stack();
this.enqueue = function(item){
inbox.push(item)
};
this.dequeue = function(){
if(outbox.size() === 0){
while(inbox.size()){
outbox.push(inbox.pop());
}
}
return outbox.pop();
};
this.size = function(){
return inbox.size() + outbox.size();
};
};