Loading main.c +35 −25 Original line number Diff line number Diff line Loading @@ -6,36 +6,46 @@ int main(void) { // -> 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 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 // head = &first_element; // while ((*head) != NULL) { // printf("value is %d\n", (*head)->value); // head = &(*head)->next; // } 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; } // -> 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
main.c +35 −25 Original line number Diff line number Diff line Loading @@ -6,36 +6,46 @@ int main(void) { // -> 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 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 // head = &first_element; // while ((*head) != NULL) { // printf("value is %d\n", (*head)->value); // head = &(*head)->next; // } 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; } // -> 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; }