Last active
June 1, 2022 12:58
-
-
Save zRains/0a331f85d5dccf676e5747107e50ae6f to your computer and use it in GitHub Desktop.
Rust 链表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use core::fmt; | |
struct Node { | |
val: i32, | |
next: Option<Box<Node>>, | |
} | |
impl Node { | |
fn new(val: i32) -> Self { | |
Self { val, next: None } | |
} | |
} | |
struct Link { | |
len: i32, | |
head_node: Option<Box<Node>>, | |
} | |
impl Link { | |
fn new() -> Self { | |
Self { | |
head_node: None, | |
len: 0, | |
} | |
} | |
// 获取长度 | |
fn len(&self) -> i32 { | |
self.len | |
} | |
// 插入一个新头节点 | |
fn shift(&mut self, val: i32) { | |
let mut new_node = Box::new(Node::new(val)); | |
if let Some(node) = self.head_node.take() { | |
new_node.next = Some(node); | |
} | |
self.head_node = Some(new_node); | |
self.len += 1; | |
} | |
// 弹出一个头节点 | |
fn unshift(&mut self) -> Option<Box<Node>> { | |
match self.head_node.take() { | |
None => None, | |
Some(node) => { | |
self.head_node = node.next; | |
Some(Box::new(Node::new(node.val))) | |
} | |
} | |
} | |
// 插入一个新尾节点 | |
fn push(&mut self, val: i32) { | |
let new_node = Box::new(Node::new(val)); | |
match self.head_node.as_mut() { | |
None => { | |
self.head_node = Some(new_node); | |
} | |
Some(mut node) => { | |
while node.as_ref().next.is_some() { | |
node = node.next.as_mut().unwrap(); | |
} | |
node.next = Some(new_node); | |
} | |
} | |
self.len += 1; | |
} | |
// 弹出一个尾节点 | |
fn pop(&mut self) -> Option<Box<Node>> { | |
match self.head_node.as_mut() { | |
None => None, | |
Some(mut node) => { | |
while node.next.is_some() && node.next.as_ref().unwrap().next.is_some() { | |
node = node.next.as_mut().unwrap(); | |
} | |
self.len -= 1; | |
match node.next { | |
Some(_) => node.next.take(), | |
None => self.head_node.take(), | |
} | |
} | |
} | |
} | |
// 删除指定值的节点 | |
fn delete(&mut self, val: i32) { | |
if let Some(mut node) = self.head_node.take() { | |
while node.val == val { | |
if let Some(child_node) = node.next.take() { | |
node = child_node; | |
self.len -= 1; | |
} else { | |
self.head_node = None; | |
self.len = 0; | |
return; | |
} | |
} | |
self.head_node = Some(node); | |
} | |
} | |
} | |
impl fmt::Display for Link { | |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
let mut current_node = &self.head_node; | |
while let Some(node) = current_node { | |
write!(f, "{} -> ", node.val).expect("读取失败"); | |
current_node = &node.next; | |
} | |
write!(f, "Nil") | |
} | |
} | |
fn main() { | |
let mut link = Link::new(); | |
link.shift(1); | |
link.shift(2); | |
link.shift(3); | |
link.shift(3); | |
link.shift(3); | |
link.shift(3); | |
link.push(100); | |
link.delete(3); | |
println!("当前长度{}", link.len()); | |
println!("{}", link); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment