mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 20:12:07 +08:00
Update variable names in list and my_list
This commit is contained in:
@ -9,65 +9,65 @@
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
vector<int> list = {1, 3, 2, 5, 4};
|
||||
cout << "列表 list = ";
|
||||
printVector(list);
|
||||
vector<int> nums = {1, 3, 2, 5, 4};
|
||||
cout << "列表 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list[1];
|
||||
cout << "访问索引 1 处的元素,得到 num = " << num << endl;
|
||||
int x = nums[1];
|
||||
cout << "访问索引 1 处的元素,得到 x = " << x << endl;
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
cout << "将索引 1 处的元素更新为 0 ,得到 list = ";
|
||||
printVector(list);
|
||||
nums[1] = 0;
|
||||
cout << "将索引 1 处的元素更新为 0 ,得到 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
cout << "清空列表后 list = ";
|
||||
printVector(list);
|
||||
nums.clear();
|
||||
cout << "清空列表后 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.push_back(1);
|
||||
list.push_back(3);
|
||||
list.push_back(2);
|
||||
list.push_back(5);
|
||||
list.push_back(4);
|
||||
cout << "添加元素后 list = ";
|
||||
printVector(list);
|
||||
nums.push_back(1);
|
||||
nums.push_back(3);
|
||||
nums.push_back(2);
|
||||
nums.push_back(5);
|
||||
nums.push_back(4);
|
||||
cout << "添加元素后 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(list.begin() + 3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 list = ";
|
||||
printVector(list);
|
||||
nums.insert(nums.begin() + 3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 删除元素 */
|
||||
list.erase(list.begin() + 3);
|
||||
cout << "删除索引 3 处的元素,得到 list = ";
|
||||
printVector(list);
|
||||
nums.erase(nums.begin() + 3);
|
||||
cout << "删除索引 3 处的元素,得到 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int n : list) {
|
||||
for (int n : nums) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
vector<int> list1 = {6, 8, 7, 10, 9};
|
||||
list.insert(list.end(), list1.begin(), list1.end());
|
||||
cout << "将列表 list1 拼接到 list 之后,得到 list = ";
|
||||
printVector(list);
|
||||
vector<int> nums1 = {6, 8, 7, 10, 9};
|
||||
nums.insert(nums.end(), nums1.begin(), nums1.end());
|
||||
cout << "将列表 nums1 拼接到 nums 之后,得到 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 排序列表 */
|
||||
sort(list.begin(), list.end());
|
||||
cout << "排序列表后 list = ";
|
||||
printVector(list);
|
||||
sort(nums.begin(), nums.end());
|
||||
cout << "排序列表后 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -9,30 +9,30 @@
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
private:
|
||||
int *nums; // 数组(存储列表元素)
|
||||
int numsCapacity = 10; // 列表容量
|
||||
int numsSize = 0; // 列表长度(即当前元素数量)
|
||||
int *arr; // 数组(存储列表元素)
|
||||
int arrCapacity = 10; // 列表容量
|
||||
int arrSize = 0; // 列表长度(即当前元素数量)
|
||||
int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
public:
|
||||
/* 构造方法 */
|
||||
MyList() {
|
||||
nums = new int[numsCapacity];
|
||||
arr = new int[arrCapacity];
|
||||
}
|
||||
|
||||
/* 析构方法 */
|
||||
~MyList() {
|
||||
delete[] nums;
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
int size() {
|
||||
return numsSize;
|
||||
return arrSize;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
int capacity() {
|
||||
return numsCapacity;
|
||||
return arrCapacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
@ -40,14 +40,14 @@ class MyList {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
return nums[index];
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
void set(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
nums[index] = num;
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
@ -55,9 +55,9 @@ class MyList {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
nums[size()] = num;
|
||||
arr[size()] = num;
|
||||
// 更新元素数量
|
||||
numsSize++;
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
@ -69,24 +69,24 @@ class MyList {
|
||||
extendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size() - 1; j >= index; j--) {
|
||||
nums[j + 1] = nums[j];
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
arr[index] = num;
|
||||
// 更新元素数量
|
||||
numsSize++;
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
int remove(int index) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
int num = nums[index];
|
||||
int num = arr[index];
|
||||
// 索引 i 之后的元素都向前移动一位
|
||||
for (int j = index; j < size() - 1; j++) {
|
||||
nums[j] = nums[j + 1];
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
numsSize--;
|
||||
arrSize--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
@ -95,15 +95,15 @@ class MyList {
|
||||
void extendCapacity() {
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组
|
||||
int newCapacity = capacity() * extendRatio;
|
||||
int *tmp = nums;
|
||||
nums = new int[newCapacity];
|
||||
int *tmp = arr;
|
||||
arr = new int[newCapacity];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < size(); i++) {
|
||||
nums[i] = tmp[i];
|
||||
arr[i] = tmp[i];
|
||||
}
|
||||
// 释放内存
|
||||
delete[] tmp;
|
||||
numsCapacity = newCapacity;
|
||||
arrCapacity = newCapacity;
|
||||
}
|
||||
|
||||
/* 将列表转换为 Vector 用于打印 */
|
||||
@ -111,7 +111,7 @@ class MyList {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
vector<int> vec(size());
|
||||
for (int i = 0; i < size(); i++) {
|
||||
vec[i] = nums[i];
|
||||
vec[i] = arr[i];
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
@ -120,52 +120,52 @@ class MyList {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
MyList *list = new MyList();
|
||||
MyList *nums = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
list->add(1);
|
||||
list->add(3);
|
||||
list->add(2);
|
||||
list->add(5);
|
||||
list->add(4);
|
||||
cout << "列表 list = ";
|
||||
vector<int> vec = list->toVector();
|
||||
nums->add(1);
|
||||
nums->add(3);
|
||||
nums->add(2);
|
||||
nums->add(5);
|
||||
nums->add(4);
|
||||
cout << "列表 nums = ";
|
||||
vector<int> vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "容量 = " << list->capacity() << " ,长度 = " << list->size() << endl;
|
||||
cout << "容量 = " << nums->capacity() << " ,长度 = " << nums->size() << endl;
|
||||
|
||||
/* 中间插入元素 */
|
||||
list->insert(3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 list = ";
|
||||
vec = list->toVector();
|
||||
nums->insert(3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 删除元素 */
|
||||
list->remove(3);
|
||||
cout << "删除索引 3 处的元素,得到 list = ";
|
||||
vec = list->toVector();
|
||||
nums->remove(3);
|
||||
cout << "删除索引 3 处的元素,得到 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list->get(1);
|
||||
int num = nums->get(1);
|
||||
cout << "访问索引 1 处的元素,得到 num = " << num << endl;
|
||||
|
||||
/* 更新元素 */
|
||||
list->set(1, 0);
|
||||
cout << "将索引 1 处的元素更新为 0 ,得到 list = ";
|
||||
vec = list->toVector();
|
||||
nums->set(1, 0);
|
||||
cout << "将索引 1 处的元素更新为 0 ,得到 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list->add(i);
|
||||
nums->add(i);
|
||||
}
|
||||
cout << "扩容后的列表 list = ";
|
||||
vec = list->toVector();
|
||||
cout << "扩容后的列表 nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "容量 = " << list->capacity() << " ,长度 = " << list->size() << endl;
|
||||
cout << "容量 = " << nums->capacity() << " ,长度 = " << nums->size() << endl;
|
||||
|
||||
// 释放内存
|
||||
delete list;
|
||||
delete nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user