【问题标题】:What would be a better way to implement .pop() in my single linked list in Rust?在我的 Rust 单链表中实现 .pop() 的更好方法是什么?
【发布时间】:2019-07-30 09:59:46
【问题描述】:

我已经在 Rust 中实现了我自己的单链表版本,这是我学习它的挑战之一,我对除了 .pop() 方法之外的所有东西都很满意。使用 2 个 while 循环非常丑陋且效率低下,但我发现没有其他方法可以解决将索引 len() - 2 处的节点设置为 None (弹出列表)并使用索引处节点的数据的问题len() - 1 表示 Some(data) 返回值(返回被弹出的元素)。

GitHub Link

pub struct SimpleLinkedList<T> {
    head: Option<Box<Node<T>>>,
}

struct Node<T> {
    data: T,
    next: Option<Box<Node<T>>>,
}

impl<T> Default for SimpleLinkedList<T> {
    fn default() -> Self {
        SimpleLinkedList { head: None }
    }
}

impl<T: Copy> Clone for SimpleLinkedList<T> {
    fn clone(&self) -> SimpleLinkedList<T> {
        let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
        let mut cur = &self.head;
        while let Some(node) = cur {
            cur = &node.next;
            out.push(node.data)
        }
        out
    }
}

impl<T> SimpleLinkedList<T> {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn len(&self) -> usize {
        let mut c = 0;
        let mut cur = &self.head;
        while let Some(node) = cur {
            cur = &node.next;
            c += 1;
        }
        c
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn push(&mut self, _element: T) {
        let mut cur = &mut self.head;
        match cur {
            Some(_) => {
                while let Some(node) = cur {
                    cur = &mut node.next;
                }
            }
            None => (),
        }
        *cur = Some(Box::from(Node {
            data: _element,
            next: None,
        }));
    }

    pub fn pop(&mut self) -> Option<T>
    where
        T: Copy,
    {
        let length = &self.len();
        let mut cur = &mut self.head;
        let mut out = None;
        match cur {
            Some(_) if *length > 1usize => {
                let mut c = 0usize;
                while let Some(node) = cur {
                    cur = &mut node.next;
                    if c >= length - 1 {
                        out = Some(node.data);
                        break;
                    }
                    c += 1;
                }

                c = 0usize;
                cur = &mut self.head;
                while let Some(node) = cur {
                    cur = &mut node.next;
                    if c == length - 2 {
                        break;
                    }
                    c += 1;
                }
            }
            Some(node) => out = Some(node.data),
            None => (),
        }
        *cur = None;
        out
    }

    pub fn peek(&self) -> Option<&T> {
        let cur = &self.head;
        match cur {
            Some(node) => Some(&node.data),
            None => None,
        }
    }
}

impl<T: Copy> SimpleLinkedList<T> {
    pub fn rev(&self) -> SimpleLinkedList<T> {
        let mut clone = self.clone();
        let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
        while let Some(val) = clone.pop() {
            out.push(val)
        }
        out
    }
}

impl<'a, T: Copy> From<&'a [T]> for SimpleLinkedList<T> {
    fn from(_item: &[T]) -> Self {
        let mut out: SimpleLinkedList<T> = SimpleLinkedList::new();
        for &e in _item.iter() {
            out.push(e);
        }
        out
    }
}

impl<T> Into<Vec<T>> for SimpleLinkedList<T> {
    fn into(self) -> Vec<T> {
        let mut out: Vec<T> = Vec::new();
        let mut cur = self.head;
        while let Some(node) = cur {
            cur = node.next;
            out.push(node.data)
        }
        out
    }
}

【问题讨论】:

  • 链表是表示数据的功能性方式。如果您尝试使用 imperatif 在 Rust 中实现它,Rust 借用检查器将使您的生活变得复杂。见,codereview.stackexchange.com/questions/207418/…。使用功能样式,一切都更干净,当然可能不会更快,我认为 rust 链表需要不安全才能使其快速(且可读)。
  • 我投票结束这个问题,因为我已经回答了here
  • @E_net4 这看起来很棒!在接下来的几天里,我一定会抽出时间阅读它
  • 感谢您发布包含您的测试的链接。

标签: linked-list rust singly-linked-list


【解决方案1】:

您可以通过跟踪您看到的最后一个元素(然后在最后更新它)来避免重新遍历列表。

如果你对如何做到这一点太天真,你就会遇到麻烦;您的“先前”指针保留列表其余部分的所有权,借用检查器不允许这样做。诀窍是随时断开该链接 - 为此,您可以使用 mem::replace 函数。完成此操作后,您必须在再次丢失之前的节点之前将其放回原处。

这就是它的完整外观(你必须原谅我对unwrap 的随意使用——我确实认为它让事情变得更清楚了):

pub fn pop(&mut self) -> Option<T>
    where T : Copy,
{
    use std::mem::replace;

    let curr = replace(&mut self.head, None);

    if curr.is_none() { // list started off empty; nothing to pop
        return None;
    }

    let mut curr = curr.unwrap(); // safe because of the check above

    if let None = curr.next { // popped the last element
        return Some(curr.data);
    }

    let mut prev_next = &mut self.head;

    while curr.next.is_some() {
        // Take ownership of the next element
        let nnext = replace(&mut curr.next, None).unwrap();

        // Update the previous element's "next" field
        *prev_next = Some(curr);

        // Progress to the next element
        curr = nnext;

        // Progress our pointer to the previous element's "next" field
        prev_next = &mut prev_next.as_mut().unwrap().next;

    }

    return Some(curr.data);
}

顺便说一句,如果您愿意稍微更改接口以便我们每次返回一个“新”列表(在pop 函数中获得所有权),或者使用持久数据结构,就像他们在Learning Rust with entirely too many linked lists 中所做的那样(已经在评论中提到):

pub fn pop_replace(self) -> (Option<T>, Self) {
    // freely mutate self and all the nodes
}

您会使用如下:

let elem, list = list.pop();

【讨论】:

    【解决方案2】:

    inspired from here

    fn pop(&mut self) -> Option<T> {
            let mut current: &mut Option<Box<Node<T>>> = &mut self.head;
            loop {
                // println!("curr: {:?}", current);
                match current {
                    None => {
                        return None;
                    }
                    Some(node) if node.next.is_none() => {
                        let val = node.data;
                        *current = node.next.take();
                        return Some(val);
                    }
                    Some(ref mut node) => {
                        current = &mut node.next;
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2023-03-09
      • 2016-07-27
      • 2014-10-27
      • 1970-01-01
      相关资源
      最近更新 更多