-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
55 lines (47 loc) · 1.13 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
typedef struct intlist {
int i;
struct intlist* next;
} intlist;
intlist *addToList(intlist *list, int i) {
if( list == NULL) {
intlist *new = malloc(sizeof(intlist));
if(new == NULL){
return NULL;
}
new -> i = i; //zwei Schreibweisen (*new).i oder new -> i
new -> next = NULL;
return new;
}
if(list -> i >= i){
intlist *new = addToList(NULL, i);
if(new == NULL) return NULL;
new -> next = list;
return new;
}
list -> next = addToList(list -> next, i);
return list;
}
void printList(intlist *list){
while(list != NULL){
printf("%d\n", list -> i);
list = list -> next;
}
}
void freeList(intlist *list){
if(list == NULL){
freeList(list -> next);
free(list); //Standardfunktion zum freigeben von Speicher
}
}
int main(void){
intlist *list = NULL;
list = addToList(list, 5);
list = addToList(list, 2);
list = addToList(list, 4);
printList(list);
freeList(list);
}
// compiling mit Warnungen -> gcc -Wall -Wextra (weil in Wall nicht alle Fehler enthalten) -pedantic -g (debuginfos) -std=c99 -o List list.c
//valgrind <prog> Programm zum testen der Speichernutzung