Loading fibonacci_ops.c +5 −2 Original line number Diff line number Diff line Loading @@ -9,11 +9,14 @@ // linked list to cache the fibonacci numbers linked_list_item *fibonacci_first_element = NULL; linked_list_item **fibonacci_head = &fibonacci_first_element; // linked_list_item *fibonacci_first_element = NULL; // linked_list_item **fibonacci_head = &fibonacci_first_element; linked_list *fibonacci_ll = NULL; // create the ability to calculate next value on demand based on the current linked list unsigned long long get_fibonacci_value_at(unsigned int index) { fibonacci_head = &fibonacci_first_element; Loading linked_list.c +19 −7 Original line number Diff line number Diff line Loading @@ -15,21 +15,33 @@ linked_list_item *create_node(const unsigned long long value) { return node; } int push(linked_list_item **head, const unsigned long long value) { // todo cm need a way to free a linked_list, not just linked_list_item linked_list* new_linked_list() { linked_list *list = malloc(sizeof(*list)); list->head = NULL; list->tail = NULL; list->size = 0; return list; } int push(linked_list* list, const unsigned long long value) { linked_list_item *new_node = create_node(value); if (new_node == NULL) { return -1; } if (*head == NULL) { *head = new_node; if (list->head == NULL) { list->head = new_node; list->tail = new_node; list->size++; return 0; } linked_list_item *current = *head; while (current->next != NULL) { current = current->next; } linked_list_item *current = list->tail; // while (current->next != NULL) { // current = current->next; // } current->next = new_node; new_node->prev = current; list->tail = new_node; return 0; } Loading linked_list.h +9 −1 Original line number Diff line number Diff line Loading @@ -6,15 +6,23 @@ #ifndef FIBONACCI_STEP_SEARCH_LINKED_LIST_H #define FIBONACCI_STEP_SEARCH_LINKED_LIST_H typedef struct linked_list linked_list; typedef struct linked_list_item linked_list_item; struct linked_list { linked_list_item *head; linked_list_item *tail; unsigned int size; }; struct linked_list_item { unsigned long long value; linked_list_item *next; linked_list_item *prev; }; int push(linked_list_item **head, unsigned long long value); linked_list* new_linked_list(); int push(linked_list* list, unsigned long long value); void delete_linked_list(linked_list_item **head); // unsigned int get_at(linked_list_item **head, unsigned int index); Loading main.c +40 −35 Original line number Diff line number Diff line Loading @@ -6,45 +6,50 @@ int main(void) { // -> here's a linked list test linked_list_item *first_element = NULL; linked_list_item *curr = first_element; push(&curr, 2); push(&curr, 3); push(&curr, 1); // Forward traversal using a separate cursor and also remember tail linked_list_item *tail = NULL; while (curr != NULL) { printf("value is %llu\n", curr->value); tail = curr; curr = curr->next; } printf("reverse!\n"); // Reverse traversal starting from tail using prev pointers curr = tail; while (curr != NULL) { printf("value is %llu\n", curr->value); curr = curr->prev; } // Clean up delete_linked_list(&first_element); // sanity check: this should print nothing curr = first_element; while (curr != NULL) { printf("value is %llu\n", curr->value); curr = curr->next; } // linked_list_item *first_element = NULL; // linked_list_item *curr = first_element; // linked_list ll; // ll.head = NULL; // // push(&ll, 2); // push(&ll, 3); // push(&ll, 1); // // // Forward traversal using a separate cursor and also remember tail // linked_list_item *curr = ll.head; // linked_list_item *tail = NULL; // while (curr != NULL) { // printf("value is %llu\n", curr->value); // tail = curr; // curr = curr->next; // } // printf("reverse!\n"); // // // Reverse traversal starting from tail using prev pointers // curr = tail; // while (curr != NULL) { // printf("value is %llu\n", curr->value); // curr = curr->prev; // } // // // Clean up // delete_linked_list(&ll.head); // // // sanity check: this should print nothing // printf("sanity check time; does anything print?\n"); // curr = ll.head; // while (curr != NULL) { // printf("value is %llu\n", curr->value); // curr = curr->next; // } // printf("it shouldn't.\n"); // -> here's a fibonacci number test // note the inefficiency: with a singly linked list, get_fibonacci_value_at() always works linearly // todo as improvement, keep a doubly linked list & retain current index + value for potentially shorter moves // for (unsigned int i = 0; i < 100; i++) { // printf("i %u, fibonacci number: %llu\n", i, get_fibonacci_value_at(i)); // } for (unsigned int i = 0; i < 100; i++) { printf("i %u, fibonacci number: %llu\n", i, get_fibonacci_value_at(i)); } return 0; } Loading
fibonacci_ops.c +5 −2 Original line number Diff line number Diff line Loading @@ -9,11 +9,14 @@ // linked list to cache the fibonacci numbers linked_list_item *fibonacci_first_element = NULL; linked_list_item **fibonacci_head = &fibonacci_first_element; // linked_list_item *fibonacci_first_element = NULL; // linked_list_item **fibonacci_head = &fibonacci_first_element; linked_list *fibonacci_ll = NULL; // create the ability to calculate next value on demand based on the current linked list unsigned long long get_fibonacci_value_at(unsigned int index) { fibonacci_head = &fibonacci_first_element; Loading
linked_list.c +19 −7 Original line number Diff line number Diff line Loading @@ -15,21 +15,33 @@ linked_list_item *create_node(const unsigned long long value) { return node; } int push(linked_list_item **head, const unsigned long long value) { // todo cm need a way to free a linked_list, not just linked_list_item linked_list* new_linked_list() { linked_list *list = malloc(sizeof(*list)); list->head = NULL; list->tail = NULL; list->size = 0; return list; } int push(linked_list* list, const unsigned long long value) { linked_list_item *new_node = create_node(value); if (new_node == NULL) { return -1; } if (*head == NULL) { *head = new_node; if (list->head == NULL) { list->head = new_node; list->tail = new_node; list->size++; return 0; } linked_list_item *current = *head; while (current->next != NULL) { current = current->next; } linked_list_item *current = list->tail; // while (current->next != NULL) { // current = current->next; // } current->next = new_node; new_node->prev = current; list->tail = new_node; return 0; } Loading
linked_list.h +9 −1 Original line number Diff line number Diff line Loading @@ -6,15 +6,23 @@ #ifndef FIBONACCI_STEP_SEARCH_LINKED_LIST_H #define FIBONACCI_STEP_SEARCH_LINKED_LIST_H typedef struct linked_list linked_list; typedef struct linked_list_item linked_list_item; struct linked_list { linked_list_item *head; linked_list_item *tail; unsigned int size; }; struct linked_list_item { unsigned long long value; linked_list_item *next; linked_list_item *prev; }; int push(linked_list_item **head, unsigned long long value); linked_list* new_linked_list(); int push(linked_list* list, unsigned long long value); void delete_linked_list(linked_list_item **head); // unsigned int get_at(linked_list_item **head, unsigned int index); Loading
main.c +40 −35 Original line number Diff line number Diff line Loading @@ -6,45 +6,50 @@ int main(void) { // -> here's a linked list test linked_list_item *first_element = NULL; linked_list_item *curr = first_element; push(&curr, 2); push(&curr, 3); push(&curr, 1); // Forward traversal using a separate cursor and also remember tail linked_list_item *tail = NULL; while (curr != NULL) { printf("value is %llu\n", curr->value); tail = curr; curr = curr->next; } printf("reverse!\n"); // Reverse traversal starting from tail using prev pointers curr = tail; while (curr != NULL) { printf("value is %llu\n", curr->value); curr = curr->prev; } // Clean up delete_linked_list(&first_element); // sanity check: this should print nothing curr = first_element; while (curr != NULL) { printf("value is %llu\n", curr->value); curr = curr->next; } // linked_list_item *first_element = NULL; // linked_list_item *curr = first_element; // linked_list ll; // ll.head = NULL; // // push(&ll, 2); // push(&ll, 3); // push(&ll, 1); // // // Forward traversal using a separate cursor and also remember tail // linked_list_item *curr = ll.head; // linked_list_item *tail = NULL; // while (curr != NULL) { // printf("value is %llu\n", curr->value); // tail = curr; // curr = curr->next; // } // printf("reverse!\n"); // // // Reverse traversal starting from tail using prev pointers // curr = tail; // while (curr != NULL) { // printf("value is %llu\n", curr->value); // curr = curr->prev; // } // // // Clean up // delete_linked_list(&ll.head); // // // sanity check: this should print nothing // printf("sanity check time; does anything print?\n"); // curr = ll.head; // while (curr != NULL) { // printf("value is %llu\n", curr->value); // curr = curr->next; // } // printf("it shouldn't.\n"); // -> here's a fibonacci number test // note the inefficiency: with a singly linked list, get_fibonacci_value_at() always works linearly // todo as improvement, keep a doubly linked list & retain current index + value for potentially shorter moves // for (unsigned int i = 0; i < 100; i++) { // printf("i %u, fibonacci number: %llu\n", i, get_fibonacci_value_at(i)); // } for (unsigned int i = 0; i < 100; i++) { printf("i %u, fibonacci number: %llu\n", i, get_fibonacci_value_at(i)); } return 0; }