From 4b96909c5c9dceca79e23d85e4dc3bf676284d55 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Wed, 25 May 2022 11:37:39 +0200 Subject: [PATCH] outsource to sperate header prettify print --- CMakeLists.txt | 2 +- List.h | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 131 +-------------------------------------------- 3 files changed, 142 insertions(+), 131 deletions(-) create mode 100644 List.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e6d24b..f3b550d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(untitled C) set(CMAKE_C_STANDARD 99) -add_executable(untitled main.c) +add_executable(untitled main.c List.h) diff --git a/List.h b/List.h new file mode 100644 index 0000000..8155afa --- /dev/null +++ b/List.h @@ -0,0 +1,140 @@ +// +// Created by lukas on 5/25/22. +// + +#ifndef UNTITLED_LIST_H +#define UNTITLED_LIST_H + +#include + +struct Node { + struct Node *next; + struct Node *prev; + void *dta; +}; +typedef struct Node Node; + +typedef struct { + Node *head; + unsigned length; +} List; + +List *newList() { + List *list = malloc(sizeof(List)); + return list; +} + +Node *_newNode(Node *next, Node *prev, void *dta) { + Node *newnode = malloc(sizeof(Node)); + newnode->next = next; + newnode->prev = prev; + newnode->dta = dta; + return newnode; +} + +Node *_traverseToLast(Node *node) { + if (node->next != NULL) { + return _traverseToLast(node->next); + } else { + return node; + } +} + +Node *_traverseToPos(Node *node, int pos) { + if (pos > 0) { + if (node->next != NULL) { + return _traverseToPos(node->next, pos - 1); + } else { + return NULL; + } + } else { + return node; + } +} + +void append(List *list, void *elem) { + if (list->head == NULL) { + // create head + list->head = _newNode(NULL, NULL, elem); + } else { + // append element + Node *last = _traverseToLast(list->head); + last->next = _newNode(NULL, last, elem); + } +} + +int _length(Node *node) { + if (node->next == NULL) { + return 1; + } else { + return 1 + _length(node->next); + } +} + +int length(List *list) { + if (list->head == NULL) { + return 0; + } + return _length(list->head); +} + +void insert(List *list, int position) { + +} + +void delete(List *list, int position) { + Node *todel = _traverseToPos(list->head, position); + + if (todel->prev == NULL) { + // we are the header + list->head = todel->next; + if (list->head != NULL) { + list->head->prev = NULL; + } + } else if (todel->next == NULL) { + // we are the last item + todel->prev->next = NULL; + } else { + todel->prev->next = todel->next; + todel->next->prev = todel->prev; + } + + free(todel); +} + +void deleteAll(List *list) { + while (list->head != NULL) { + delete(list, 0); + } +} + +void deleteList(List *list) { + deleteAll(list); + free(list); +} + +List *merge(List *list1, List *list2) { + return NULL; +} + +void *first(List *list) { + return list->head->dta; +} + +void *last(List *list) { + return _traverseToLast(list->head)->dta; +} + +void print(List *list, char *typeFormatter) { + Node *curr = list->head; + printf("{"); + while (curr != NULL) { + printf(typeFormatter, curr->dta); + printf(", "); + + curr = curr->next; + } + printf("}\n"); +} + +#endif //UNTITLED_LIST_H diff --git a/main.c b/main.c index 766818f..4b119c2 100644 --- a/main.c +++ b/main.c @@ -1,134 +1,5 @@ #include -#include -#include - -struct Node { - struct Node *next; - struct Node *prev; - void *dta; -}; -typedef struct Node Node; - -typedef struct { - Node *head; - unsigned length; -} List; - -List *newList() { - List *list = malloc(sizeof(List)); - return list; -} - -Node *_newNode(Node *next, Node *prev, void *dta) { - Node *newnode = malloc(sizeof(Node)); - newnode->next = next; - newnode->prev = prev; - newnode->dta = dta; - return newnode; -} - -Node *_traverseToLast(Node *node) { - if (node->next != NULL) { - return _traverseToLast(node->next); - } else { - return node; - } -} - -Node *_traverseToPos(Node *node, int pos) { - if (pos > 0) { - if (node->next != NULL) { - return _traverseToPos(node->next, pos - 1); - } else { - return NULL; - } - } else { - return node; - } -} - -void append(List *list, void *elem) { - if (list->head == NULL) { - // create head - list->head = _newNode(NULL, NULL, elem); - } else { - // append element - Node *last = _traverseToLast(list->head); - last->next = _newNode(NULL, last, elem); - } -} - -int _length(Node *node) { - if (node->next == NULL) { - return 1; - } else { - return 1 + _length(node->next); - } -} - -int length(List *list) { - if (list->head == NULL) { - return 0; - } - return _length(list->head); -} - -void insert(List *list, int position) { - -} - -void delete(List *list, int position) { - Node *todel = _traverseToPos(list->head, position); - - if (todel->prev == NULL) { - // we are the header - list->head = todel->next; - if (list->head != NULL) { - list->head->prev = NULL; - } - } else if (todel->next == NULL) { - // we are the last item - todel->prev->next = NULL; - } else { - todel->prev->next = todel->next; - todel->next->prev = todel->prev; - } - - free(todel); -} - -void deleteAll(List *list) { - while (list->head != NULL) { - delete(list, 0); - } -} - -void deleteList(List *list) { - deleteAll(list); - free(list); -} - -List *merge(List *list1, List *list2) { - return NULL; -} - -void *first(List *list) { - return list->head->dta; -} - -void *last(List *list) { - return _traverseToLast(list->head)->dta; -} - -void print(List *list, char *typeFormatter) { - Node *curr = list->head; - while (curr != NULL) { - printf(typeFormatter, curr->dta); - printf("\n"); - - curr = curr->next; - } -} +#include "List.h" int main() { printf("Hello, World!\n");