Data structures 如何反转单链表并将其转换为向量?

Data structures 如何反转单链表并将其转换为向量?,data-structures,rust,Data Structures,Rust,在编写A*算法时,我尝试反转单个链接的操作列表,并将其打包到Vec中 以下是我的单链表的结构: use std::rc::Rc; struct FrontierElem<A> { prev: Option<Rc<FrontierElem<A>>>, action: A, } 当时我没有找到SliceExt::reverse方法,所以我继续执行第二个计划:从头到尾填充向量。我没有找到安全的方法 /// Copies action

在编写A*算法时,我尝试反转单个链接的操作列表,并将其打包到Vec中

以下是我的单链表的结构:

use std::rc::Rc;

struct FrontierElem<A> {
    prev: Option<Rc<FrontierElem<A>>>,
    action: A,
}
当时我没有找到SliceExt::reverse方法,所以我继续执行第二个计划:从头到尾填充向量。我没有找到安全的方法

/// Copies action fields from single-linked list to vector in reverse order.
/// `fel` stands for first element
fn rev2<A>(fel: &Rc<FrontierElem<A>>) -> Vec<A>
where
    A: Clone,
{
    let mut cnt = 0usize;
    // First pass. Let's find a length of list `fel`
    {
        let mut cur = fel;
        while let Some(ref prev) = cur.prev {
            cnt = cnt + 1;
            cur = prev;
        }
    } // Lexical scoping to unborrow `fel`

    // Second pass. Create and fill `ret` vector
    let mut ret = Vec::<A>::with_capacity(cnt);
    {
        let mut idx = cnt - 1;
        let mut cur = fel;
        // I didn't find safe and fast way to populate vector from the end to the beginning.
        unsafe {
            ret.set_len(cnt); //unsafe. vector values aren't initialized
            while let Some(ref prev) = cur.prev {
                ret[idx] = cur.action.clone();
                idx = idx - 1;
                cur = prev;
            }
        }
        assert_eq!(idx, std::usize::MAX);
    } // Lexical scoping to make `fel` usable again
    ret
}

填充向量,然后使用.as_mut_slice.reverse将其反转


可以将这样的内容作为问题发布,然后回答,但您需要遵循此网站的相应表格:
/// Copies action fields from single-linked list to vector in reverse order.
/// `fel` stands for first element
fn rev2<A>(fel: &Rc<FrontierElem<A>>) -> Vec<A>
where
    A: Clone,
{
    let mut cnt = 0usize;
    // First pass. Let's find a length of list `fel`
    {
        let mut cur = fel;
        while let Some(ref prev) = cur.prev {
            cnt = cnt + 1;
            cur = prev;
        }
    } // Lexical scoping to unborrow `fel`

    // Second pass. Create and fill `ret` vector
    let mut ret = Vec::<A>::with_capacity(cnt);
    {
        let mut idx = cnt - 1;
        let mut cur = fel;
        // I didn't find safe and fast way to populate vector from the end to the beginning.
        unsafe {
            ret.set_len(cnt); //unsafe. vector values aren't initialized
            while let Some(ref prev) = cur.prev {
                ret[idx] = cur.action.clone();
                idx = idx - 1;
                cur = prev;
            }
        }
        assert_eq!(idx, std::usize::MAX);
    } // Lexical scoping to make `fel` usable again
    ret
}
fn rev1<A>(fel: &Rc<FrontierElem<A>>) -> Vec<A>
where
    A: Clone,
{
    let mut cur = fel;
    let mut ret = Vec::new();
    while let Some(ref prev) = cur.prev {
        ret.push(cur.action.clone());
        cur = prev;
    } // First action (where cur.prev==None) is ignored by design
    ret.as_mut_slice().reverse();
    ret
}