Commit d16b81be authored by Chris's avatar Chris
Browse files

cleanup for migrating to new linked_list type

parent 3420752d
Loading
Loading
Loading
Loading
+16 −14
Original line number Diff line number Diff line
@@ -8,17 +8,14 @@
#include "linked_list.h"

// linked list to cache the fibonacci numbers

// 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;
    if (fibonacci_ll == NULL) {
        fibonacci_ll = new_linked_list();
    }

    linked_list_item *current_item = fibonacci_ll->head;

    unsigned int current_i = 0;
    unsigned long long current_value = 0;
@@ -26,11 +23,12 @@ unsigned long long get_fibonacci_value_at(unsigned int index) {
    unsigned long long a = 1;
    unsigned long long b = 0;
    for (current_i = 0; current_i < index; current_i++) {
        if (*fibonacci_head == NULL) {
            push(fibonacci_head, next_fibonacci_number(a, b));
        if (current_item == NULL) {
            push(fibonacci_ll, next_fibonacci_number(a, b));
            current_item = fibonacci_ll->tail;
        }

        current_value = (*fibonacci_head)->value;
        current_value = current_item->value;

        // overflow check
        if (current_value < a || current_value < b) {
@@ -39,14 +37,18 @@ unsigned long long get_fibonacci_value_at(unsigned int index) {
        }

        if (current_i % 2 == 0) {
            a = (*fibonacci_head)->value;
            a = current_item->value;
        } else {
            b = (*fibonacci_head)->value;
            b = current_item->value;
        }
        // iterate to next element
        fibonacci_head = &(*fibonacci_head)->next;
        current_item = current_item->next;
    }

//    fibonacci_ll-> tail != NULL && printf("fibonacci tail value is %llu\n", fibonacci_ll->tail->value); // test to ensure that tail value is updating correctly

    printf("fibonacci_ll size is %u\n", fibonacci_ll->size);

    return current_value;
}

+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
#ifndef FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H
#define FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H

unsigned long long get_fibonacci_value_at(unsigned int index);

unsigned long long get_fibonacci_value_at(unsigned int index);
unsigned long long next_fibonacci_number(unsigned long long a, unsigned long long b);
#endif //FIBONACCI_STEP_SEARCH_FIBONACCI_OPS_H
 No newline at end of file
+10 −11
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@ linked_list_item *create_node(const unsigned long long value) {
    return node;
}

// 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;
@@ -36,21 +35,21 @@ int push(linked_list* list, const unsigned long long value) {
        return 0;
    }
    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;
    list->size++;
    return 0;
}

void delete_linked_list(linked_list_item **head) {
    linked_list_item *current = *head;
    while (current != NULL) {
        linked_list_item *next = current->next;
        free(current);
        current = next;
    }
    *head = NULL;
void delete_linked_list(linked_list *list) {
    linked_list_item *current_ll_item = list->head;
    while (current_ll_item != NULL) {
        linked_list_item *next = current_ll_item->next;
        free(current_ll_item);
        current_ll_item = next;
    }
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
}
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,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_item **head);
void delete_linked_list(linked_list *list);
// unsigned int get_at(linked_list_item **head, unsigned int index);

#endif //FIBONACCI_STEP_SEARCH_LINKED_LIST_H
 No newline at end of file
+43 −40
Original line number Diff line number Diff line
@@ -6,50 +6,53 @@

int main(void) {
    // -> here's a linked list test
    // 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");
     linked_list_item *first_element = NULL;
     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
     printf("cleanup\n");
     delete_linked_list(&ll);
    printf("cleanup done\n");

     // 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));
//    }

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

    return 0;
}