Select Git revision
linkedlist.c
linkedlist.c 3.33 KiB
#include "linkedlist.h"
#include <assert.h>
#include <stdbool.h>
#include "point.h"
list_points_node_int_t* list_points_node_create_int(vector_int_t* vec) {
list_points_node_int_t* node = malloc(sizeof(list_points_node_int_t));
if (NULL == node) return NULL;
point_int_t* point = point_create_int(vec);
if (NULL == point) return NULL;
node->point = point;
node->next = NULL;
return node;
}
list_points_node_fpt_t* list_points_node_create_fpt(vector_fpt_t* vec) {
list_points_node_fpt_t* node = malloc(sizeof(list_points_node_fpt_t));
if (NULL == node) return NULL;
point_fpt_t* point = point_create_fpt(vec);
if (NULL == point) return NULL;
node->point = point;
node->next = NULL;
return node;
}
void list_points_node_destroy_int(list_points_node_int_t* node, const bool full) {
if (NULL == node) return;
if (full) point_destroy_int(node->point);
free(node);
}
void list_points_node_destroy_fpt(list_points_node_fpt_t* node, const bool full) {
if (NULL == node) return;
if (full) point_destroy_fpt(node->point);
free(node);
}
list_points_int_t* list_points_create_int() {
list_points_int_t* list = NULL;
list = malloc(sizeof(list_points_int_t));
if (NULL == list) return NULL;
list->head = NULL;
list->tail = NULL;
list->size = 0;
return list;
}
list_points_fpt_t* list_points_create_fpt() {
list_points_fpt_t* list = NULL;
list = malloc(sizeof(list_points_fpt_t));
if (NULL == list) return NULL;
list->head = NULL;
list->tail = NULL;
list->size = 0;
return list;
}
void list_points_destroy_int(list_points_int_t* list, const bool full) {
if (NULL == list) return;
list_points_node_int_t* node;
while ((node = list->head) != NULL) {
list->head = node->next;
list_points_node_destroy_int(node, full);
}
free(list);
}
void list_points_destroy_fpt(list_points_fpt_t* list, const bool full) {
if (NULL == list) return;
list_points_node_fpt_t* node;
while ((node = list->head) != NULL) {
list->head = node->next;
list_points_node_destroy_fpt(node, full);
}
free(list);
}
void list_points_append_int(list_points_int_t* list, vector_int_t* vector) {
if (NULL == vector) return;
list_points_node_int_t* node = list_points_node_create_int(vector);
if (NULL == list->head) { // if list is empty
list->head = node;
list->tail = list->head;
} else {
list->tail->next = node;
list->tail = node;
}
++list->size;
}
void list_points_append_fpt(list_points_fpt_t* list, vector_fpt_t* vector) {
if (NULL == vector) return;
list_points_node_fpt_t* node = list_points_node_create_fpt(vector);
if (NULL == list->head) { // if list is empty
list->head = node;
list->tail = list->head;
} else {
list->tail->next = node;
list->tail = node;
}
++list->size;
}
point_int_t** list_points_to_array_int(const list_points_int_t* list) {
point_int_t** a = calloc(list->size, sizeof(point_int_t*));
if (NULL == a) return NULL;
list_points_node_int_t* cur = list->head;
size_t idx = 0;
while (cur != NULL) {
a[idx] = cur->point;
cur = cur->next;
++idx;
}
assert(idx == list->size);
return a;
}
point_fpt_t** list_points_to_array_fpt(const list_points_fpt_t* list) {
point_fpt_t** a = calloc(list->size, sizeof(point_fpt_t*));
if (NULL == a) return NULL;
list_points_node_fpt_t* cur = list->head;
size_t idx = 0;
while (cur != NULL) {
a[idx] = cur->point;
cur = cur->next;
++idx;
}
assert(idx == list->size);
return a;
}