File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ # --- Directions
2
+
3
+ Create a stack data structure. The stack
4
+ should be a class with methods 'push', 'pop', and
5
+ 'peek'. Adding an element to the stack should
6
+ store it until it is removed.
7
+
8
+ # --- Examples
9
+
10
+ const s = new Stack();
11
+ s.push(1);
12
+ s.push(2);
13
+ s.pop(); should returns 2
14
+ s.pop(); should returns 1
15
+
16
+
17
+ # --- Solutions
18
+
19
+ // Solution
20
+
21
+ class Stack {
22
+ constructor() {
23
+ this.data = [];
24
+ }
25
+
26
+ push(record) {
27
+ this.data.push(record);
28
+ }
29
+
30
+ pop() {
31
+ return this.data.pop();
32
+ }
33
+
34
+ peek() {
35
+ return this.data[this.data.length -1];
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ // --- Directions
2
+
3
+ // Create a stack data structure. The stack
4
+ // should be a class with methods 'push', 'pop', and
5
+ // 'peek'. Adding an element to the stack should
6
+ // store it until it is removed.
7
+
8
+ // --- Examples
9
+
10
+ // const s = new Stack();
11
+ // s.push(1);
12
+ // s.push(2);
13
+ // s.pop(); should returns 2
14
+ // s.pop(); should returns 1
15
+
16
+ // Solution
17
+
18
+ class Stack {
19
+ constructor ( ) {
20
+ this . data = [ ] ;
21
+ }
22
+
23
+ push ( record ) {
24
+ this . data . push ( record ) ;
25
+ }
26
+
27
+ pop ( ) {
28
+ return this . data . pop ( ) ;
29
+ }
30
+
31
+ peek ( ) {
32
+ return this . data [ this . data . length - 1 ] ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments