From e54dc6a4d50afc4eeebd1e44d16a8bbd373d9a3f Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 7 Apr 2022 14:24:13 +0200 Subject: [PATCH] =?UTF-8?q?Upload=20files=20to=20'P06=5FL=C3=B6sung=5FAndr?= =?UTF-8?q?in'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- P06_Lösung_Andrin/list.c | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 P06_Lösung_Andrin/list.c diff --git a/P06_Lösung_Andrin/list.c b/P06_Lösung_Andrin/list.c new file mode 100644 index 0000000..fbe4631 --- /dev/null +++ b/P06_Lösung_Andrin/list.c @@ -0,0 +1,83 @@ +#include "list.h" +#include +#include +#include "person.h" + +int addPerson(node_t *coming,node_t *last,person_t *neu) { + node_t *p = malloc(sizeof(node_t)); + if(p == NULL) { + return 0; + } + last->next = p; + p->next = coming; + p->content = *neu; + return 1; +} + +int insert_list(node_t *liste,person_t *neu) { + + node_t *coming = liste->next; + node_t *last = liste; + + //falls Liste leer + if(coming == liste) { + return addPerson(coming,liste,neu); + } + + while(coming != liste) { + if(person_compare(&coming->content,neu) == 0){ + return 0; + } + if(person_compare(&coming->content,neu) > 0) { + return addPerson(coming,last,neu); + } + last = coming; + coming = coming->next; + } + return addPerson(coming,last,neu); +} + +int remove_list(node_t *liste,person_t *entfernen) { + + node_t *knoten = liste; + + do { + node_t *nextKnoten = knoten->next; + if(person_compare(entfernen,&nextKnoten->content) == 0) { + knoten->next = nextKnoten->next; + free(nextKnoten); + return 1; + } + + knoten = knoten->next; + } + while(knoten != liste); + + return 0; +} + +void clear_list(node_t *liste) { + + node_t *knoten = liste->next; + node_t *delete; + while(knoten != liste) { + delete = knoten; + knoten = knoten->next; + free(delete); + } + liste->next = liste; +} + +void show_list(node_t *liste) { + node_t *next = liste->next; + + if(next == liste) { + puts("Die Liste ist leer!"); + }else { + while(next != liste) { + printf("Name: %s First Name: %s Age: %d\n",next->content.name,next->content.first_name,next->content.age); + next = next->next; + } + } +} +