介绍
视频地址:www.bilibili.com/video/av78062009/
相关源码:github.com/anonymousGiga/Rust-link...
详细内容
本节我们就在上一节定义的链表的基础上来实现链表的通用函数。
因为本章的链表更多的操作链表的头尾和尾,所以我们在通用函数中不提供push和pop,而是提供append和tail函数。
append
append函数类似于之前我们实现的push,但是因为现在我们使用的Rc而不是Box,所以代码上略有差别,如下:
pub fn append(&mut self, elem: T) -> List<T> { | |
List { head: Some(Rc::new(Node { | |
elem: elem, | |
next: self.head.clone(),}))} | |
} |
tail
tail函数主要返回最后放入链表的元素,实现代码如下:
pub fn tail(&self) -> List<T> { | |
List { head: self.head.as_ref().and_then(|node| { | |
node.next.clone()})}} |
head
head函数类似于我们之前的peek函数,代码如下:
pub fn head(&self) -> Option<&T> { | |
self.head.as_ref().map(|node| &node.elem)} |
上述函数的测试函数
fn basics() {let mut list = List::new(); | |
assert_eq!(list.head(), None); | |
let list = list.append(1).append(2).append(3); | |
assert_eq!(list.head(), Some(&3)); | |
let list = list.tail(); | |
assert_eq!(list.head(), Some(&2)); | |
let list = list.tail(); | |
assert_eq!(list.head(), Some(&1)); | |
let list = list.tail(); | |
assert_eq!(list.head(), None);} |
iter
下面我们来实现迭代器,代码如下:
//实现Iter | |
pub struct Iter<'a, T> { | |
next: Option<&'a Node<T>>, | |
} | |
impl<T> List<T> { | |
pub fn iter(&self) -> Iter<T> { | |
Iter { next: self.head.as_deref() }} | |
} | |
impl<'a, T> Iterator for Iter<'a, T> { | |
type Item = &'a T; | |
fn next(&mut self) -> Option<Self::Item> { | |
self.next.map(|node| { | |
self.next = node.next.as_deref();&node.elem | |
})} | |
} |
迭代器的测试代码
迭代器的测试代码如下:
#[test] | |
fn iter() {let list = List::new().append(1).append(2).append(3); | |
let mut iter = list.iter(); | |
assert_eq!(iter.next(), Some(&3)); | |
assert_eq!(iter.next(), Some(&2)); | |
assert_eq!(iter.next(), Some(&1));} |
完整代码
本节的所有完整代码如下:
use std::rc::Rc; | |
pub struct List<T> { | |
head: Link<T>, | |
} | |
type Link<T> = Option<Rc<Node<T>>>; | |
struct Node<T> { | |
elem: T, | |
next: Link<T>, | |
} | |
impl<T> List<T> { | |
pub fn new() -> Self { | |
List { head: None }} | |
pub fn append(&mut self, elem: T) -> List<T> { | |
List { head: Some(Rc::new(Node { | |
elem: elem, | |
next: self.head.clone(),}))} | |
} | |
pub fn tail(&self) -> List<T> { | |
List { head: self.head.as_ref().and_then(|node| { | |
node.next.clone()})}} | |
pub fn head(&self) -> Option<&T> { | |
self.head.as_ref().map(|node| &node.elem)} | |
} | |
//实现Iter | |
pub struct Iter<'a, T> { | |
next: Option<&'a Node<T>>, | |
} | |
impl<T> List<T> { | |
pub fn iter(&self) -> Iter<T> { | |
Iter { next: self.head.as_deref() }} | |
} | |
impl<'a, T> Iterator for Iter<'a, T> { | |
type Item = &'a T; | |
fn next(&mut self) -> Option<Self::Item> { | |
self.next.map(|node| { | |
self.next = node.next.as_deref();&node.elem | |
})} | |
} | |
mod test { | |
use super::List; | |
fn basics() {let mut list = List::new(); | |
assert_eq!(list.head(), None); | |
let list = list.append(1).append(2).append(3); | |
assert_eq!(list.head(), Some(&3)); | |
let list = list.tail(); | |
assert_eq!(list.head(), Some(&2)); | |
let list = list.tail(); | |
assert_eq!(list.head(), Some(&1)); | |
let list = list.tail(); | |
assert_eq!(list.head(), None);} | |
fn iter() {let list = List::new().append(1).append(2).append(3); | |
let mut iter = list.iter(); | |
assert_eq!(iter.next(), Some(&3)); | |
assert_eq!(iter.next(), Some(&2)); | |
assert_eq!(iter.next(), Some(&1));} | |
} |