Advertisement
kyoo0000

simple_generic_array_list.c

Sep 9th, 2021
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct {
  5.   void**  values;
  6.   size_t  len, capacity;
  7. } ArrList;
  8.  
  9. ArrList* init_list(size_t capacity) {
  10.   ArrList *l = (ArrList*) calloc(1, sizeof(ArrList));
  11.   l->capacity = capacity;
  12.   l->len = 0;
  13.   l->values = (void**) calloc(capacity, sizeof(void*));
  14.   return l;
  15. }
  16.  
  17. void add_value(ArrList *l, void *value) {
  18.   if(l->len == l->capacity) {
  19.     l->capacity += 100;
  20.     l->values = (int*) realloc(l->values, l->capacity *sizeof(void*));
  21.   }
  22.   l->values[l->len++] = value;
  23. }
  24.  
  25. void destroy_arrlist(ArrList *list, void (*destroy_value)(void* value)) {
  26.   for(int i = 0; i < list->len; i++)
  27.     destroy_value(*(list->values + i));
  28.   free(list);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement