int insert_in_list(List **list, char *content, int index) { List *ptr_to_node_prior; List *next_to_assign; /* handle index parameter less than 0 */ if (index < 0) { return 0; } /* else, if index parameter 0 or list is empty */ if (index == 0 || *list == NULL) { /* simply run add_node to make new node head */ return add_node(list, content); } /* ELSE */ /* find the node prior to node we want to insert */ ptr_to_node_prior = find_node_prior(*list, index); /* if index req'd greater than size of list */ if (ptr_to_node_prior == NULL) { /* make next val for new node NULL (make new node equivalent to tail of list) */ next_to_assign = NULL; ptr_to_node_prior = find_end_of_list(list); } else { /* store next value of node prior */ next_to_assign = ptr_to_node_prior->next; } /* create node we want to insert, with input string & the next val of the node prior, & insert in list */ return insert_new_node(ptr_to_node_prior, content, next_to_assign); }
/** * add_to_list - Add the block to the specified free list. */ static void add_to_list(char *bp, int list_index) { char *tail_payload; mem_header *tail_node; mem_header *current = MEMHEADER_FROM_PAYLOAD(bp); TRACE(">>>Entering add_to_list(bp=0x%X, list_index=%d)\n", (unsigned int)bp, list_index); current->next_free = NULL; tail_payload = find_end_of_list(list_index); if (tail_payload == NULL) { free_lists[list_index] = bp; current->prev_free = NULL; TRACE("<<<---Leaving add_to_list(), list's head pointer NULL, list empty\n"); return; } tail_node = MEMHEADER_FROM_PAYLOAD(tail_payload); tail_node->next_free = bp; current->prev_free = tail_payload; TRACE("<<<---Leaving add_to_list()\n"); }
int add_node(List **list, char *content) { struct List *ptr_to_node; struct List *ptr_to_current_tail; ptr_to_node = malloc(sizeof(struct List)); if (ptr_to_node == NULL) { return 1; } ptr_to_node->str = copy_string(content); ptr_to_node->next = NULL; /* if list is NULL (i.e. list is empty) */ if (*list == NULL) { /* make list point to ptr_to_node */ *list = ptr_to_node; return 0; } /* ELSE */ /* find current tail node in list and make it point to newly-allocated node instead of NULL. */ ptr_to_current_tail = find_end_of_list(list); ptr_to_current_tail->next = ptr_to_node; return 0; }