mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 12:56:28 +08:00
8a388d8422
* update rust codes * update rust codes * update rust codes * update and add rust codes for hash_map, binary_search, bubble_sort * update and add rust codes for hash_map, binary_search, bubble_sort * add rust codes for chapter stack * add rust codes for chapter queue * add rust codes for chapter deque
42 lines
No EOL
1.4 KiB
Rust
42 lines
No EOL
1.4 KiB
Rust
/**
|
|
* File: deque.rs
|
|
* Created Time: 2023-02-05
|
|
* Author: sjinzh (sjinzh@gmail.com)
|
|
*/
|
|
|
|
use std::collections::LinkedList;
|
|
|
|
/* Driver Code */
|
|
pub fn main() {
|
|
// 初始化双向队列
|
|
let mut deque: LinkedList<i32> = LinkedList::new();
|
|
deque.push_back(2); // 添加至队尾
|
|
deque.push_back(5);
|
|
deque.push_back(4);
|
|
deque.push_front(3); // 添加至队首
|
|
deque.push_front(1);
|
|
print!("双向队列 deque = ");
|
|
inc::print_util::print_queue(&deque);
|
|
|
|
// 访问元素
|
|
let peek_first = deque.front().unwrap(); // 队首元素
|
|
print!("\n队首元素 peekFirst = {peek_first}");
|
|
let peek_last = deque.back().unwrap(); // 队尾元素
|
|
print!("\n队尾元素 peekLast = {peek_last}");
|
|
|
|
// 元素出队
|
|
let poll_first = deque.pop_front().unwrap(); // 队首元素出队
|
|
print!("\n队首出队元素 pollFirst = {poll_first},队首出队后 deque = ");
|
|
inc::print_util::print_queue(&deque);
|
|
let poll_last = deque.pop_back().unwrap(); // 队尾元素出队
|
|
print!("\n队尾出队元素 pollLast = {poll_last},队尾出队后 deque = ");
|
|
inc::print_util::print_queue(&deque);
|
|
|
|
// 获取双向队列的长度
|
|
let size = deque.len();
|
|
print!("\n双向队列长度 size = {size}");
|
|
|
|
/* 判断双向队列是否为空 */
|
|
let is_empty = deque.is_empty();
|
|
print!("\n双向队列是否为空 = {is_empty}");
|
|
} |