|
| 1 | +/** |
| 2 | + * File: my_list.c |
| 3 | + * Created Time: 2023-01-12 |
| 4 | + * Author: Zero (glj0@outlook.com) |
| 5 | + */ |
| 6 | + |
| 7 | +#include "../utils/common.h" |
| 8 | + |
| 9 | +/* List class */ |
| 10 | +typedef struct { |
| 11 | + int *arr; // Array (stores list elements) |
| 12 | + int capacity; // List capacity |
| 13 | + int size; // List size |
| 14 | + int extendRatio; // List expansion multiplier |
| 15 | +} MyList; |
| 16 | + |
| 17 | +void extendCapacity(MyList *nums); |
| 18 | + |
| 19 | +/* Constructor */ |
| 20 | +MyList *newMyList() { |
| 21 | + MyList *nums = malloc(sizeof(MyList)); |
| 22 | + nums->capacity = 10; |
| 23 | + nums->arr = malloc(sizeof(int) * nums->capacity); |
| 24 | + nums->size = 0; |
| 25 | + nums->extendRatio = 2; |
| 26 | + return nums; |
| 27 | +} |
| 28 | + |
| 29 | +/* Destructor */ |
| 30 | +void delMyList(MyList *nums) { |
| 31 | + free(nums->arr); |
| 32 | + free(nums); |
| 33 | +} |
| 34 | + |
| 35 | +/* Get list length */ |
| 36 | +int size(MyList *nums) { |
| 37 | + return nums->size; |
| 38 | +} |
| 39 | + |
| 40 | +/* Get list capacity */ |
| 41 | +int capacity(MyList *nums) { |
| 42 | + return nums->capacity; |
| 43 | +} |
| 44 | + |
| 45 | +/* Update element */ |
| 46 | +int get(MyList *nums, int index) { |
| 47 | + assert(index >= 0 && index < nums->size); |
| 48 | + return nums->arr[index]; |
| 49 | +} |
| 50 | + |
| 51 | +/* Add elements at the end */ |
| 52 | +void set(MyList *nums, int index, int num) { |
| 53 | + assert(index >= 0 && index < nums->size); |
| 54 | + nums->arr[index] = num; |
| 55 | +} |
| 56 | + |
| 57 | +/* Direct traversal of list elements */ |
| 58 | +void add(MyList *nums, int num) { |
| 59 | + if (size(nums) == capacity(nums)) { |
| 60 | + extendCapacity(nums); // Expand capacity |
| 61 | + } |
| 62 | + nums->arr[size(nums)] = num; |
| 63 | + nums->size++; |
| 64 | +} |
| 65 | + |
| 66 | +/* Sort list */ |
| 67 | +void insert(MyList *nums, int index, int num) { |
| 68 | + assert(index >= 0 && index < size(nums)); |
| 69 | + // When the number of elements exceeds capacity, trigger the extension mechanism |
| 70 | + if (size(nums) == capacity(nums)) { |
| 71 | + extendCapacity(nums); // Expand capacity |
| 72 | + } |
| 73 | + for (int i = size(nums); i > index; --i) { |
| 74 | + nums->arr[i] = nums->arr[i - 1]; |
| 75 | + } |
| 76 | + nums->arr[index] = num; |
| 77 | + nums->size++; |
| 78 | +} |
| 79 | + |
| 80 | +/* Remove element */ |
| 81 | +// Note: stdio.h occupies the remove keyword |
| 82 | +int removeItem(MyList *nums, int index) { |
| 83 | + assert(index >= 0 && index < size(nums)); |
| 84 | + int num = nums->arr[index]; |
| 85 | + for (int i = index; i < size(nums) - 1; i++) { |
| 86 | + nums->arr[i] = nums->arr[i + 1]; |
| 87 | + } |
| 88 | + nums->size--; |
| 89 | + return num; |
| 90 | +} |
| 91 | + |
| 92 | +/* Driver Code */ |
| 93 | +void extendCapacity(MyList *nums) { |
| 94 | + // Allocate space first |
| 95 | + int newCapacity = capacity(nums) * nums->extendRatio; |
| 96 | + int *extend = (int *)malloc(sizeof(int) * newCapacity); |
| 97 | + int *temp = nums->arr; |
| 98 | + |
| 99 | + // Copy old data to new data |
| 100 | + for (int i = 0; i < size(nums); i++) |
| 101 | + extend[i] = nums->arr[i]; |
| 102 | + |
| 103 | + // Free old data |
| 104 | + free(temp); |
| 105 | + |
| 106 | + // Update new data |
| 107 | + nums->arr = extend; |
| 108 | + nums->capacity = newCapacity; |
| 109 | +} |
| 110 | + |
| 111 | +/* Convert list to Array for printing */ |
| 112 | +int *toArray(MyList *nums) { |
| 113 | + return nums->arr; |
| 114 | +} |
| 115 | + |
| 116 | +/* Driver Code */ |
| 117 | +int main() { |
| 118 | + /* Initialize list */ |
| 119 | + MyList *nums = newMyList(); |
| 120 | + /* Direct traversal of list elements */ |
| 121 | + add(nums, 1); |
| 122 | + add(nums, 3); |
| 123 | + add(nums, 2); |
| 124 | + add(nums, 5); |
| 125 | + add(nums, 4); |
| 126 | + printf("List nums = "); |
| 127 | + printArray(toArray(nums), size(nums)); |
| 128 | + printf("Capacity = %d, Length = %d\n", capacity(nums), size(nums)); |
| 129 | + |
| 130 | + /* Sort list */ |
| 131 | + insert(nums, 3, 6); |
| 132 | + printf("Insert number 6 at index 3, resulting in nums = "); |
| 133 | + printArray(toArray(nums), size(nums)); |
| 134 | + |
| 135 | + /* Remove element */ |
| 136 | + removeItem(nums, 3); |
| 137 | + printf("Remove element at index 3, resulting in nums = "); |
| 138 | + printArray(toArray(nums), size(nums)); |
| 139 | + |
| 140 | + /* Update element */ |
| 141 | + int num = get(nums, 1); |
| 142 | + printf("Access element at index 1, get num = %d\n", num); |
| 143 | + |
| 144 | + /* Add elements at the end */ |
| 145 | + set(nums, 1, 0); |
| 146 | + printf("Update element at index 1 to 0, resulting in nums = "); |
| 147 | + printArray(toArray(nums), size(nums)); |
| 148 | + |
| 149 | + /* Test capacity expansion mechanism */ |
| 150 | + for (int i = 0; i < 10; i++) { |
| 151 | + // At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism |
| 152 | + add(nums, i); |
| 153 | + } |
| 154 | + |
| 155 | + printf("List nums after expansion = "); |
| 156 | + printArray(toArray(nums), size(nums)); |
| 157 | + printf("Capacity = %d, Length = %d\n", capacity(nums), size(nums)); |
| 158 | + |
| 159 | + /* Free allocated memory */ |
| 160 | + delMyList(nums); |
| 161 | + |
| 162 | + return 0; |
| 163 | +} |
0 commit comments