mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 01:06:28 +08:00
53d91db10d
* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element. * fix(codes/cpp): Fix access error when printArray(arr, 0) * Update PrintUtil.hpp * fix(codes/c): Fix some errors of cmake build * feat(codes/c): Add hashing_search.c * styles(codes/c): Modify function description * styles(codes/c): Modify binary_search.c code style * fix(codes/c): Fix the problem in binary_tree_bfs.c and the problem that the memory is not released. * feat: Add preorder_traversal_i_compact.c * feat(codes/c): Add head_sort.c * feat(codes/c): Add bucket_sort.c * feat(codes/c): Add binary_search_edge.c * fix(codes/c): Add programs that are not managed by cmake (c code) * feat(codes/c): Add selection_sort.c * style(codes/c): Change swap in selection_sort.c to `selectionSort` * styles(codes/c): Change style. * fix(codes/c): Fix some formatting errors and temporarily remove backtracking chapters * fix(codes/c): Fix space_complexity.c build error. * feat(codes/c): Add array_binary_tree.c * feat(code/c): Update push_back and pop_back in vector.h * styles(codes/c): Adjust format. * feat(codes/c): Add `interation.c ` `recursion.c` `simple_hash.c` `binary_search_edge.c` `binary_search_insertion.c` in C codes. * fix(mylist.c): Fix `insert` function in `mylist.c` https://github.com/krahets/hello-algo/discussions/32#discussioncomment-6974163 * fix(array.c): Fix unreleased memory error --------- Co-authored-by: Yudong Jin <krahets@163.com>
114 lines
2.9 KiB
C
114 lines
2.9 KiB
C
/**
|
||
* File: array.c
|
||
* Created Time: 2022-12-20
|
||
* Author: MolDuM (moldum@163.com)
|
||
*/
|
||
|
||
#include "../utils/common.h"
|
||
|
||
/* 随机访问元素 */
|
||
int randomAccess(int *nums, int size) {
|
||
// 在区间 [0, size) 中随机抽取一个数字
|
||
int randomIndex = rand() % size;
|
||
// 获取并返回随机元素
|
||
int randomNum = nums[randomIndex];
|
||
return randomNum;
|
||
}
|
||
|
||
/* 扩展数组长度 */
|
||
int *extend(int *nums, int size, int enlarge) {
|
||
// 初始化一个扩展长度后的数组
|
||
int *res = (int *)malloc(sizeof(int) * (size + enlarge));
|
||
// 将原数组中的所有元素复制到新数组
|
||
for (int i = 0; i < size; i++) {
|
||
res[i] = nums[i];
|
||
}
|
||
// 初始化扩展后的空间
|
||
for (int i = size; i < size + enlarge; i++) {
|
||
res[i] = 0;
|
||
}
|
||
// 返回扩展后的新数组
|
||
return res;
|
||
}
|
||
|
||
/* 在数组的索引 index 处插入元素 num */
|
||
void insert(int *nums, int size, int num, int index) {
|
||
// 把索引 index 以及之后的所有元素向后移动一位
|
||
for (int i = size - 1; i > index; i--) {
|
||
nums[i] = nums[i - 1];
|
||
}
|
||
// 将 num 赋给 index 处元素
|
||
nums[index] = num;
|
||
}
|
||
|
||
/* 删除索引 index 处元素 */
|
||
// 注意:stdio.h 占用了 remove 关键词
|
||
void removeItem(int *nums, int size, int index) {
|
||
// 把索引 index 之后的所有元素向前移动一位
|
||
for (int i = index; i < size - 1; i++) {
|
||
nums[i] = nums[i + 1];
|
||
}
|
||
}
|
||
|
||
/* 遍历数组 */
|
||
void traverse(int *nums, int size) {
|
||
int count = 0;
|
||
// 通过索引遍历数组
|
||
for (int i = 0; i < size; i++) {
|
||
count++;
|
||
}
|
||
}
|
||
|
||
/* 在数组中查找指定元素 */
|
||
int find(int *nums, int size, int target) {
|
||
for (int i = 0; i < size; i++) {
|
||
if (nums[i] == target)
|
||
return i;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
/* Driver Code */
|
||
int main() {
|
||
/* 初始化数组 */
|
||
int size = 5;
|
||
int arr[5];
|
||
printf("数组 arr = ");
|
||
printArray(arr, size);
|
||
|
||
int nums[5] = {1, 3, 2, 5, 4};
|
||
printf("数组 nums = ");
|
||
printArray(nums, size);
|
||
|
||
/* 随机访问 */
|
||
int randomNum = randomAccess(nums, size);
|
||
printf("在 nums 中获取随机元素 %d", randomNum);
|
||
|
||
/* 长度扩展 */
|
||
int enlarge = 3;
|
||
int *res = extend(nums, size, enlarge);
|
||
size += enlarge;
|
||
printf("将数组长度扩展至 8 ,得到 nums = ");
|
||
printArray(res, size);
|
||
|
||
/* 插入元素 */
|
||
insert(res, size, 6, 3);
|
||
printf("在索引 3 处插入数字 6 ,得到 nums = ");
|
||
printArray(res, size);
|
||
|
||
/* 删除元素 */
|
||
removeItem(res, size, 2);
|
||
printf("删除索引 2 处的元素,得到 nums = ");
|
||
printArray(res, size);
|
||
|
||
/* 遍历数组 */
|
||
traverse(res, size);
|
||
|
||
/* 查找元素 */
|
||
int index = find(res, size, 3);
|
||
printf("在 res 中查找元素 3 ,得到索引 = %d\n", index);
|
||
|
||
/* 释放内存 */
|
||
free(res);
|
||
return 0;
|
||
}
|