int main(){ struct node *head; head = NULL; head = ll_prepend(head, 1); printf("After prepending once, head = %d\n", head->value); printf("----------\n"); head = ll_prepend(head, 3); printf("After prepending twice, head = %d\n", head->value); printf("----------\n"); ll_append(head, 2); printf("The values in the list are %d, %d, %d\n", head->value, head->next->value, head->next->next->value); printf("----------\n"); printf("INSPECT\n"); inspect_list(head); return 0; }
int main() { log_info("Creating a linked list."); LinkedList* list = ll_new(); check(list->first == NULL, "List was not correctly initialised."); check(list->length == 0, "List should not have any elements."); log_info("Adding an element to the list."); int v = 1337; int* vpointer = malloc(sizeof(int)); *vpointer = v; ll_prepend(list, vpointer); check(list->length == 1, "Length increment failed"); check(list->first != NULL, "Append failed"); check(list->first->data == vpointer, "Bad address"); log_info("The address of list->first->data %p", list->first->data); check(*((int*) list->first->data) == v, "Bad value"); log_info("The value is %d as expected", *((int*) list->first->data)); log_info("Adding another element."); v = 1338; int* another = malloc(sizeof(int)); *another = v; ll_prepend(list, another); check(list->length == 2, "Length increment failed"); check(*((int*) list->first->data) == v, "List increment screwed up"); log_info("Checking list[0]."); check(*((int*) ll_get(list, 0)) == v, "First element does not match what was last prepended."); log_info("Checking list[1]."); check(*((int*) ll_get(list, 1)) == 1337, "First element does not match what was first prepended."); log_info("Freeing the linked list."); ll_free(list); return 0; error: return 1; }
void ll_insert(smb_ll *list, int index, DATA new_data) { if (index <= 0) { ll_prepend(list, new_data); } else if (index >= list->length) { ll_append(list, new_data); } else { smb_ll_node *new_node = ll_create_node(new_data); smb_status status = SMB_SUCCESS; smb_ll_node *current = ll_navigate(list, index, &status); // Since we already checked indices, there can be no errors. assert(status == SMB_SUCCESS); current->prev->next = new_node; new_node->prev = current->prev; new_node->next = current; current->prev = new_node; list->length++; } }
smcp_node_t smcp_node_init( smcp_node_t self, smcp_node_t node, const char* name ) { smcp_node_t ret = NULL; require(self || (self = smcp_node_alloc()), bail); ret = (smcp_node_t)self; ret->request_handler = (void*)&smcp_default_request_handler; if (node) { require(name, bail); ret->name = name; #if SMCP_NODE_ROUTER_USE_BTREE bt_insert( (void**)&((smcp_node_t)node)->children, ret, (bt_compare_func_t)smcp_node_compare, (bt_delete_func_t)smcp_node_delete, NULL ); #else ll_prepend( (void**)&((smcp_node_t)node)->children, (void*)ret ); #endif ret->parent = node; } DEBUG_PRINTF("%s: %p",__func__,ret); bail: return ret; }
void ll_push_front(smb_ll *list, DATA new_data) { ll_prepend(list, new_data); }
void enqueue(t_queue* queue,void* datum) { ll_prepend(queue,datum); }