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

Method remove

publication/code/chapter04/lvec.rs:108–127  ·  view source on GitHub ↗
(&mut self, index: usize)

Source from the content-addressed store, hash-verified

106 }
107
108 fn remove(&mut self, index: usize) -> Option<T> {
109 if index >= self.size { return None; }
110
111 // 分两种情况删除节点,首节点删除最好处理
112 let mut node;
113 if 0 == index {
114 node = self.head.take().unwrap();
115 self.head = node.next.take();
116 } else { // 非首节点需要找到待删除节点,并处理前后链接
117 let mut curr = self.head.as_mut().unwrap();
118 for _i in 0..index - 1 {
119 curr = curr.next.as_mut().unwrap();
120 }
121 node = curr.next.take().unwrap();
122 curr.next = node.next.take();
123 }
124 self.size -= 1;
125
126 Some(node.elem)
127 }
128
129 // 以下是为栈实现的迭代功能
130 // into_iter: 栈改变,成为迭代器

Callers 11

merge_collapseMethod · 0.45
merge_force_collapseMethod · 0.45
merge_collapseMethod · 0.45
merge_force_collapseMethod · 0.45
popMethod · 0.45
nextMethod · 0.45
remove_rearMethod · 0.45
remove_rearMethod · 0.45
nextMethod · 0.45
remove_vertexMethod · 0.45
bfsFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected