Use Vec.last() method to access the top item of stack. (#942)
* Use Vec.last() method to access the top item of stack. * Use Vec.last() method to access the top item of stack.
This commit is contained in:
parent
2b0cf6f9ab
commit
9baf4a1753
@ -22,7 +22,7 @@ pub fn main() {
|
|||||||
print_util::print_array(&stack);
|
print_util::print_array(&stack);
|
||||||
|
|
||||||
// 访问栈顶元素
|
// 访问栈顶元素
|
||||||
let peek = stack.get(stack.len() - 1).unwrap();
|
let peek = stack.last().unwrap();
|
||||||
print!("\n栈顶元素 peek = {peek}");
|
print!("\n栈顶元素 peek = {peek}");
|
||||||
|
|
||||||
// 元素出栈
|
// 元素出栈
|
||||||
|
@ -278,7 +278,7 @@
|
|||||||
stack.push(4);
|
stack.push(4);
|
||||||
|
|
||||||
/* 访问栈顶元素 */
|
/* 访问栈顶元素 */
|
||||||
let top = stack[stack.len() - 1];
|
let top = stack.last().unwrap();
|
||||||
|
|
||||||
/* 元素出栈 */
|
/* 元素出栈 */
|
||||||
let pop = stack.pop().unwrap();
|
let pop = stack.pop().unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user