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 75e387f

Browse files
authored
Merge pull request XPoet#11 from fltenwall/master
栈的pop和peek操作增加栈为空的判断,提高代码健壮性
2 parents c17cf12 + be7ac84 commit 75e387f

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 によって変換されたページ (->オリジナル) /