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 be7ac84

Browse files
栈的pop和peek操作增加栈为空的判断,提高代码健壮性
1 parent c17cf12 commit be7ac84

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

‎src/Stack/stack.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ export default class Stack {
1212

1313
// pop() 出栈操作,从栈中取出元素,并返回取出的那个元素
1414
pop() {
15+
if(this.isEmpty()) throw new Error('栈空了');
1516
return this.items.pop();
1617
}
1718

1819
// peek() 查看栈顶元素
1920
peek() {
21+
if(this.isEmpty()) throw new Error('栈空了');
2022
return this.items[this.items.length - 1];
2123
}
2224

0 commit comments

Comments
(0)

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