Loading fibonacci_ops.c +38 −2 Original line number Diff line number Diff line Loading @@ -2,8 +2,44 @@ // Created by Chris on 9/12/25. // #include <stdio.h> #include "fibonacci_ops.h" #include "linked_list.h" // linked list to cache the fibonacci numbers // create a linked list to cache the fibonacci numbers linked_list_item *fibonacci_first_element = NULL; linked_list_item **fibonacci_head = &fibonacci_first_element; // create the ability to calculate next value on demand based on the current linked list unsigned int get_fibonacci_value_at(unsigned int index) { fibonacci_head = &fibonacci_first_element; unsigned int current_i = 0; unsigned int current_value = 0; unsigned int a = 1; unsigned int b = 0; for (current_i = 0; current_i < index; current_i++) { if (*fibonacci_head == NULL) { push(fibonacci_head, next_fibonacci_number(a, b)); } current_value = (*fibonacci_head)->value; if (current_i % 2 == 0) { a = (*fibonacci_head)->value; } else { b = (*fibonacci_head)->value; } // iterate to next element fibonacci_head = &(*fibonacci_head)->next; } return current_value; } unsigned int next_fibonacci_number(const unsigned int a, const unsigned int b) { return a + b; } fibonacci_ops.h +3 −0 Original line number Diff line number Diff line #pragma once // // Created by Chris on 9/12/25. // Loading @@ -5,4 +6,6 @@ #ifndef FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H #define FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H unsigned int get_fibonacci_value_at(unsigned int index); unsigned int next_fibonacci_number(unsigned int a, unsigned int b); #endif //FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H No newline at end of file linked_list.c +3 −3 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ // Created by Chris on 9/12/25. // linked_list_item *create_node(unsigned int value) { linked_list_item *create_node(const unsigned int value) { linked_list_item *node = malloc(sizeof(*node)); if (node == NULL) { return NULL; Loading @@ -14,7 +14,7 @@ linked_list_item *create_node(unsigned int value) { return node; } int push(linked_list_item **head, unsigned int value) { int push(linked_list_item **head, const unsigned int value) { linked_list_item *new_node = create_node(value); if (new_node == NULL) { return -1; Loading linked_list.h +3 −1 Original line number Diff line number Diff line #pragma once // // Created by Chris on 9/12/25. // Loading @@ -10,10 +11,11 @@ typedef struct linked_list_item linked_list_item; struct linked_list_item { unsigned int value; linked_list_item *next; // linked_list_item *prev; linked_list_item *prev; }; int push(linked_list_item **head, unsigned int value); void delete_linked_list(linked_list_item **head); // unsigned int get_at(linked_list_item **head, unsigned int index); #endif //FIBONACCI_STEP_SEARCH_LINKED_LIST_H No newline at end of file main.c +30 −24 Original line number Diff line number Diff line #include <stdio.h> #include <stdlib.h> #include "fibonacci_ops.h" #include "linked_list.h" int main(void) { linked_list_item *first_element = NULL; linked_list_item **head = &first_element; push(head, 2); push(head, 3); push(head, 1); head = &first_element; while ((*head) != NULL) { printf("value is %d\n", (*head)->value); head = &(*head)->next; // -> here's a linked list test // linked_list_item *first_element = NULL; // linked_list_item **head = &first_element; // // push(head, 2); // push(head, 3); // push(head, 1); // // head = &first_element; // // while ((*head) != NULL) { // printf("value is %d\n", (*head)->value); // head = &(*head)->next; // } // // head = &first_element; // delete_linked_list(head); // // // sanity check: this should print nothing // head = &first_element; // while ((*head) != NULL) { // printf("value is %d\n", (*head)->value); // head = &(*head)->next; // } // -> here's a fibonacci number test for (unsigned int i = 0; i < 10; i++) { printf("fibonacci number: %d \n", get_fibonacci_value_at(i)); } head = &first_element; delete_linked_list(head); // sanity check: this should print nothing head = &first_element; while ((*head) != NULL) { printf("value is %d\n", (*head)->value); head = &(*head)->next; } return 0; } Loading
fibonacci_ops.c +38 −2 Original line number Diff line number Diff line Loading @@ -2,8 +2,44 @@ // Created by Chris on 9/12/25. // #include <stdio.h> #include "fibonacci_ops.h" #include "linked_list.h" // linked list to cache the fibonacci numbers // create a linked list to cache the fibonacci numbers linked_list_item *fibonacci_first_element = NULL; linked_list_item **fibonacci_head = &fibonacci_first_element; // create the ability to calculate next value on demand based on the current linked list unsigned int get_fibonacci_value_at(unsigned int index) { fibonacci_head = &fibonacci_first_element; unsigned int current_i = 0; unsigned int current_value = 0; unsigned int a = 1; unsigned int b = 0; for (current_i = 0; current_i < index; current_i++) { if (*fibonacci_head == NULL) { push(fibonacci_head, next_fibonacci_number(a, b)); } current_value = (*fibonacci_head)->value; if (current_i % 2 == 0) { a = (*fibonacci_head)->value; } else { b = (*fibonacci_head)->value; } // iterate to next element fibonacci_head = &(*fibonacci_head)->next; } return current_value; } unsigned int next_fibonacci_number(const unsigned int a, const unsigned int b) { return a + b; }
fibonacci_ops.h +3 −0 Original line number Diff line number Diff line #pragma once // // Created by Chris on 9/12/25. // Loading @@ -5,4 +6,6 @@ #ifndef FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H #define FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H unsigned int get_fibonacci_value_at(unsigned int index); unsigned int next_fibonacci_number(unsigned int a, unsigned int b); #endif //FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H No newline at end of file
linked_list.c +3 −3 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ // Created by Chris on 9/12/25. // linked_list_item *create_node(unsigned int value) { linked_list_item *create_node(const unsigned int value) { linked_list_item *node = malloc(sizeof(*node)); if (node == NULL) { return NULL; Loading @@ -14,7 +14,7 @@ linked_list_item *create_node(unsigned int value) { return node; } int push(linked_list_item **head, unsigned int value) { int push(linked_list_item **head, const unsigned int value) { linked_list_item *new_node = create_node(value); if (new_node == NULL) { return -1; Loading
linked_list.h +3 −1 Original line number Diff line number Diff line #pragma once // // Created by Chris on 9/12/25. // Loading @@ -10,10 +11,11 @@ typedef struct linked_list_item linked_list_item; struct linked_list_item { unsigned int value; linked_list_item *next; // linked_list_item *prev; linked_list_item *prev; }; int push(linked_list_item **head, unsigned int value); void delete_linked_list(linked_list_item **head); // unsigned int get_at(linked_list_item **head, unsigned int index); #endif //FIBONACCI_STEP_SEARCH_LINKED_LIST_H No newline at end of file
main.c +30 −24 Original line number Diff line number Diff line #include <stdio.h> #include <stdlib.h> #include "fibonacci_ops.h" #include "linked_list.h" int main(void) { linked_list_item *first_element = NULL; linked_list_item **head = &first_element; push(head, 2); push(head, 3); push(head, 1); head = &first_element; while ((*head) != NULL) { printf("value is %d\n", (*head)->value); head = &(*head)->next; // -> here's a linked list test // linked_list_item *first_element = NULL; // linked_list_item **head = &first_element; // // push(head, 2); // push(head, 3); // push(head, 1); // // head = &first_element; // // while ((*head) != NULL) { // printf("value is %d\n", (*head)->value); // head = &(*head)->next; // } // // head = &first_element; // delete_linked_list(head); // // // sanity check: this should print nothing // head = &first_element; // while ((*head) != NULL) { // printf("value is %d\n", (*head)->value); // head = &(*head)->next; // } // -> here's a fibonacci number test for (unsigned int i = 0; i < 10; i++) { printf("fibonacci number: %d \n", get_fibonacci_value_at(i)); } head = &first_element; delete_linked_list(head); // sanity check: this should print nothing head = &first_element; while ((*head) != NULL) { printf("value is %d\n", (*head)->value); head = &(*head)->next; } return 0; }