MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / remove

Method remove

code/chapter03/lvec.rs:103–122  ·  view source on GitHub ↗
(&mut self, index: usize)

Source from the content-addressed store, hash-verified

101 }
102
103 fn remove(&mut self, index: usize) -> Option<T> {
104 if index >= self.size { return None; }
105
106 // 分两种情况删除节点,首节点删除最好处理
107 let mut node;
108 if 0 == index {
109 node = self.head.take().unwrap();
110 self.head = node.next.take();
111 } else { // 非首节点需要找到待删除节点,并处理前后链接
112 let mut curr = self.head.as_mut().unwrap();
113 for _i in 0..index-1 {
114 curr = curr.next.as_mut().unwrap();
115 }
116 node = curr.next.take().unwrap();
117 curr.next = node.next.take();
118 }
119 self.size -= 1;
120
121 Some(node.elem)
122 }
123
124 // 打印 LVec,当然也可以实习 ToString 特性
125 fn print_lvec(&self) {

Callers 4

popMethod · 0.45
mainFunction · 0.45
remove_rearMethod · 0.45
remove_rearMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected