snp-lab-code/P06_Lösung_Andrin/list.c

84 lines
1.5 KiB
C
Raw Normal View History

2022-04-07 14:24:13 +02:00
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
#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;
}
}
}