-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
list_insert now works as it should #18
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,14 @@ list_t *create_list(void) { | |
return list; | ||
} | ||
|
||
static | ||
void list_resize(list_t *list) { | ||
if (list->length == list->capacity) { | ||
list->capacity += 10; | ||
list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
} | ||
} | ||
|
||
void list_free(list_t *list) { | ||
if (list == NULL) { | ||
return; | ||
|
@@ -20,25 +28,20 @@ void list_free(list_t *list) { | |
} | ||
|
||
void list_add(list_t *list, void *item) { | ||
if (list->length == list->capacity) { | ||
list->capacity += 10; | ||
list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
} | ||
list_resize(list); | ||
list->items[list->length++] = item; | ||
} | ||
|
||
void list_insert(list_t *list, int index, void *item) { | ||
// TODO: Implement this properly | ||
if (list->length == list->capacity) { | ||
list->capacity += 10; | ||
list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
} | ||
list->items[list->length++] = item; | ||
list_resize(list); | ||
memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); | ||
list->length++; | ||
list->items[index] = item; | ||
} | ||
|
||
void list_del(list_t *list, int index) { | ||
list->length--; | ||
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1)); | ||
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. capacity is the one that moves more though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, my bad, I read the diff backwards. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no problem, i wanted to try to implement the multikey handling with list_t but compiler complains about int->void* void*->int to much. |
||
} | ||
|
||
void list_cat(list_t *list, list_t *source) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style issue - join these two lines