Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
rust
链表
基于 Rust 的单向链表实现
发布于 2021-04-28 09:24:13 浏览:1258
订阅该版
[tocm] ## 利用结构体 (struct) 定义成员变量 num 表示当前结点值,next 指向下一个结点的指针。 ``` #[derive(Debug)] pub struct List { pub num: i32, pub next: Option
>, } ``` ## 利用 impl 关键字来定义结构体成员方法 ``` impl List { } ``` ### 创建链表 ``` pub fn new(value: i32) -> List { List { next: None, num: value } } ``` ### 添加结点 ``` pub fn append(&mut self, value: i32) { let _node = List::new(value); self.get_last_node().next = Some(Box::new(_node)); } ``` ### 计算链表长度 ``` pub fn len(&self) -> u32 { match &self.next { Some(list) => list.len() + 1, None => 0, } } ``` ### 找到最后一个结点 ``` pub fn get_last_node(&mut self) -> &mut Self { if let Some(ref mut node) = self.next { node.get_last_node() } else { return self; } } ``` ### 遍历链表 ``` pub fn traverse(&self) { let mut node_num = 0; let mut curr = self.next.as_ref(); while curr.is_some() { print!("node{}: {:?}\n", node_num, curr); curr = curr.unwrap().next.as_ref(); node_num += 1; } } ``` ### 删除结点值为 value 的结点 ``` pub fn delete_node(head: Option
>, value: i32) -> Option
> { let mut head = head; let mut curr = head.as_mut(); while curr.is_some() && curr.as_ref().unwrap().next.is_some() { if curr.as_ref().unwrap().next.as_ref().unwrap().num == value { let next = curr.as_mut().unwrap().next.as_mut().unwrap().next.take(); curr.as_mut().unwrap().next = next; } else { curr = curr.unwrap().next.as_mut(); } } return head; } ``` ## 测试代码 ``` #[derive(Debug)] pub struct List { pub num: i32, pub next: Option
>, } impl List { pub fn new(value: i32) -> List { List { next: None, num: value } } // 翻转链表 pub fn reverse_list(head:Option
>) -> Option
> { let mut prev = None; // 上一个节点 let mut cur = head; // 当前节点 while let Some(mut _node) = cur { // 用take置换next中的节点需要 mut cur = _node.next.take(); // 换出 next 作为下一次的 cur _node.next = prev; // 把next指向前一个节点 prev = Some(_node); // 更新 prev } return prev; // 跳出循环时,prev就是翻转后的头节点 } // 遍历链表 pub fn traverse(&self) { let mut node_num = 0; let mut curr = self.next.as_ref(); while curr.is_some() { print!("node{}: {:?}\n", node_num, curr); curr = curr.unwrap().next.as_ref(); node_num += 1; } } // 删除节点, 删除节点值为 value 的节点 pub fn delete_node(head: Option
>, value: i32) -> Option
> { let mut head = head; let mut curr = head.as_mut(); while curr.is_some() && curr.as_ref().unwrap().next.is_some() { if curr.as_ref().unwrap().next.as_ref().unwrap().num == value { let next = curr.as_mut().unwrap().next.as_mut().unwrap().next.take(); curr.as_mut().unwrap().next = next; } else { curr = curr.unwrap().next.as_mut(); } } return head; } // 找到最后一个节点 pub fn get_last_node(&mut self) -> &mut Self { if let Some(ref mut node) = self.next { node.get_last_node() } else { return self; } } // 添加节点 pub fn append(&mut self, value: i32) { let _node = List::new(value); self.get_last_node().next = Some(Box::new(_node)); } // 计算链表长度 pub fn len(&self) -> u32 { match &self.next { Some(list) => list.len() + 1, None => 0, } } } fn main() { // 创建一个新的链表 let mut list = List::new(0); // 添加节点 list.append(1); list.append(2); list.append(3); list.append(4); println!("linked list has length: {}", list.len()); // 遍历列表 println!("{:?}", list.traverse()); // 删除列表 value 为 2 的节点,并返回新列表 let new_list = List::delete_node(Some(Box::new(list)), 2); println!("{:?}", new_list); } linked list has length: 4 node0: Some(List { num: 1, next: Some(List { num: 2, next: Some(List { num: 3, next: Some(List { num: 4, next: None }) }) }) }) node1: Some(List { num: 2, next: Some(List { num: 3, next: Some(List { num: 4, next: None }) }) }) node2: Some(List { num: 3, next: Some(List { num: 4, next: None }) }) node3: Some(List { num: 4, next: None }) () Some(List { num: 0, next: Some(List { num: 1, next: Some(List { num: 3, next: Some(List { num: 4, next: None }) }) }) } ```
1
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
Papalymo
请勿打扰
文章
25
回答
85
被采纳
17
关注TA
发私信
相关文章
1
Rust 嵌入式开发 STM32 和 RISC-V
2
链表使用bug:SCB_CFSR_UFSR:0x100 UNALIGNED
3
请教链表代码理解问题
4
关于这个函数 rt_list_insert_before
5
rt-thread使用链表组件编译报错
6
请问有没有介绍rtthread内部链表的材料?
7
单链表问题,rt_slist_for_each 宏问题
8
请问对 rt_slist_t 或 rt_list_t 链表的操作需要加锁吗?
9
IPC 内核链表while()死循环
10
rt_container_of宏接口的使用
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
FAL
RTC
rt-smart
I2C_IIC
cubemx
UART
ESP8266
WIZnet_W5500
BSP
ota在线升级
PWM
flash
packages_软件包
freemodbus
潘多拉开发板_Pandora
ADC
GD32
定时器
编译报错
flashDB
keil_MDK
socket
中断
rt_mq_消息队列_msg_queue
Debug
ulog
SFUD
msh
C++_cpp
at_device
本月问答贡献
出出啊
1524
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
821
个答案
179
次被采纳
crystal266
555
个答案
162
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
1
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部