Skip to content

Commit

Permalink
Update variable names in list and my_list
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Oct 9, 2023
1 parent e5f8c93 commit fb55298
Show file tree
Hide file tree
Showing 25 changed files with 967 additions and 967 deletions.
150 changes: 75 additions & 75 deletions codes/c/chapter_array_and_linkedlist/my_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,158 +8,158 @@

/* 列表类简易实现 */
struct myList {
int *nums; // 数组(存储列表元素)
int *arr; // 数组(存储列表元素)
int capacity; // 列表容量
int size; // 列表大小
int extendRatio; // 列表每次扩容的倍数
};

typedef struct myList myList;
typedef struct myList MyList;

void extendCapacity(myList *list);
void extendCapacity(MyList *nums);

/* 构造函数 */
myList *newMyList() {
myList *list = malloc(sizeof(myList));
list->capacity = 10;
list->nums = malloc(sizeof(int) * list->capacity);
list->size = 0;
list->extendRatio = 2;
return list;
MyList *newMyList() {
MyList *nums = malloc(sizeof(MyList));
nums->capacity = 10;
nums->arr = malloc(sizeof(int) * nums->capacity);
nums->size = 0;
nums->extendRatio = 2;
return nums;
}

/* 析构函数 */
void delMyList(myList *list) {
free(list->nums);
free(list);
void delMyList(MyList *nums) {
free(nums->arr);
free(nums);
}

/* 获取列表长度 */
int size(myList *list) {
return list->size;
int size(MyList *nums) {
return nums->size;
}

/* 获取列表容量 */
int capacity(myList *list) {
return list->capacity;
int capacity(MyList *nums) {
return nums->capacity;
}

/* 访问元素 */
int get(myList *list, int index) {
assert(index >= 0 && index < list->size);
return list->nums[index];
int get(MyList *nums, int index) {
assert(index >= 0 && index < nums->size);
return nums->arr[index];
}

/* 更新元素 */
void set(myList *list, int index, int num) {
assert(index >= 0 && index < list->size);
list->nums[index] = num;
void set(MyList *nums, int index, int num) {
assert(index >= 0 && index < nums->size);
nums->arr[index] = num;
}

/* 尾部添加元素 */
void add(myList *list, int num) {
if (size(list) == capacity(list)) {
extendCapacity(list); // 扩容
void add(MyList *nums, int num) {
if (size(nums) == capacity(nums)) {
extendCapacity(nums); // 扩容
}
list->nums[size(list)] = num;
list->size++;
nums->arr[size(nums)] = num;
nums->size++;
}

/* 中间插入元素 */
void insert(myList *list, int index, int num) {
assert(index >= 0 && index < size(list));
void insert(MyList *nums, int index, int num) {
assert(index >= 0 && index < size(nums));
// 元素数量超出容量时,触发扩容机制
if (size(list) == capacity(list)) {
extendCapacity(list); // 扩容
if (size(nums) == capacity(nums)) {
extendCapacity(nums); // 扩容
}
for (int i = size(list); i > index; --i) {
list->nums[i] = list->nums[i - 1];
for (int i = size(nums); i > index; --i) {
nums->arr[i] = nums->arr[i - 1];
}
list->nums[index] = num;
list->size++;
nums->arr[index] = num;
nums->size++;
}

/* 删除元素 */
// 注意:stdio.h 占用了 remove 关键词
int removeNum(myList *list, int index) {
assert(index >= 0 && index < size(list));
int num = list->nums[index];
for (int i = index; i < size(list) - 1; i++) {
list->nums[i] = list->nums[i + 1];
int removeNum(MyList *nums, int index) {
assert(index >= 0 && index < size(nums));
int num = nums->arr[index];
for (int i = index; i < size(nums) - 1; i++) {
nums->arr[i] = nums->arr[i + 1];
}
list->size--;
nums->size--;
return num;
}

/* 列表扩容 */
void extendCapacity(myList *list) {
void extendCapacity(MyList *nums) {
// 先分配空间
int newCapacity = capacity(list) * list->extendRatio;
int newCapacity = capacity(nums) * nums->extendRatio;
int *extend = (int *)malloc(sizeof(int) * newCapacity);
int *temp = list->nums;
int *temp = nums->arr;

// 拷贝旧数据到新数据
for (int i = 0; i < size(list); i++)
extend[i] = list->nums[i];
for (int i = 0; i < size(nums); i++)
extend[i] = nums->arr[i];

// 释放旧数据
free(temp);

// 更新新数据
list->nums = extend;
list->capacity = newCapacity;
nums->arr = extend;
nums->capacity = newCapacity;
}

/* 将列表转换为 Array 用于打印 */
int *toArray(myList *list) {
return list->nums;
int *toArray(MyList *nums) {
return nums->arr;
}

/* Driver Code */
int main() {
/* 初始化列表 */
myList *list = newMyList();
MyList *nums = newMyList();
/* 尾部添加元素 */
add(list, 1);
add(list, 3);
add(list, 2);
add(list, 5);
add(list, 4);
printf("列表 list = ");
printArray(toArray(list), size(list));
printf("容量 = %d ,长度 = %d\n", capacity(list), size(list));
add(nums, 1);
add(nums, 3);
add(nums, 2);
add(nums, 5);
add(nums, 4);
printf("列表 nums = ");
printArray(toArray(nums), size(nums));
printf("容量 = %d ,长度 = %d\n", capacity(nums), size(nums));

/* 中间插入元素 */
insert(list, 3, 6);
printf("在索引 3 处插入数字 6 ,得到 list = ");
printArray(toArray(list), size(list));
insert(nums, 3, 6);
printf("在索引 3 处插入数字 6 ,得到 nums = ");
printArray(toArray(nums), size(nums));

/* 删除元素 */
removeNum(list, 3);
printf("删除索引 3 处的元素,得到 list = ");
printArray(toArray(list), size(list));
removeNum(nums, 3);
printf("删除索引 3 处的元素,得到 nums = ");
printArray(toArray(nums), size(nums));

/* 访问元素 */
int num = get(list, 1);
int num = get(nums, 1);
printf("访问索引 1 处的元素,得到 num = %d\n", num);

/* 更新元素 */
set(list, 1, 0);
printf("将索引 1 处的元素更新为 0 ,得到 list = ");
printArray(toArray(list), size(list));
set(nums, 1, 0);
printf("将索引 1 处的元素更新为 0 ,得到 nums = ");
printArray(toArray(nums), size(nums));

/* 测试扩容机制 */
for (int i = 0; i < 10; i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
add(list, i);
add(nums, i);
}

printf("扩容后的列表 list = ");
printArray(toArray(list), size(list));
printf("容量 = %d ,长度 = %d\n", capacity(list), size(list));
printf("扩容后的列表 nums = ");
printArray(toArray(nums), size(nums));
printf("容量 = %d ,长度 = %d\n", capacity(nums), size(nums));

/* 释放分配内存 */
delMyList(list);
delMyList(nums);

return 0;
}
66 changes: 33 additions & 33 deletions codes/cpp/chapter_array_and_linkedlist/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading

0 comments on commit fb55298

Please sign in to comment.