2023-02-09 00:25:01 +08:00
|
|
|
/*
|
2023-02-05 16:25:42 +08:00
|
|
|
* File: queue.rs
|
|
|
|
* Created Time: 2023-02-05
|
2024-01-02 01:25:37 +08:00
|
|
|
* Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICEx@outlook.com)
|
2023-02-09 00:25:01 +08:00
|
|
|
*/
|
|
|
|
|
2024-10-23 18:49:47 +08:00
|
|
|
use hello_algo_rust::include::print_util;
|
2023-02-05 16:25:42 +08:00
|
|
|
|
2023-02-15 21:32:03 +08:00
|
|
|
use std::collections::VecDeque;
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
pub fn main() {
|
|
|
|
// 初始化队列
|
2023-02-15 21:32:03 +08:00
|
|
|
let mut queue: VecDeque<i32> = VecDeque::new();
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
// 元素入队
|
|
|
|
queue.push_back(1);
|
|
|
|
queue.push_back(3);
|
|
|
|
queue.push_back(2);
|
|
|
|
queue.push_back(5);
|
|
|
|
queue.push_back(4);
|
|
|
|
print!("队列 queue = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
print_util::print_queue(&queue);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
// 访问队首元素
|
|
|
|
let peek = queue.front().unwrap();
|
|
|
|
println!("\n队首元素 peek = {peek}");
|
|
|
|
|
|
|
|
// 元素出队
|
2023-03-13 21:58:21 +08:00
|
|
|
let pop = queue.pop_front().unwrap();
|
|
|
|
print!("出队元素 pop = {pop},出队后 queue = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
print_util::print_queue(&queue);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
// 获取队列的长度
|
|
|
|
let size = queue.len();
|
|
|
|
print!("\n队列长度 size = {size}");
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
// 判断队列是否为空
|
2023-02-05 16:25:42 +08:00
|
|
|
let is_empty = queue.is_empty();
|
|
|
|
print!("\n队列是否为空 = {is_empty}");
|
2024-03-16 02:13:41 +08:00
|
|
|
}
|