mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-25 19:22:27 +08:00
build
This commit is contained in:
@ -1785,16 +1785,16 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
#[allow(dead_code)]
|
||||
struct MyList {
|
||||
arr: Vec<i32>, // 数组(存储列表元素)
|
||||
capacity: usize, // 列表容量
|
||||
size: usize, // 列表长度(当前元素数量)
|
||||
extend_ratio: usize, // 每次列表扩容的倍数
|
||||
capacity: usize, // 列表容量
|
||||
size: usize, // 列表长度(当前元素数量)
|
||||
extend_ratio: usize, // 每次列表扩容的倍数
|
||||
}
|
||||
|
||||
#[allow(unused,unused_comparisons)]
|
||||
#[allow(unused, unused_comparisons)]
|
||||
impl MyList {
|
||||
/* 构造方法 */
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let mut vec = Vec::new();
|
||||
let mut vec = Vec::new();
|
||||
vec.resize(capacity, 0);
|
||||
Self {
|
||||
arr: vec,
|
||||
@ -1817,13 +1817,17 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
/* 访问元素 */
|
||||
pub fn get(&self, index: usize) -> i32 {
|
||||
// 索引如果越界,则抛出异常,下同
|
||||
if index >= self.size {panic!("索引越界")};
|
||||
if index >= self.size {
|
||||
panic!("索引越界")
|
||||
};
|
||||
return self.arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
pub fn set(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size {panic!("索引越界")};
|
||||
if index >= self.size {
|
||||
panic!("索引越界")
|
||||
};
|
||||
self.arr[index] = num;
|
||||
}
|
||||
|
||||
@ -1840,7 +1844,9 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
|
||||
/* 在中间插入元素 */
|
||||
pub fn insert(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
if index >= self.size() {
|
||||
panic!("索引越界")
|
||||
};
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if self.size == self.capacity() {
|
||||
self.extend_capacity();
|
||||
@ -1856,7 +1862,9 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
|
||||
/* 删除元素 */
|
||||
pub fn remove(&mut self, index: usize) -> i32 {
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
if index >= self.size() {
|
||||
panic!("索引越界")
|
||||
};
|
||||
let num = self.arr[index];
|
||||
// 将将索引 index 之后的元素都向前移动一位
|
||||
for j in (index..self.size - 1) {
|
||||
|
Reference in New Issue
Block a user