@@ -11,6 +11,13 @@ list_t *create_list(void) {
11
11
return list ;
12
12
}
13
13
14
+ static void list_resize (list_t * list ) {
15
+ if (list -> length == list -> capacity ) {
16
+ list -> capacity += 10 ;
17
+ list -> items = realloc (list -> items , sizeof (void * ) * list -> capacity );
18
+ }
19
+ }
20
+
14
21
void list_free (list_t * list ) {
15
22
if (list == NULL ) {
16
23
return ;
@@ -20,25 +27,20 @@ void list_free(list_t *list) {
20
27
}
21
28
22
29
void list_add (list_t * list , void * item ) {
23
- if (list -> length == list -> capacity ) {
24
- list -> capacity += 10 ;
25
- list -> items = realloc (list -> items , sizeof (void * ) * list -> capacity );
26
- }
30
+ list_resize (list );
27
31
list -> items [list -> length ++ ] = item ;
28
32
}
29
33
30
34
void list_insert (list_t * list , int index , void * item ) {
31
- // TODO: Implement this properly
32
- if (list -> length == list -> capacity ) {
33
- list -> capacity += 10 ;
34
- list -> items = realloc (list -> items , sizeof (void * ) * list -> capacity );
35
- }
36
- list -> items [list -> length ++ ] = item ;
35
+ list_resize (list );
36
+ memmove (& list -> items [index + 1 ], & list -> items [index ], sizeof (void * ) * (list -> length - index ));
37
+ list -> length ++ ;
38
+ list -> items [index ] = item ;
37
39
}
38
40
39
41
void list_del (list_t * list , int index ) {
40
42
list -> length -- ;
41
- memmove (& list -> items [index ], & list -> items [index + 1 ], sizeof (void * ) * (list -> capacity - index - 1 ));
43
+ memmove (& list -> items [index ], & list -> items [index + 1 ], sizeof (void * ) * (list -> length - index ));
42
44
}
43
45
44
46
void list_cat (list_t * list , list_t * source ) {
0 commit comments