Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95e3b85

Browse files
adding stack data structure
1 parent ba64b82 commit 95e3b85

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

‎stack/directions.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

‎stack/stack.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /