(&mut self, index: usize)
| 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: 栈改变,成为迭代器 |
no outgoing calls
no test coverage detected