mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 13:26:29 +08:00
e65c7bd478
* Refine some details and coding style for Rust codes * Update coding style for Rust codes * Update time_complexity.rs * Update array.rs * Update leetcode_two_sum.rs * Update hash_map.rs * Update file headers * Update coding style for Rust codes and Zig codes * Update coding style for Rust codes and Zig codes --------- Co-authored-by: Yudong Jin <krahets@163.com>
44 lines
No EOL
1.4 KiB
Rust
44 lines
No EOL
1.4 KiB
Rust
/*
|
|
* File: deque.rs
|
|
* Created Time: 2023-02-05
|
|
* Author: sjinzh (sjinzh@gmail.com)
|
|
*/
|
|
|
|
include!("../include/include.rs");
|
|
|
|
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 = ");
|
|
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 = ");
|
|
print_util::print_queue(&deque);
|
|
let poll_last = deque.pop_back().unwrap(); // 队尾元素出队
|
|
print!("\n队尾出队元素 pollLast = {poll_last},队尾出队后 deque = ");
|
|
print_util::print_queue(&deque);
|
|
|
|
// 获取双向队列的长度
|
|
let size = deque.len();
|
|
print!("\n双向队列长度 size = {size}");
|
|
|
|
// 判断双向队列是否为空
|
|
let is_empty = deque.is_empty();
|
|
print!("\n双向队列是否为空 = {is_empty}");
|
|
} |