rust数据结构双链表实现

use std::cell::RefCell;
use std::rc::{Rc, Weak};

type Link<T> = Option<Rc<RefCell<Node<T>>>>;

#[derive(Debug)]
struct Node<T> {
    value: T,
    prev: Option<Weak<RefCell<Node<T>>>>,
    next: Link<T>,
}

struct List<T> {
    head: Link<T>,
    tail: Link<T>,
}

impl<T: std::fmt::Debug> List<T> {
    fn new() -> Self {
        Self { head: None, tail: None }
    }

    fn push_back(&mut self, value: T) {
        let new_node = Rc::new(RefCell::new(Node {
            value,
            prev: None,
            next: None,
        }));

        match self.tail.take() {
            Some(old_tail) => {
                old_tail.borrow_mut().next = Some(new_node.clone());
                new_node.borrow_mut().prev = Some(Rc::downgrade(&old_tail));
                self.tail = Some(new_node);
            }
            None => {
                self.head = Some(new_node.clone());
                self.tail = Some(new_node);
            }
        }
    }


    fn pop_back(&mut self) -> Option<T> {
        self.tail.take().map(|old_tail| {

            {
                let mut old_tail_ref = old_tail.borrow_mut();

                match old_tail_ref.prev.take() {
                    Some(prev_weak) => {
                        if let Some(prev) = prev_weak.upgrade() {
                            prev.borrow_mut().next = None;
                            self.tail = Some(prev);
                        }
                    }
                    None => {
                        self.head = None;
                    }
                }
            } // 👈 old_tail_ref 在这里 drop

            println!(
                "Dropping strong={}, weak={}",
                Rc::strong_count(&old_tail),
                Rc::weak_count(&old_tail)
            );

            Rc::try_unwrap(old_tail)
                .ok()
                .unwrap()
                .into_inner()
                .value
        })
    }

    fn print_counts(&self) {
        if let Some(ref head) = self.head {
            println!(
                "HEAD strong={}, weak={}",
                Rc::strong_count(head),
                Rc::weak_count(head)
            );
        }

        if let Some(ref tail) = self.tail {
            println!(
                "TAIL strong={}, weak={}",
                Rc::strong_count(tail),
                Rc::weak_count(tail)
            );
        }
    }
}

fn main() {
    let mut list = List::new();

    println!("== push 1 ==");
    list.push_back(1);
    list.print_counts();

    println!("== push 2 ==");
    list.push_back(2);
    list.print_counts();

    println!("== push 3 ==");
    list.push_back(3);
    list.print_counts();

    println!("== pop ==");
    list.pop_back();
    list.print_counts();

    println!("== pop ==");
    list.pop_back();
    list.print_counts();

    println!("== pop ==");
    list.pop_back();
    list.print_counts();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值