/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
Add a node of value val before the index - th node in the linked list . If index equals to the length of linked list the node will be appended to the end of linked list . If index is greater than the length the node will not be inserted .
pub fn add_at_index(&self, index: i32, val: i32) {
        if index > self.len.get() || index < 0 {
            return;
        }
        let p = self.locate(index);
        // 记录左右节点
        let next = p.borrow().next.clone().unwrap();
        let prev = p;
        // 新建节点
        let node = Rc::new(RefCell::new(Node::new(val)));
        node.borrow_mut().prev = Some(Rc::downgrade(&prev));
        node.borrow_mut().next = Some(next.clone());
        // 改变左右节点的 next & prev 指针
        next.borrow_mut().prev = Some(Rc::downgrade(&node));
        prev.borrow_mut().next = Some(node);
        self.len.set(self.len.get() + 1);
    }
pub fn add_at_index ( & self , index : i32 , val : i32 ) { if index > self . len . get ( ) || index < 0 { return ; } let p = self . locate ( index ) ; // 记录左右节点 let next = p . borrow ( ) . next . clone ( ) . unwrap ( ) ; let prev = p ; // 新建节点 let node = Rc :: new ( RefCell :: new ( Node :: new ( val ) ) ) ; node . borrow_mut ( ) . prev = Some ( Rc :: downgrade ( & prev ) ) ; node . borrow_mut ( ) . next = Some ( next . clone ( ) ) ; // 改变左右节点的 next & prev 指针 next . borrow_mut ( ) . prev = Some ( Rc :: downgrade ( & node ) ) ; prev . borrow_mut ( ) . next = Some ( node ) ; self . len . set ( self . len . get ( ) + 1 ) ; }
Add a node of value val before the index-th node in the linked list.
Add a node of value val before the index - th node in the linked list .
&self
index
i32
val
i32