mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-16 14:14:03 +08:00
@@ -19,8 +19,7 @@ struct MyList {
|
|||||||
impl MyList {
|
impl MyList {
|
||||||
/* 构造方法 */
|
/* 构造方法 */
|
||||||
pub fn new(capacity: usize) -> Self {
|
pub fn new(capacity: usize) -> Self {
|
||||||
let mut vec = Vec::new();
|
let mut vec = vec![0; capacity];
|
||||||
vec.resize(capacity, 0);
|
|
||||||
Self {
|
Self {
|
||||||
arr: vec,
|
arr: vec,
|
||||||
capacity,
|
capacity,
|
||||||
@@ -92,7 +91,7 @@ impl MyList {
|
|||||||
};
|
};
|
||||||
let num = self.arr[index];
|
let num = self.arr[index];
|
||||||
// 将将索引 index 之后的元素都向前移动一位
|
// 将将索引 index 之后的元素都向前移动一位
|
||||||
for j in (index..self.size - 1) {
|
for j in index..self.size - 1 {
|
||||||
self.arr[j] = self.arr[j + 1];
|
self.arr[j] = self.arr[j + 1];
|
||||||
}
|
}
|
||||||
// 更新元素数量
|
// 更新元素数量
|
||||||
@@ -111,7 +110,7 @@ impl MyList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 将列表转换为数组 */
|
/* 将列表转换为数组 */
|
||||||
pub fn to_array(&mut self) -> Vec<i32> {
|
pub fn to_array(&self) -> Vec<i32> {
|
||||||
// 仅转换有效长度范围内的列表元素
|
// 仅转换有效长度范围内的列表元素
|
||||||
let mut arr = Vec::new();
|
let mut arr = Vec::new();
|
||||||
for i in 0..self.size {
|
for i in 0..self.size {
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.que_size -= 1; // 更新队列长度
|
self.que_size -= 1; // 更新队列长度
|
||||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
old_front.borrow().val
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// 队尾出队操作
|
// 队尾出队操作
|
||||||
@@ -136,7 +136,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.que_size -= 1; // 更新队列长度
|
self.que_size -= 1; // 更新队列长度
|
||||||
Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val
|
old_rear.borrow().val
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,12 +163,16 @@ impl<T: Copy> LinkedListDeque<T> {
|
|||||||
|
|
||||||
/* 返回数组用于打印 */
|
/* 返回数组用于打印 */
|
||||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||||
if let Some(node) = head {
|
let mut res: Vec<T> = Vec::new();
|
||||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
|
||||||
nums.insert(0, node.borrow().val);
|
if let Some(cur) = cur {
|
||||||
return nums;
|
res.push(cur.borrow().val);
|
||||||
|
recur(cur.borrow().next.as_ref(), res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Vec::new();
|
|
||||||
|
recur(head, &mut res);
|
||||||
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ impl<T: Copy> LinkedListQueue<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.que_size -= 1;
|
self.que_size -= 1;
|
||||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
old_front.borrow().val
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,12 +78,18 @@ impl<T: Copy> LinkedListQueue<T> {
|
|||||||
|
|
||||||
/* 将链表转化为 Array 并返回 */
|
/* 将链表转化为 Array 并返回 */
|
||||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||||
if let Some(node) = head {
|
let mut res: Vec<T> = Vec::new();
|
||||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
|
||||||
nums.insert(0, node.borrow().val);
|
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
|
||||||
return nums;
|
if let Some(cur) = cur {
|
||||||
|
res.push(cur.borrow().val);
|
||||||
|
recur(cur.borrow().next.as_ref(), res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Vec::new();
|
|
||||||
|
recur(head, &mut res);
|
||||||
|
|
||||||
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,16 +45,10 @@ impl<T: Copy> LinkedListStack<T> {
|
|||||||
/* 出栈 */
|
/* 出栈 */
|
||||||
pub fn pop(&mut self) -> Option<T> {
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
self.stack_peek.take().map(|old_head| {
|
self.stack_peek.take().map(|old_head| {
|
||||||
match old_head.borrow_mut().next.take() {
|
self.stack_peek = old_head.borrow_mut().next.take();
|
||||||
Some(new_head) => {
|
|
||||||
self.stack_peek = Some(new_head);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
self.stack_peek = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.stk_size -= 1;
|
self.stk_size -= 1;
|
||||||
Rc::try_unwrap(old_head).ok().unwrap().into_inner().val
|
|
||||||
|
old_head.borrow().val
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user