Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a1fecd0

Browse files
committedAug 3, 2018
simple stack implementation
1 parent 15e7872 commit a1fecd0

5 files changed

+29
-0
lines changed
 
File renamed without changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
// Each data structure can be implemented by the different method. We can treat stack as a subclass of Array. So we take the array for example here.
3+
4+
class Stack {
5+
6+
constructor() {
7+
this.stack = []
8+
}
9+
10+
push(item) {
11+
this.stack.push(item)
12+
}
13+
14+
pop (item) {
15+
this.stack.pop(item)
16+
}
17+
18+
peek (item) {
19+
this.stack[this.getCount() - 1]
20+
}
21+
22+
getCount() {
23+
return this.stack.length
24+
}
25+
26+
isEmpty() {
27+
return this.stack.length === 0
28+
}
29+
}
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.