2023-02-09 00:25:01 +08:00
|
|
|
/*
|
2023-02-05 16:25:42 +08:00
|
|
|
* File: deque.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
|
|
|
*/
|
|
|
|
|
|
|
|
include!("../include/include.rs");
|
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 deque: VecDeque<i32> = VecDeque::new();
|
2023-07-24 22:27:26 +08:00
|
|
|
deque.push_back(3);
|
|
|
|
deque.push_back(2);
|
2023-02-05 16:25:42 +08:00
|
|
|
deque.push_back(5);
|
|
|
|
print!("双向队列 deque = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
print_util::print_queue(&deque);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
// 访问元素
|
2023-07-24 22:27:26 +08:00
|
|
|
let peek_first = deque.front().unwrap();
|
2023-02-05 16:25:42 +08:00
|
|
|
print!("\n队首元素 peekFirst = {peek_first}");
|
2023-07-24 22:27:26 +08:00
|
|
|
let peek_last = deque.back().unwrap();
|
2023-02-05 16:25:42 +08:00
|
|
|
print!("\n队尾元素 peekLast = {peek_last}");
|
|
|
|
|
2023-07-24 22:27:26 +08:00
|
|
|
/* 元素入队 */
|
|
|
|
deque.push_back(4);
|
|
|
|
print!("\n元素 4 队尾入队后 deque = ");
|
|
|
|
print_util::print_queue(&deque);
|
|
|
|
deque.push_front(1);
|
|
|
|
print!("\n元素 1 队首入队后 deque = ");
|
|
|
|
print_util::print_queue(&deque);
|
|
|
|
|
2023-02-05 16:25:42 +08:00
|
|
|
// 元素出队
|
2023-07-24 22:27:26 +08:00
|
|
|
let pop_last = deque.pop_back().unwrap();
|
|
|
|
print!("\n队尾出队元素 = {pop_last},队尾出队后 deque = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
print_util::print_queue(&deque);
|
2023-07-24 22:27:26 +08:00
|
|
|
let pop_first = deque.pop_front().unwrap();
|
|
|
|
print!("\n队首出队元素 = {pop_first},队首出队后 deque = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
print_util::print_queue(&deque);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
|
|
// 获取双向队列的长度
|
|
|
|
let size = deque.len();
|
|
|
|
print!("\n双向队列长度 size = {size}");
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
// 判断双向队列是否为空
|
2023-02-05 16:25:42 +08:00
|
|
|
let is_empty = deque.is_empty();
|
|
|
|
print!("\n双向队列是否为空 = {is_empty}");
|
|
|
|
}
|