void list_insert(int index, LIST_TYPE value, List *list){ if(index > list->size || index < 0){ printf("index is out of bounds.\n"); assert(0); return; } Node* newNode = node_constructor(value); // this wud break if it wasnt the case that the only way to insert something to a empty list is to insert it into the 0 index. // whcih means prepend will handle this fine. if(index == 0){ list_prepend(value, list); } else if(index == list->size){ list_append(value, list); } else{ int counter = 0; Node* ptr; for(ptr = list->head; ptr != NULL; ptr = ptr->next){ if(counter == index){ Node* left = ptr->prev; Node* right = ptr; left->next = newNode; newNode->prev = left; right->prev = newNode; newNode->next = right; list->size++; return; } counter++; } } }
void list_prepend(LIST_TYPE value, List* list){ Node* newNode = node_constructor(value); if(list->head == NULL && list->tail == NULL){ list->head = newNode; list->tail = newNode; } else{ list->head->prev = newNode; newNode->next = list->head; list->head = newNode; } list->size++; }
t_list *recur_list_build(int fd, t_list *prev) { char *line; t_list *ret; int check; (void)prev; line = NULL; check = 0; if ((check = get_next_line(fd, &line)) == 0) exit(0); ret = node_constructor(line); ret->prev = NULL; while (((check = get_next_line(fd, &line))) || ft_strlen(line)) { ret->next = node_constructor(line); ret->next->prev = ret; ret = ret->next; } ret->next = NULL; while (ret->prev) ret = ret->prev; return (ret); }