diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index f8ab8f560..b261a6877 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -68,6 +68,10 @@ void add(myList *list, int num) { /* 中间插入元素 */ void insert(myList *list, int index, int num) { assert(index >= 0 && index < size(list)); + // 元素数量超出容量时,触发扩容机制 + if (size(list) == capacity(list)) { + extendCapacity(list); // 扩容 + } for (int i = size(list); i > index; --i) { list->nums[i] = list->nums[i - 1]; }