void list_push_front(list_t l, element_t e) { ASSERT_LIST(l); list_insert(l, &l->head, e); }
void list_push_back(list_t l, element_t e) { ASSERT_LIST(l); list_insert(l, l->head.next, e); }
int list_empty(list_t l) { ASSERT_LIST(l); return (NULL == l->front); }
int list_size(list_t l) { ASSERT_LIST(l); return (l->size); }
int list_empty(list_t l) { ASSERT_LIST(l); return (l->head.next == &l->head); }
int list_empty(list_t l) { ASSERT_LIST(l); return (0 == l->size); }
element_t list_pop_back(list_t l) { ASSERT_LIST(l); ASSERT_LIST_UNDERFLOW(l); return list_erase(l, l->head.next); }
element_t list_pop_front(list_t l) { ASSERT_LIST(l); ASSERT_LIST_UNDERFLOW(l); return list_erase(l, (&l->head)->prev); }
void linked_list_free (linked_list list) { ASSERT_LIST (list); linked_list_header *header = (linked_list_header *) list; if (header->next) { linked_list_free ((linked_list) header->next); } jsp_mm_free (list); }
list_t list_create(void) { list_t l = (list_t)malloc(sizeof(*l)); ASSERT_LIST(l); l->front = l->rear = NULL; l->size = 0; return l; }
list_t list_create(void) { list_t l = (list_t)malloc(sizeof(*l)); ASSERT_LIST(l); l->size = 0; l->head.prev = l->head.next = &l->head; return l; }
void linked_list_free (linked_list list) { ASSERT_LIST (list); linked_list_header *header = (linked_list_header *) list; if (header->next) { linked_list_free ((linked_list) header->next); } mem_heap_free_block (list); }
void list_traverse(list_t l, void (*visit)(element_t)) { list_node_t iter; ASSERT_LIST(l); ASSERT_LIST_UNDERFLOW(l); iter = l->front; while (NULL != iter) { visit(iter->e); iter = iter->next; } }
void list_push_back(list_t l, element_t e) { int i; ASSERT_LIST(l); ASSERT_LIST_OVERFLOW(l); i = l->free; l->free = l->elements[i].next; l->elements[i].e = e; l->rear = i; ++l->size; }
void list_clear(list_t l) { list_node_t iter, node; ASSERT_LIST(l); iter = l->head.next; while (iter != &l->head) { node = iter; iter = iter->next; list_erase(l, iter); } l->size = 0; }
void list_insert(list_t l, element_t e) { list_node_t new_node; ASSERT_LIST(l); new_node = list_node_create(e); if (NULL == l->front) l->front = l->rear = new_node; else { l->rear->next = new_node; l->rear = new_node; } ++l->size; }
element_t list_remove(list_t l) { element_t e; list_node_t old_node; ASSERT_LIST(l); ASSERT_LIST_UNDERFLOW(l); old_node = l->front; l->front = l->front->next; e = old_node->e; free(old_node); --l->size; return e; }
void list_circular(list_t l, int circular) { int i; list_node_t iter; ASSERT_LIST(l); iter = l->head.next; for (i = 0; i < circular; ++i) { fprintf(stdout, "The [%d] circular circular-list show:\n", i + 1); while (iter != &l->head) { fprintf(stdout, "\tthe element is {value=>%lf}\n", *(double*)iter->e); iter = iter->next; } iter = iter->next; } }
void list_push_front(list_t l, element_t e) { int i; ASSERT_LIST(l); ASSERT_LIST_OVERFLOW(l); i = l->free; l->free = l->elements[i].next; l->elements[i].e = e; if (0 != l->size) l->elements[i].next = l->front; else l->rear = i; l->front = i; ++l->size; }
int list_find(list_t l, element_t e, int (*cmp)(element_t, element_t, int), int elem_sz) { int i = 0; list_node_t iter; ASSERT_LIST(l); ASSERT_LIST_UNDERFLOW(l); iter = l->front; while (NULL != iter) { if (cmp(e, iter->e, elem_sz)) return i; ++i; } return (-1); }
element_t list_pop_front(list_t l) { int i; element_t e; ASSERT_LIST(l); ASSERT_LIST_UNDERFLOW(l); i = l->front; e = l->elements[i].e; l->front = l->elements[i].next; l->elements[i].next = l->free; l->free = i; if (0 == --l->size) l->front = l->rear = l->free = 0; return e; }
list_t list_create(int capacity) { list_t l = (list_t)malloc(sizeof(*l)); ASSERT_LIST(l); l->capacity = (capacity < LIST_CAPACITY_DEF ? LIST_CAPACITY_DEF : capacity); l->size = 0; l->front = l->rear = 0; l->free = 0; l->elements = (element_t)malloc(l->capacity * sizeof(list_node_t)); if (NULL == l->elements) { fprintf(stderr, "create static list failed ..."); exit(0); } list_initialize(l); return l; }
void * linked_list_element (linked_list list, size_t element_num) { ASSERT_LIST (list); linked_list_header *header = (linked_list_header *) list; size_t block_size = linked_list_block_size (header->element_size); linked_list raw = list + sizeof (linked_list_header); if (block_size < header->element_size * (element_num + 1)) { if (header->next) { return linked_list_element ((linked_list) header->next, element_num - (block_size / header->element_size)); } else { return NULL; } } raw += header->element_size * element_num; return raw; }
void linked_list_set_element (linked_list list, size_t element_num, void *element) { ASSERT_LIST (list); linked_list_header *header = (linked_list_header *) list; size_t block_size = linked_list_block_size (header->element_size); uint8_t *raw = (uint8_t *) (header + 1); if (block_size < header->element_size * (element_num + 1)) { if (header->next == null_list) { header->next = (linked_list_header *) linked_list_init (header->element_size); } linked_list_set_element ((linked_list) header->next, element_num - (block_size / header->element_size), element); return; } if (element == NULL) { return; } memcpy (raw + element_num * header->element_size, element, header->element_size); }