- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 266字
- 2021-07-02 14:11:52
Doubly linked list
The transaction log of the previous section is due for an upgrade. The product team wants to enable users to be able to examine the log by going through it forward and backward to see what each step does. This is bad news for the regular linked list, as it's really inefficient to go anywhere other than forward. So, how is this rectified?
It is rectified using the doubly linked list. The doubly linked list introduces the link back. While this sounds like a minor change, it allows to work on that list backward as well as forward, which significantly improves the ability to look up items. By augmenting the previous singly linked list item with a back pointer, the doubly linked list is almost created:
#[derive(Debug, Clone)]
struct Node {
value: String,
next: Link,
prev: Link,
}
type Link = Option<Rc<RefCell<Node>>>;
#[derive(Debug, Clone)]
pub struct BetterTransactionLog {
head: Link,
tail: Link,
pub length: u64,
}
Similar to the singly linked list, the list itself only consists of a head and a tail pointer, which makes accessing either end of the list cheap and easy. Additionally, the nodes now also feature a pointer back to the preceding node, making the list look like this:

This is also the point that makes the doubly linked list tricky in Rust. The ownership principle is great if there is a hierarchy of ownership: a customer has an address, a text file has several lines of text, and so on. However, a node in a doubly linked list doesn't have clear ownership of either of its neighbors.
- 數(shù)據(jù)庫基礎(chǔ)與應(yīng)用:Access 2010
- 算法競賽入門經(jīng)典:習(xí)題與解答
- Python數(shù)據(jù)分析、挖掘與可視化從入門到精通
- 大數(shù)據(jù)算法
- Lean Mobile App Development
- Python醫(yī)學(xué)數(shù)據(jù)分析入門
- 中國數(shù)字流域
- 圖數(shù)據(jù)實戰(zhàn):用圖思維和圖技術(shù)解決復(fù)雜問題
- TextMate How-to
- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- 數(shù)據(jù)庫技術(shù)及應(yīng)用
- 改變未來的九大算法
- 云工作時代:科技進化必將帶來的新工作方式
- MySQL性能調(diào)優(yōu)與架構(gòu)設(shè)計
- Arquillian Testing Guide