-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.js
More file actions
54 lines (53 loc) · 947 Bytes
/
Stack.js
File metadata and controls
54 lines (53 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import LinkedList from '../linked-list/LinkedList'
export default class Stack {
constructor() {
this.linkedList = new LinkedList()
}
/**
* @return {boolean}
*/
isEmpty() {
return !this.linkedList.tail
}
/**
* 获取尾部元素值
* @return {*}
*/
peek() {
if (this.isEmpty()) {
return null
}
return this.linkedList.tail.value
}
/**
* 入栈
* @param {*} value
*/
push(value) {
this.linkedList.append(value)
}
/**
* 出栈
* @return {*}
*/
pop() {
const removedTail = this.linkedList.deleteTail()
return removedTail ? removedTail.value : null
}
/**
* @return {*[]}
*/
toArray() {
return this.linkedList
.toArray()
.map(linkedListNode => linkedListNode.value)
.reverse()
}
/**
* @param {Function} callback
* @return {string}
*/
toString(callback) {
return this.linkedList.toString(callback)
}
}