|
|
|
@ -14,13 +14,13 @@ struct myList {
|
|
|
|
|
int extendRatio; // 列表每次扩容的倍数
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct myList MyList;
|
|
|
|
|
typedef struct myList myList;
|
|
|
|
|
|
|
|
|
|
void extendCapacity(MyList *nums);
|
|
|
|
|
void extendCapacity(myList *nums);
|
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
|
|
|
|
MyList *newMyList() {
|
|
|
|
|
MyList *nums = malloc(sizeof(MyList));
|
|
|
|
|
myList *newMyList() {
|
|
|
|
|
myList *nums = malloc(sizeof(myList));
|
|
|
|
|
nums->capacity = 10;
|
|
|
|
|
nums->arr = malloc(sizeof(int) * nums->capacity);
|
|
|
|
|
nums->size = 0;
|
|
|
|
@ -29,35 +29,35 @@ MyList *newMyList() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 析构函数 */
|
|
|
|
|
void delMyList(MyList *nums) {
|
|
|
|
|
void delMyList(myList *nums) {
|
|
|
|
|
free(nums->arr);
|
|
|
|
|
free(nums);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表长度 */
|
|
|
|
|
int size(MyList *nums) {
|
|
|
|
|
int size(myList *nums) {
|
|
|
|
|
return nums->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取列表容量 */
|
|
|
|
|
int capacity(MyList *nums) {
|
|
|
|
|
int capacity(myList *nums) {
|
|
|
|
|
return nums->capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
int get(MyList *nums, int index) {
|
|
|
|
|
int get(myList *nums, int index) {
|
|
|
|
|
assert(index >= 0 && index < nums->size);
|
|
|
|
|
return nums->arr[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 更新元素 */
|
|
|
|
|
void set(MyList *nums, int index, int num) {
|
|
|
|
|
void set(myList *nums, int index, int num) {
|
|
|
|
|
assert(index >= 0 && index < nums->size);
|
|
|
|
|
nums->arr[index] = num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 尾部添加元素 */
|
|
|
|
|
void add(MyList *nums, int num) {
|
|
|
|
|
void add(myList *nums, int num) {
|
|
|
|
|
if (size(nums) == capacity(nums)) {
|
|
|
|
|
extendCapacity(nums); // 扩容
|
|
|
|
|
}
|
|
|
|
@ -66,7 +66,7 @@ void add(MyList *nums, int num) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中间插入元素 */
|
|
|
|
|
void insert(MyList *nums, int index, int num) {
|
|
|
|
|
void insert(myList *nums, int index, int num) {
|
|
|
|
|
assert(index >= 0 && index < size(nums));
|
|
|
|
|
// 元素数量超出容量时,触发扩容机制
|
|
|
|
|
if (size(nums) == capacity(nums)) {
|
|
|
|
@ -81,7 +81,7 @@ void insert(MyList *nums, int index, int num) {
|
|
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
|
// 注意:stdio.h 占用了 remove 关键词
|
|
|
|
|
int removeNum(MyList *nums, int index) {
|
|
|
|
|
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++) {
|
|
|
|
@ -92,7 +92,7 @@ int removeNum(MyList *nums, int index) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 列表扩容 */
|
|
|
|
|
void extendCapacity(MyList *nums) {
|
|
|
|
|
void extendCapacity(myList *nums) {
|
|
|
|
|
// 先分配空间
|
|
|
|
|
int newCapacity = capacity(nums) * nums->extendRatio;
|
|
|
|
|
int *extend = (int *)malloc(sizeof(int) * newCapacity);
|
|
|
|
@ -111,14 +111,14 @@ void extendCapacity(MyList *nums) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将列表转换为 Array 用于打印 */
|
|
|
|
|
int *toArray(MyList *nums) {
|
|
|
|
|
int *toArray(myList *nums) {
|
|
|
|
|
return nums->arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
int main() {
|
|
|
|
|
/* 初始化列表 */
|
|
|
|
|
MyList *nums = newMyList();
|
|
|
|
|
myList *nums = newMyList();
|
|
|
|
|
/* 尾部添加元素 */
|
|
|
|
|
add(nums, 1);
|
|
|
|
|
add(nums, 3);
|
|
|
|
|