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