Last active
August 29, 2015 14:17
-
-
Save cjxgm/e96f87d31ff46c7057ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "list.h" | |
typedef struct node node_t; | |
struct node | |
{ | |
node_t* next; | |
void* data; | |
}; | |
struct list | |
{ | |
node_t* head; | |
node_t* back; | |
list_value_deleter_t* deleter; | |
}; | |
// low-level life-time management | |
static list_t* list_alloc() { return malloc(sizeof(list_t)); } | |
static void list_free(list_t* list) { free(list); } | |
static void list_init(list_t* list, list_value_deleter_t* deleter) | |
{ | |
list->head = list->back = calloc(1, sizeof(node_t)); | |
list->deleter = deleter; | |
} | |
static void list_deinit(list_t* list) { free(list->head); } | |
list_t* list_new(list_value_deleter_t* deleter) | |
{ | |
list_t* list = list_alloc(); | |
list_init(list, deleter); | |
return list; | |
} | |
list_t* list_new_default() { return list_new(&free); } | |
void list_delete(list_t* list) | |
{ | |
list_clear(list); | |
list_deinit(list); | |
list_free(list); | |
} | |
bool list_not_empty(list_t* list) { return list->head->next; } | |
void list_clear(list_t* list) | |
{ | |
while (list_not_empty(list)) | |
list_pop_front(list); | |
} | |
void* list_remove_front(list_t* list) | |
{ | |
node_t* front = list->head->next; | |
list->head->next = front->next; | |
void* data = front->data; | |
free(front); | |
return data; | |
} | |
void list_pop_front(list_t* list) | |
{ | |
void* data = list_remove_front(list); | |
list->deleter(data); | |
} | |
void list_push_back(list_t* list, void* data) | |
{ | |
node_t* node = malloc(sizeof(node_t)); | |
node->next = NULL; | |
node->data = data; | |
list->back->next = node; | |
list->back = node; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdbool.h> | |
#include <stdlib.h> | |
typedef struct list list_t; | |
typedef void list_value_deleter_t(void*); | |
list_t* list_new(list_value_deleter_t* deleter); | |
list_t* list_new_default(); | |
void list_delete(list_t* list); | |
bool list_not_empty(list_t* list); | |
void list_clear(list_t* list); | |
void* list_remove_front(list_t* list); | |
void list_pop_front(list_t* list); | |
void list_push_back(list_t* list, void* data); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "list.h" | |
list_t* int_list_new() { return list_new_default(); } | |
void int_list_push_back(list_t* list, int x) | |
{ | |
int* value = malloc(sizeof(int)); | |
*value = x; | |
list_push_back(list, value); | |
} | |
int main() | |
{ | |
list_t* list = int_list_new(); | |
int_list_push_back(list, 1); | |
int_list_push_back(list, 2); | |
int_list_push_back(list, 3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment