Skip to content

Instantly share code, notes, and snippets.

@euske
Created May 29, 2020 00:30
Show Gist options
  • Select an option

  • Save euske/82ff10c1eebcec7ccc6fe7ccddb2c38b to your computer and use it in GitHub Desktop.

Select an option

Save euske/82ff10c1eebcec7ccc6fe7ccddb2c38b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int T;
typedef struct {
T* objs;
size_t nobjs;
size_t maxobjs;
} T_list;
T_list* T_list_create()
{
T_list* list = (T_list*) malloc(sizeof(T_list));
if (list == NULL) exit(111);
list->objs = NULL;
list->nobjs = list->maxobjs = 0;
return list;
}
void T_list_destroy(T_list* list)
{
if (list->objs != NULL) {
free(list->objs);
}
free(list);
}
void T_list_add(T_list* list, T obj)
{
if (list->maxobjs <= list->nobjs) {
list->maxobjs += (list->maxobjs/2)+1;
list->objs = (T*) realloc(list->objs, sizeof(T) * list->maxobjs);
if (list->objs == NULL) exit(111);
}
list->objs[list->nobjs++] = obj;
}
int main(int argc, char* argv[])
{
int n = 100;
T_list* a = T_list_create();
for (int i = 0; i < n; i++) {
T_list_add(a, i);
}
T_list_destroy(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment