File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
animation-simulation/栈和队列 Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 18
18
19
19
下面我们来看一下题目代码,也是很容易理解。
20
20
21
+ Java Code:
21
22
``` java
22
23
class MyStack {
23
24
// 初始化队列
@@ -55,3 +56,34 @@ class MyStack {
55
56
56
57
```
57
58
59
+ JS Code:
60
+ ``` javascript
61
+ var MyStack = function () {
62
+ this .queue = [];
63
+ };
64
+
65
+ MyStack .prototype .push = function (x ) {
66
+ this .queue .push (x);
67
+ if (this .queue .length > 1 ) {
68
+ let i = this .queue .length - 1 ;
69
+ while (i) {
70
+ this .queue .push (this .queue .shift ());
71
+ i-- ;
72
+ }
73
+ }
74
+ };
75
+
76
+ MyStack .prototype .pop = function () {
77
+ return this .queue .shift ();
78
+ };
79
+
80
+ MyStack .prototype .top = function () {
81
+ return this .empty () ? null : this .queue [0 ];
82
+
83
+ };
84
+
85
+ MyStack .prototype .empty = function () {
86
+ return ! this .queue .length ;
87
+ };
88
+ ```
89
+
You can’t perform that action at this time.
0 commit comments