mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
🚀feat: add rust codes for linkedlist_stack, linkedlist_queue and linkedlist_deque (#410)
* feat: add rust codes for space_complexity * feat: add rust codes for linkedlist_stack * update * feat: add rust codes for linkedlist_queue * feat: add rust codes for linkedlist_deque * update
This commit is contained in:
@ -49,16 +49,31 @@ path = "chapter_array_and_linkedlist/my_list.rs"
|
||||
name = "stack"
|
||||
path = "chapter_stack_and_queue/stack.rs"
|
||||
|
||||
# Run Command: cargo run --bin linkedlist_stack
|
||||
[[bin]]
|
||||
name = "linkedlist_stack"
|
||||
path = "chapter_stack_and_queue/linkedlist_stack.rs"
|
||||
|
||||
# Run Command: cargo run --bin queue
|
||||
[[bin]]
|
||||
name = "queue"
|
||||
path = "chapter_stack_and_queue/queue.rs"
|
||||
|
||||
# Run Command: cargo run --bin linkedlist_queue
|
||||
[[bin]]
|
||||
name = "linkedlist_queue"
|
||||
path = "chapter_stack_and_queue/linkedlist_queue.rs"
|
||||
|
||||
# Run Command: cargo run --bin deque
|
||||
[[bin]]
|
||||
name = "deque"
|
||||
path = "chapter_stack_and_queue/deque.rs"
|
||||
|
||||
# Run Command: cargo run --bin linkedlist_deque
|
||||
[[bin]]
|
||||
name = "linkedlist_deque"
|
||||
path = "chapter_stack_and_queue/linkedlist_deque.rs"
|
||||
|
||||
# Run Command: cargo run --bin hash_map
|
||||
[[bin]]
|
||||
name = "hash_map"
|
||||
|
||||
@ -5,8 +5,10 @@
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
#[allow(non_snake_case)]
|
||||
|
||||
@ -9,6 +9,8 @@ include!("../include/include.rs");
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
use tree_node::TreeNode;
|
||||
|
||||
/* 函数 */
|
||||
fn function() ->i32 {
|
||||
|
||||
207
codes/rust/chapter_stack_and_queue/linkedlist_deque.rs
Normal file
207
codes/rust/chapter_stack_and_queue/linkedlist_deque.rs
Normal file
@ -0,0 +1,207 @@
|
||||
/*
|
||||
* File: linkedlist_deque.rs
|
||||
* Created Time: 2023-03-11
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/* 双向链表结点 */
|
||||
pub struct ListNode<T> {
|
||||
pub val: T, // 结点值
|
||||
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继结点引用(指针)
|
||||
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱结点引用(指针)
|
||||
}
|
||||
|
||||
impl<T> ListNode<T> {
|
||||
pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {
|
||||
Rc::new(RefCell::new(ListNode {
|
||||
val,
|
||||
next: None,
|
||||
prev: None,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
#[allow(dead_code)]
|
||||
pub struct LinkedListDeque<T> {
|
||||
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
|
||||
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
|
||||
que_size: usize, // 双向队列的长度
|
||||
}
|
||||
|
||||
impl<T: Copy> LinkedListDeque<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
front: None,
|
||||
rear: None,
|
||||
que_size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
pub fn size(&self) -> usize {
|
||||
return self.que_size;
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.size() == 0;
|
||||
}
|
||||
|
||||
/* 入队操作 */
|
||||
pub fn push(&mut self, num: T, is_front: bool) {
|
||||
let node = ListNode::new(num);
|
||||
// 队首入队操作
|
||||
if is_front {
|
||||
// 将 node 添加至链表头部
|
||||
match self.front.take() {
|
||||
Some(old_front) => {
|
||||
old_front.borrow_mut().prev = Some(node.clone());
|
||||
node.borrow_mut().next = Some(old_front);
|
||||
self.front = Some(node); // 更新头结点
|
||||
}
|
||||
None => {
|
||||
self.rear = Some(node.clone());
|
||||
self.front = Some(node);
|
||||
}
|
||||
}
|
||||
// 队尾入队操作
|
||||
} else {
|
||||
// 将 node 添加至链表尾部
|
||||
match self.rear.take() {
|
||||
Some(old_rear) => {
|
||||
old_rear.borrow_mut().next = Some(node.clone());
|
||||
node.borrow_mut().prev = Some(old_rear);
|
||||
self.rear = Some(node); // 更新尾结点
|
||||
}
|
||||
None => {
|
||||
self.front = Some(node.clone());
|
||||
self.rear = Some(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.que_size += 1;
|
||||
}
|
||||
|
||||
/* 队首入队 */
|
||||
pub fn push_first(&mut self, num: T) {
|
||||
self.push(num, true);
|
||||
}
|
||||
|
||||
/* 队尾入队 */
|
||||
pub fn push_last(&mut self, num: T) {
|
||||
self.push(num, false);
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
pub fn poll(&mut self, is_front: bool) -> Option<T> {
|
||||
if self.is_empty() {return None};
|
||||
// 队首出队操作
|
||||
if is_front {
|
||||
self.front.take().map(|old_front| {
|
||||
match old_front.borrow_mut().next.take() {
|
||||
Some(new_front) => {
|
||||
new_front.borrow_mut().prev.take();
|
||||
self.front = Some(new_front); // 更新头结点
|
||||
}
|
||||
None => {
|
||||
self.rear.take();
|
||||
}
|
||||
}
|
||||
self.que_size -= 1;
|
||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
||||
})
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
self.rear.take().map(|old_rear| {
|
||||
match old_rear.borrow_mut().prev.take() {
|
||||
Some(new_rear) => {
|
||||
new_rear.borrow_mut().next.take();
|
||||
self.rear = Some(new_rear); // 更新尾结点
|
||||
}
|
||||
None => {
|
||||
self.front.take();
|
||||
}
|
||||
}
|
||||
self.que_size -= 1;
|
||||
Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
pub fn poll_first(&mut self) -> Option<T> {
|
||||
return self.poll(true);
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
pub fn poll_last(&mut self) -> Option<T> {
|
||||
return self.poll(false);
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
|
||||
self.front.as_ref()
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
|
||||
self.rear.as_ref()
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.insert(0, node.borrow().val);
|
||||
return nums;
|
||||
}
|
||||
return Vec::new();
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化双向队列 */
|
||||
let mut deque = LinkedListDeque::new();
|
||||
deque.push_last(3);
|
||||
deque.push_last(2);
|
||||
deque.push_last(5);
|
||||
print!("双向队列 deque = ");
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
|
||||
/* 访问元素 */
|
||||
let peek_first = deque.peek_first().unwrap().borrow().val;
|
||||
print!("\n队首元素 peek_first = {}", peek_first);
|
||||
let peek_last = deque.peek_last().unwrap().borrow().val;
|
||||
print!("\n队尾元素 peek_last = {}", peek_last);
|
||||
|
||||
/* 元素入队 */
|
||||
deque.push_last(4);
|
||||
print!("\n元素 4 队尾入队后 deque = ");
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
deque.push_first(1);
|
||||
print!("\n元素 1 队首入队后 deque = ");
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
|
||||
/* 元素出队 */
|
||||
let poll_last = deque.poll_last().unwrap();
|
||||
print!("\n队尾出队元素 = {},队尾出队后 deque = ", poll_last);
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
let poll_first = deque.poll_first().unwrap();
|
||||
print!("\n队首出队元素 = {},队首出队后 deque = ", poll_first);
|
||||
print_util::print_array(&deque.to_array(deque.peek_first()));
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
let size = deque.size();
|
||||
print!("\n双向队列长度 size = {}", size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
let is_empty = deque.is_empty();
|
||||
print!("\n双向队列是否为空 = {}", is_empty);
|
||||
}
|
||||
121
codes/rust/chapter_stack_and_queue/linkedlist_queue.rs
Normal file
121
codes/rust/chapter_stack_and_queue/linkedlist_queue.rs
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* File: linkedlist_queue.rs
|
||||
* Created Time: 2023-03-11
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
#[allow(dead_code)]
|
||||
pub struct LinkedListQueue<T> {
|
||||
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
|
||||
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
|
||||
que_size: usize, // 队列的长度
|
||||
}
|
||||
|
||||
impl<T: Copy> LinkedListQueue<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
front: None,
|
||||
rear: None,
|
||||
que_size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
pub fn size(&self) -> usize {
|
||||
return self.que_size;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.size() == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
pub fn push(&mut self, num: T) {
|
||||
// 尾结点后添加 num
|
||||
let new_rear = ListNode::new(num);
|
||||
match self.rear.take() {
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
Some(old_rear) => {
|
||||
old_rear.borrow_mut().next = Some(new_rear.clone());
|
||||
self.rear = Some(new_rear);
|
||||
}
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
None => {
|
||||
self.front = Some(new_rear.clone());
|
||||
self.rear = Some(new_rear);
|
||||
}
|
||||
}
|
||||
self.que_size += 1;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
pub fn poll(&mut self) -> Option<T> {
|
||||
self.front.take().map(|old_front| {
|
||||
match old_front.borrow_mut().next.take() {
|
||||
Some(new_front) => {
|
||||
self.front = Some(new_front);
|
||||
}
|
||||
None => {
|
||||
self.rear.take();
|
||||
}
|
||||
}
|
||||
self.que_size -= 1;
|
||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
||||
})
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
|
||||
self.front.as_ref()
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.insert(0, node.borrow().val);
|
||||
return nums;
|
||||
}
|
||||
return Vec::new();
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化队列 */
|
||||
let mut queue = LinkedListQueue::new();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
print!("队列 queue = ");
|
||||
print_util::print_array(&queue.to_array(queue.peek()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
let peek = queue.peek().unwrap().borrow().val;
|
||||
print!("\n队首元素 peek = {}", peek);
|
||||
|
||||
/* 元素出队 */
|
||||
let poll = queue.poll().unwrap();
|
||||
print!("\n出队元素 poll = {},出队后 queue = ", poll);
|
||||
print_util::print_array(&queue.to_array(queue.peek()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
let size = queue.size();
|
||||
print!("\n队列长度 size = {}", size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
let is_empty = queue.is_empty();
|
||||
print!("\n队列是否为空 = {}", is_empty);
|
||||
}
|
||||
108
codes/rust/chapter_stack_and_queue/linkedlist_stack.rs
Normal file
108
codes/rust/chapter_stack_and_queue/linkedlist_stack.rs
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* File: linkedlist_stack.rs
|
||||
* Created Time: 2023-03-11
|
||||
* Author: sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
|
||||
/* 基于链表实现的栈 */
|
||||
#[allow(dead_code)]
|
||||
pub struct LinkedListStack<T> {
|
||||
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头结点作为栈顶
|
||||
stk_size: usize, // 栈的长度
|
||||
}
|
||||
|
||||
impl<T: Copy> LinkedListStack<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
stack_peek: None,
|
||||
stk_size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
pub fn size(&self) -> usize {
|
||||
return self.stk_size;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
pub fn push(&mut self, num: T) {
|
||||
let node = ListNode::new(num);
|
||||
node.borrow_mut().next = self.stack_peek.take();
|
||||
self.stack_peek = Some(node);
|
||||
self.stk_size += 1;
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.stack_peek.take().map(|old_head| {
|
||||
match old_head.borrow_mut().next.take() {
|
||||
Some(new_head) => {
|
||||
self.stack_peek = Some(new_head);
|
||||
}
|
||||
None => {
|
||||
self.stack_peek = None;
|
||||
}
|
||||
}
|
||||
self.stk_size -= 1;
|
||||
Rc::try_unwrap(old_head).ok().unwrap().into_inner().val
|
||||
})
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
|
||||
self.stack_peek.as_ref()
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.push(node.borrow().val);
|
||||
return nums;
|
||||
}
|
||||
return Vec::new();
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化栈 */
|
||||
let mut stack = LinkedListStack::new();
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
print!("栈 stack = ");
|
||||
print_util::print_array(&stack.to_array(stack.peek()));
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
let peek = stack.peek().unwrap().borrow().val;
|
||||
print!("\n栈顶元素 peek = {}", peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
let pop = stack.pop().unwrap();
|
||||
print!("\n出栈元素 pop = {},出栈后 stack = ", pop);
|
||||
print_util::print_array(&stack.to_array(stack.peek()));
|
||||
|
||||
/* 获取栈的长度 */
|
||||
let size = stack.size();
|
||||
print!("\n栈的长度 size = {}", size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
let is_empty = stack.is_empty();
|
||||
print!("\n栈是否为空 = {}", is_empty);
|
||||
}
|
||||
@ -6,6 +6,4 @@
|
||||
|
||||
pub mod print_util;
|
||||
pub mod list_node;
|
||||
pub use list_node::ListNode;
|
||||
pub mod tree_node;
|
||||
pub use tree_node::TreeNode;
|
||||
pub mod tree_node;
|
||||
Reference in New Issue
Block a user