-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path225. Implement Stack using Queues.js
More file actions
138 lines (122 loc) · 2.56 KB
/
Copy path225. Implement Stack using Queues.js
File metadata and controls
138 lines (122 loc) · 2.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.q = [];
this.tempq = [];
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
var tmp;
while (this.q.length > 0) {
tmp = this.q.pop();
this.tempq.unshift(tmp);
}
this.q.unshift(x);
while (this.tempq.length > 0) {
tmp = this.tempq.pop();
this.q.unshift(tmp);
}
return;
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.q.pop();
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
if (this.q.length > 0) {
return this.q[this.q.length-1];
}
else {
return null;
}
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.q.length === 0;
};
/**
* Your MyStack object will be instantiated and called as such:
* var obj = Object.create(MyStack).createNew()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.main = [];
this.helper = [];
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
var main = this.main;
var helper = this.helper;
if (main.length === 0) {
main.unshift(x);
return;
}
var tmp;
while (main.length !== 0) {
tmp = main.pop();
helper.unshift(tmp);
}
main.unshift(x);
while (helper.length !== 0) {
tmp = helper.pop();
main.unshift(tmp);
}
return;
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
var main = this.main;
return main.pop();
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
var main = this.main;
return main[main.length-1];
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
var main = this.main;
return (main.length === 0);
};
/**
* Your MyStack object will be instantiated and called as such:
* var obj = Object.create(MyStack).createNew()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/