Commit 2f112076 authored by Chris's avatar Chris
Browse files

created cache of fibonacci numbers

parent b8d08aa6
Loading
Loading
Loading
Loading
+38 −2
Original line number Diff line number Diff line
@@ -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;
}
+3 −0
Original line number Diff line number Diff line
#pragma once
//
// Created by Chris on 9/12/25.
//
@@ -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
+3 −3
Original line number Diff line number Diff line
@@ -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;
@@ -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;
+3 −1
Original line number Diff line number Diff line
#pragma once
//
// Created by Chris on 9/12/25.
//
@@ -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
+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;
}