Last active
April 23, 2017 21:03
-
-
Save jbenner-radham/0dd9360fce8f81b29497f6eb8de3ac2f to your computer and use it in GitHub Desktop.
Fun with linked list data structures!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| STD = -std=c99 | |
| # > If there are C compiler options that must be used for proper compilation of | |
| # > certain files, do not include them in CFLAGS. Users expect to be able to | |
| # > specify CFLAGS freely themselves. Instead, arrange to pass the necessary | |
| # > options to the C compiler independently of CFLAGS, by writing them | |
| # > explicitly in the compilation commands or by defining an implicit rule [...] | |
| CFLAGS = $(STD) -O2 -pedantic -Wall -Wextra -Iinclude -g -fsanitize=address -fno-omit-frame-pointer | |
| # The first target listed acts as the default. | |
| list: | |
| @ $(CC) $(CFLAGS) -o word_list word_list.c && ./word_list | |
| clean: | |
| @ rm -f word_list | |
| .PHONEY: list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef _POSIX_C_SOURCE | |
| #define _POSIX_C_SOURCE 200809L // POSIX.1-2008 Feature Test Macro | |
| #endif | |
| #include <stdbool.h> // true, false | |
| #include <stdio.h> // printf() | |
| #include <stdlib.h> // malloc() | |
| #include <string.h> // strncpy() | |
| typedef struct _word_list { | |
| ssize_t index; | |
| char word[BUFSIZ]; | |
| struct _word_list *prev; | |
| struct _word_list *next; | |
| } WordList; | |
| void word_list_ctor(WordList *list) { | |
| list->index = -1; | |
| list->prev = NULL; | |
| list->next = NULL; | |
| memset(list->word, '\0', BUFSIZ); | |
| } | |
| void word_list_append(WordList *list, char *word) { | |
| if (list->index == -1) { | |
| list->index += 1; | |
| strncpy(list->word, word, BUFSIZ); | |
| return; | |
| } | |
| WordList *current = list; | |
| WordList *new = (WordList*) malloc(sizeof(WordList)); | |
| word_list_ctor(new); | |
| strncpy(new->word, word, BUFSIZ); | |
| while (current->next != NULL) { | |
| current = current->next; | |
| } | |
| new->prev = current; | |
| new->index = (current->index + 1); | |
| current->next = new; | |
| } | |
| void word_list_dtor(WordList **list) { | |
| WordList *current = *list; | |
| WordList *next; | |
| while (current != NULL) { | |
| next = current->next; | |
| free(current); | |
| current = next; | |
| } | |
| *list = NULL; | |
| } | |
| void word_list_print(WordList *list) { | |
| WordList *current = list; | |
| while (current != NULL) { | |
| printf("%s\n", current->word); | |
| current = current->next; | |
| } | |
| } | |
| void word_list_print_reversed(WordList *list) { | |
| WordList *current = list; | |
| while (current != NULL) { | |
| printf("%s\n", current->word); | |
| current = current->prev; | |
| } | |
| } | |
| WordList *word_list_get_last(WordList *list) { | |
| WordList *current = list; | |
| while (current->next != NULL) { | |
| current = current->next; | |
| } | |
| return current; | |
| } | |
| int main(void) { | |
| WordList *list = (WordList*) malloc(sizeof(WordList)); | |
| word_list_ctor(list); | |
| word_list_append(list, "One"); | |
| word_list_append(list, "Two"); | |
| word_list_append(list, "Three"); | |
| word_list_append(list, "Four"); | |
| word_list_append(list, "Five"); | |
| word_list_print(list); | |
| WordList *last = word_list_get_last(list); | |
| word_list_print_reversed(last); | |
| word_list_dtor(&list); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment