We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c6cd0b4 commit c25f3f3Copy full SHA for c25f3f3
JavaScript/1-stack.js
@@ -0,0 +1,34 @@
1
+'use strict';
2
+
3
+class Stack {
4
+ constructor() {
5
+ this.last = null;
6
+ }
7
+ push(item) {
8
+ const prev = this.last;
9
+ const element = { prev, item };
10
+ this.last = element;
11
12
+ pop() {
13
+ const element = this.last;
14
+ if (!element) return null;
15
+ this.last = element.prev;
16
+ return element.item;
17
18
+}
19
20
+// Usage
21
22
+const obj1 = { name: 'first' };
23
+const obj2 = { name: 'second' };
24
+const obj3 = { name: 'third' };
25
26
+const list = new Stack();
27
+list.push(obj1);
28
+list.push(obj2);
29
+list.push(obj3);
30
31
+console.dir(list.pop());
32
33
34
0 commit comments