mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 02:03:10 +08:00
build
This commit is contained in:
@ -1815,16 +1815,16 @@ comments: true
|
||||
#[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,
|
||||
@ -1847,13 +1847,17 @@ comments: true
|
||||
/* 访问元素 */
|
||||
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;
|
||||
}
|
||||
|
||||
@ -1870,7 +1874,9 @@ comments: true
|
||||
|
||||
/* 在中间插入元素 */
|
||||
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();
|
||||
@ -1886,7 +1892,9 @@ comments: true
|
||||
|
||||
/* 删除元素 */
|
||||
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