Skip to content

Commit 9439c23

Browse files
committed
simpify simple array to stack!
1 parent 6a58c48 commit 9439c23

File tree

1 file changed

+4
-35
lines changed

1 file changed

+4
-35
lines changed

src/data_structure/stack/stack-array-implement.ts

+4-35
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,24 @@
1-
class Node {
2-
value: any;
3-
next: any;
4-
constructor(value: any) {
5-
this.value = value;
6-
this.next = null;
7-
}
8-
}
91

102
class Stack {
113
array: any = []
12-
top: any;
13-
bottom: any;
14-
length: number;
154
constructor() {
16-
this.top = null;
17-
this.bottom = null;
18-
this.length = 0;
195
}
206

217
peek() {
22-
if (this.length == 0) return null
23-
return this.top
8+
if (this.array.length == 0) return null
9+
return this.array[this.array.length - 1]
2410
}
2511

2612
push(value: any) {
2713
this.array.push(value)
28-
if (this.length === 0) {
29-
this.top = this.array[0]
30-
this.bottom = this.array[0]
31-
} else {
32-
this.top = this.array[this.length]
33-
}
34-
this.length++
35-
3614
}
3715

3816
pop() {
39-
if (this.length == 0) return null
40-
const remove = this.array.pop()
41-
if (this.length == 0) {
42-
this.bottom = null
43-
this.top = null
44-
} else {
45-
this.top = this.array[this.array.length - 1]
46-
}
47-
this.length--
48-
return remove
17+
return this.array.pop()
4918
}
5019

5120
isEmpty() {
52-
return this.length === 0
21+
return this.array.length === 0
5322
}
5423

5524
//isEmpty

0 commit comments

Comments
 (0)