Commit 4ac35bb2 authored by Chris's avatar Chris
Browse files

cleanup in main; created start_from_tail() and approach notes on how to do ll...

cleanup in main; created start_from_tail() and approach notes on how to do ll retrieval for get_fibonacci_value_at()
parent d16b81be
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -11,6 +11,13 @@
linked_list *fibonacci_ll = NULL;

unsigned long long get_fibonacci_value_at(unsigned int index) {
    // todo cm split this into two functionalities:
    //  1. list has to be built out beyond what it currently houses
    //      -> with this approach, start from tail and push more (similar to current functionality) -- utilize ll->size
    //  2. retrieve from currently cached values
    //      -> with this approach, use linked_list start_from_tail() to determine where to start traversal and then traverse like normal ll
    //      -> (need to test start_from_tail() also)
    //      -> we could also put this functionality in linked_list.c so that linked list operations aren't tangled in fiboniacci_ops
    if (fibonacci_ll == NULL) {
        fibonacci_ll = new_linked_list();
    }
+9 −0
Original line number Diff line number Diff line
@@ -53,3 +53,12 @@ void delete_linked_list(linked_list *list) {
    list->tail = NULL;
    list->size = 0;
}

/**
 * basically if the index is closer to the end of the list, you should start from the tail and decrement
 */
int start_from_tail(linked_list *list, const unsigned int index) {
    if (index > list->size) return 0;
    const unsigned int midpoint = list->size - index;
    return index > midpoint;
}
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ struct linked_list_item {
linked_list* new_linked_list();
int push(linked_list* list, unsigned long long value);
void delete_linked_list(linked_list *list);
int start_from_tail(linked_list *list, unsigned int index);
// unsigned int get_at(linked_list_item **head, unsigned int index);

#endif //FIBONACCI_STEP_SEARCH_LINKED_LIST_H
 No newline at end of file
+52 −43
Original line number Diff line number Diff line
@@ -4,9 +4,17 @@
#include "fibonacci_ops.h"
#include "linked_list.h"

void linked_list_test(void);
void fibonacci_generator_test(void);

int main(void) {
    // -> here's a linked list test
     linked_list_item *first_element = NULL;
    linked_list_test();
    fibonacci_generator_test();

    return 0;
}

void linked_list_test(void) {
    linked_list ll;
    ll.head = NULL;

@@ -45,14 +53,15 @@ int main(void) {
    }
    printf("it shouldn't.\n");

    // -> here's a fibonacci number test
}

void fibonacci_generator_test(void) {
    //      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));
    }

//    printf("i %u, fibonacci number: %llu\n", 6, get_fibonacci_value_at(6));
    printf("i %u, fibonacci number: %llu\n", 6, get_fibonacci_value_at(6)); // random access

    return 0;
}