diff --git a/20200123/ausgabeumleitung.txt b/20200123/ausgabeumleitung.txt new file mode 100644 index 0000000000000000000000000000000000000000..7129bf59114603220059e16b419df562f23a6780 --- /dev/null +++ b/20200123/ausgabeumleitung.txt @@ -0,0 +1,15 @@ +cassini/home/peter/bo/2019ws/hp/20200123> ./stack-6 +stack overflow +cassini/home/peter/bo/2019ws/hp/20200123> ./stack-6 2>/dev/null +cassini/home/peter/bo/2019ws/hp/20200123> ./stack-6 >/dev/null +stack overflow +cassini/home/peter/bo/2019ws/hp/20200123> ./stack-5 +137 +7 +3 +cassini/home/peter/bo/2019ws/hp/20200123> ./stack-5 2>/dev/null +137 +7 +3 +cassini/home/peter/bo/2019ws/hp/20200123> ./stack-5 >/dev/null +cassini/home/peter/bo/2019ws/hp/20200123> diff --git a/20200123/fifo-1.c b/20200123/fifo-1.c new file mode 100644 index 0000000000000000000000000000000000000000..8bce6c07c152381c11ce367027b980a9330be3e0 --- /dev/null +++ b/20200123/fifo-1.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void push (int x) +{ + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + return fifo[0]; + fifo[0] = fifo[1]; + fifo[1] = fifo[2]; + fifo[2] = fifo[3]; + /* ... */ +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-2.c b/20200123/fifo-2.c new file mode 100644 index 0000000000000000000000000000000000000000..f95579893f62180b408ecb10756ac8938b3c9848 --- /dev/null +++ b/20200123/fifo-2.c @@ -0,0 +1,31 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void push (int x) +{ + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + fifo[0] = fifo[1]; + fifo[1] = fifo[2]; + fifo[2] = fifo[3]; + /* ... */ + return fifo[0]; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-3.c b/20200123/fifo-3.c new file mode 100644 index 0000000000000000000000000000000000000000..5214e1b28fb1b060bdaeea7be09e346a644e7f5d --- /dev/null +++ b/20200123/fifo-3.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void push (int x) +{ + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + int result = fifo[0]; + fifo[0] = fifo[1]; + fifo[1] = fifo[2]; + fifo[2] = fifo[3]; + /* ... */ + return result; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-4.c b/20200123/fifo-4.c new file mode 100644 index 0000000000000000000000000000000000000000..957d5f18f6c8fe9d7c057db3d2467221b9dc463e --- /dev/null +++ b/20200123/fifo-4.c @@ -0,0 +1,30 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void push (int x) +{ + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + int result = fifo[0]; + for (int i = 1; i < FIFO_SIZE; i++) + fifo[i - 1] = fifo[i]; + return result; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-5.c b/20200123/fifo-5.c new file mode 100644 index 0000000000000000000000000000000000000000..092c1cdb5863d5c7bdac98f48ca8527ca3520e6b --- /dev/null +++ b/20200123/fifo-5.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void push (int x) +{ + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + int result = fifo[0]; + for (int i = 1; i < FIFO_SIZE; i++) + fifo[i - 1] = fifo[i]; + return result; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + push (42); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-6.c b/20200123/fifo-6.c new file mode 100644 index 0000000000000000000000000000000000000000..2f055d6ce6df4c2fca950192053a6e008bed38b4 --- /dev/null +++ b/20200123/fifo-6.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void push (int x) +{ + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + int result = fifo[0]; + for (int i = 1; i < FIFO_SIZE; i++) + fifo[i - 1] = fifo[i]; + fifo_pointer--; + return result; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + push (42); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-7.c b/20200123/fifo-7.c new file mode 100644 index 0000000000000000000000000000000000000000..0d739115c136639f64c4d98c439af7e4d72fe420 --- /dev/null +++ b/20200123/fifo-7.c @@ -0,0 +1,35 @@ +#include <stdio.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_write_pointer = 0; +int fifo_read_pointer = 0; + +void push (int x) +{ + fifo[fifo_write_pointer++] = x; + if (fifo_write_pointer >= FIFO_SIZE) + fifo_write_pointer = 0; +} + +int pop (void) +{ + int result = fifo[fifo_read_pointer++]; + if (fifo_read_pointer >= FIFO_SIZE) + fifo_read_pointer = 0; + return result; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + push (42); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-8.c b/20200123/fifo-8.c new file mode 100644 index 0000000000000000000000000000000000000000..b4ff68713645e0a5782b516071022bf71a8c50ac --- /dev/null +++ b/20200123/fifo-8.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_write_pointer = 0; +int fifo_read_pointer = 0; + +void push (int x) +{ + fifo[fifo_write_pointer++] = x; + if (fifo_write_pointer >= FIFO_SIZE) + fifo_write_pointer = 0; + if (fifo_write_pointer == fifo_read_pointer) + { + fprintf (stderr, "fifo overflow\n"); + exit (1); + } +} + +int pop (void) +{ + if (fifo_read_pointer == fifo_write_pointer) + { + fprintf (stderr, "fifo underflow\n"); + exit (1); + } + else + { + int result = fifo[fifo_read_pointer++]; + if (fifo_read_pointer >= FIFO_SIZE) + fifo_read_pointer = 0; + return result; + } +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + push (42); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/fifo-9.c b/20200123/fifo-9.c new file mode 100644 index 0000000000000000000000000000000000000000..27f77412bac52fd70c52378c25531f03bd93d734 --- /dev/null +++ b/20200123/fifo-9.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FIFO_SIZE 10 + +int fifo[FIFO_SIZE]; +int fifo_write_pointer = 0; +int fifo_read_pointer = 0; + +void push (int x) +{ + int old_fifo_write_pointer = fifo_write_pointer; + fifo_write_pointer++; + if (fifo_write_pointer >= FIFO_SIZE) + fifo_write_pointer = 0; + if (fifo_write_pointer == fifo_read_pointer) + { + fprintf (stderr, "fifo overflow\n"); + exit (1); + } + else + fifo[old_fifo_write_pointer] = x; +} + +int pop (void) +{ + if (fifo_read_pointer == fifo_write_pointer) + { + fprintf (stderr, "fifo underflow\n"); + exit (1); + } + else + { + int result = fifo[fifo_read_pointer++]; + if (fifo_read_pointer >= FIFO_SIZE) + fifo_read_pointer = 0; + return result; + } +} + +int main (void) +{ + push (3); + push (7); + push (137); + for (int i = 0; i < 42; i++) + push (i); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + push (42); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/hp-musterloesung-20200123.pdf b/20200123/hp-musterloesung-20200123.pdf index e9a62c761f76ad691e9f502887840b9d8be789b6..a304827618f10201d5b3d7648dc325119db3d508 100644 Binary files a/20200123/hp-musterloesung-20200123.pdf and b/20200123/hp-musterloesung-20200123.pdf differ diff --git a/20200123/hp-musterloesung-20200123.tex b/20200123/hp-musterloesung-20200123.tex index 9cc611bff8d3d854ac2a2cb8c3791e322d91e4a0..61fdcbb5543b42d4e4351b924e23551b2069e263 100644 --- a/20200123/hp-musterloesung-20200123.tex +++ b/20200123/hp-musterloesung-20200123.tex @@ -349,7 +349,9 @@ \medskip - Die \textbf{Hinweise} auf der nächsten Seite beschreiben + Die \textbf{Hinweise} auf der nächsten Seite\footnote{Auf dem Übungszettel + befanden sich die Hinweise auf der nächsten Seite. In dieser Musterlösung + befinden sie sich direkt darunter.} beschreiben einen möglichen Weg, die Aufgabe zu lösen.\\ Es seht Ihnen frei, die Aufgabe auch auf andere Weise zu lösen. @@ -370,9 +372,9 @@ Damit können Sie sich gleichzeitig von der Bedingung lösen, daß die Länge des Arrays ein Vielfaches von 8 sein muß. \item - Gehen Sie nun von einem statichen zu einem dynamischen Array über, + Gehen Sie nun von einem statischen zu einem dynamischen Array über, und implementieren sie die Funktionen \lstinline{bit_array_init()}, - \lstinline{bit_array_done()} und \lstinline{bit_array_reseize()}. + \lstinline{bit_array_done()} und \lstinline{bit_array_resize()}. \end{itemize} \points{14} @@ -384,10 +386,102 @@ \solution - (wird nachgereicht) + Die hier vorgestellte Lösung folgt den Hinweisen. + \begin{itemize} + \item + \textbf{Setzen Sie zunächst voraus, daß das Array die konstante Länge 8 hat, + und schreiben Sie zunächst nur die Funktionen + \lstinline{bit_array_set()}, \lstinline{bit_array_flip()} und + \lstinline{bit_array_get()}.} + + Siehe: \gitfile{hp}{20200123}{loesung-3-1.c} + + Wir speichern in jedem der acht Bit einer \lstinline{uint8_t}-Variablen + jeweils eine Zahl, die 0 oder 1 sein kann. + Dies geschieht durch Setzen bzw.\ Löschen bzw.\ Umklappen einzelner Bits + in der Variablen. + + Das Programm enthält zusätzlich eine Funktion \lstinline{output()}, + mit der man sich den Inhalt des Arrays anzeigen lassen kann, + sowie ein Hauptprogramm \lstinline{main()}, um die Funktionen zu testen. + + \item + \textbf{Verallgemeinern Sie nun auf eine konstante Länge, + bei der es sich um ein Vielfaches von 8 handelt.} + + Siehe: \gitfile{hp}{20200123}{loesung-3-2.c} + + In diesem Programm setzen wir die Länge auf konstant \lstinline{LENGTH} Bits, + wobei es sich um eine Präprozessor-Konstante mit dem Wert 32 handelt. + + Um \lstinline{LENGTH} Bits zu speichern, benötigen wir ein Array + der Länge \lstinline{LENGTH / 8} Bytes. + + Um auf ein einzelnes Bit zuzugreifen, müssen wir zunächst ermitteln, + in welchem der Bytes sich befindet. Außerdem interessieren wir uns + für die Nummer des Bits innerhalb des Bytes. + Den Array-Index des Bytes erhalten wir, indem wir den Index des Bits + durch 8 dividieren. Der bei dieser Division verbleibende Rest ist die + Nummer des Bits innerhalb des Bytes. + + Diese Rechnungen führen wir in den drei Funktionen + \lstinline{bit_array_set()}, \lstinline{bit_array_flip()} und + \lstinline{bit_array_get()} durch. + (Diese ist eine eher unelegante Code-Verdopplung -- hier sogar eine Verdreifachung. + Für den Produktiveinsatz lohnt es sich, darüber nachzudenken, + wie man diese vermeiden kann, ohne gleichzeitig an Effizienz einzubüßen. + Hierfür käme z.\,B.\ ein Präprozessor-Makro in Frage. + Für die Lösung der Übungsaufgabe wird dies hingegen nicht verlangt.) + + \item + \textbf{Implementieren Sie nun die Überprüfung auf unsinnige Parameterwerte. + Damit können Sie sich gleichzeitig von der Bedingung lösen, + daß die Länge des Arrays ein Vielfaches von 8 sein muß.} + + Siehe: \gitfile{hp}{20200123}{loesung-3-3.c} + + Um weitere Code-Verdopplungen zu vermeiden, + führen wir eine Funktion \lstinline{check_index()} ein, + die alle Prüfungen durchführt. + + Wenn die Länge des Arrays kein Vielfaches von 8 ist, + wird das letzte Byte nicht vollständig genutzt. + Die einzige Schwierigkeit besteht darin, die korrekte Anzahl von Bytes + zu ermitteln, nämlich die Länge dividiert durch 8, aber nicht ab-, sondern + aufgerundet. Am elegantesten geht dies durch vorherige Addition von 7: + \lstinline{#define BYTES ((LENGTH + 7) / 8)}. + Es ist aber auch zulässig, die Anzahl der Bytes mit Hilfe einer + \lstinline{if}-Anweisung zu ermitteln: Länge durch 8 teilen und abrunden; + falls die Division nicht glatt aufging, um 1 erhöhen. + + \item + \textbf{Gehen Sie nun von einem statischen zu einem dynamischen Array über, + und implementieren sie die Funktionen \lstinline{bit_array_init()}, + \lstinline{bit_array_done()} und \lstinline{bit_array_resize()}.} + + Siehe: \gitfile{hp}{20200123}{loesung-3-4.c}. + Damit ist die Aufgabe gelöst. + + Aus den Präprozessor-Konstanten \lstinline{LENGTH} und \lstinline{BYTES} + werden nun globale \lstinline{int}-Variable. + Die Funktion \lstinline{bit_array_init()} berechnet die korrekten Werte + für diese Variablen und legt das Array mittels \lstinline{malloc()} dynamisch + an. Eine Größenänderung des Arrays erfolgt mittels \lstinline{realloc()}, + das Freigeben mittels \lstinline{free()}. + + Das Programm setzt Variable, die aktuell nicht verwendet werden, + auf den Wert \lstinline{0} bzw.\ \lstinline{NULL}. + Dies ermöglicht es der Funktion \lstinline{check_index()}, + auch zu prüfen, ob das Array vorher korrekt mit \lstinline{bit_array_init()} + erzeugt wurde -- oder ob es vielleicht schon wieder mit + \lstinline{bit_array_done()} freigegeben wurde. + + \end{itemize} + + \bigskip \begin{flushright} - \textit{Viel Erfolg -- auch in der Klausur!} + \textit{Viel Erfolg in der Klausur!} \end{flushright} \end{document} diff --git a/20200123/hp-uebung-20200123.pdf b/20200123/hp-uebung-20200123.pdf index 58efe2878f69459f4b76787c574c9257212cc9ba..737653183b7785090211a763d50b818d355a2293 100644 Binary files a/20200123/hp-uebung-20200123.pdf and b/20200123/hp-uebung-20200123.pdf differ diff --git a/20200123/hp-uebung-20200123.tex b/20200123/hp-uebung-20200123.tex index d94c7c93e461098bbf2560667162055754f4c02f..74e70c2c2348b232afde8aaef7432242cf856953 100644 --- a/20200123/hp-uebung-20200123.tex +++ b/20200123/hp-uebung-20200123.tex @@ -252,9 +252,9 @@ Damit können Sie sich gleichzeitig von der Bedingung lösen, daß die Länge des Arrays ein Vielfaches von 8 sein muß. \item - Gehen Sie nun von einem statichen zu einem dynamischen Array über, + Gehen Sie nun von einem statischen zu einem dynamischen Array über, und implementieren sie die Funktionen \lstinline{bit_array_init()}, - \lstinline{bit_array_done()} und \lstinline{bit_array_reseize()}. + \lstinline{bit_array_done()} und \lstinline{bit_array_resize()}. \end{itemize} \points{14} diff --git a/20200123/lists-1.c b/20200123/lists-1.c new file mode 100644 index 0000000000000000000000000000000000000000..a04067e1403601ef56dd706d6148c1d386884e82 --- /dev/null +++ b/20200123/lists-1.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +typedef struct +{ + int content; + node *next; +} node; + +int main (void) +{ + return 0; +} diff --git a/20200123/lists-2.c b/20200123/lists-2.c new file mode 100644 index 0000000000000000000000000000000000000000..f27d1d5af7c0c237f0d0286155380ef9452a497a --- /dev/null +++ b/20200123/lists-2.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +typedef struct node +{ + int content; + struct node *next; +} node; + +int main (void) +{ + return 0; +} diff --git a/20200123/lists-3.c b/20200123/lists-3.c new file mode 100644 index 0000000000000000000000000000000000000000..9c9029724efff25263813c5491add92504779d17 --- /dev/null +++ b/20200123/lists-3.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +typedef struct node +{ + int content; + struct node *next; +} node; + +int main (void) +{ + node node3 = { 3, NULL }; + node node7 = { 7, NULL }; + node node137 = { 137, NULL }; + + node *first = &node3; + + for (node *p = first; p; p = p->next) + printf ("%d\n", p->content); + + return 0; +} diff --git a/20200123/lists-4.c b/20200123/lists-4.c new file mode 100644 index 0000000000000000000000000000000000000000..e048736b85cc228c35f31644d003e00cdefc5496 --- /dev/null +++ b/20200123/lists-4.c @@ -0,0 +1,25 @@ +#include <stdio.h> + +typedef struct node +{ + int content; + struct node *next; +} node; + +int main (void) +{ + node node3 = { 3, NULL }; + node node7 = { 7, NULL }; + node node137 = { 137, NULL }; + + node3.next = &node7; + node7.next = &node137; + node137.next = NULL; + + node *first = &node3; + + for (node *p = first; p; p = p->next) + printf ("%d\n", p->content); + + return 0; +} diff --git a/20200123/lists-5.c b/20200123/lists-5.c new file mode 100644 index 0000000000000000000000000000000000000000..a0cc620a884c133dd89582de726139091bc9c5d0 --- /dev/null +++ b/20200123/lists-5.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +typedef struct node +{ + int content; + struct node *next; +} node; + +int main (void) +{ + node node3 = { 3, NULL }; + node node7 = { 7, NULL }; + node node137 = { 137, NULL }; + + node3.next = &node7; + node7.next = &node137; + node137.next = NULL; + + node node5 = { 5, NULL }; + node5.next = node3.next; + node3.next = &node5; + + node *first = &node3; + + for (node *p = first; p; p = p->next) + printf ("%d\n", p->content); + + return 0; +} diff --git a/20200123/lists-5.s b/20200123/lists-5.s new file mode 100644 index 0000000000000000000000000000000000000000..110bc231d950988e1f9a926e2c87167bab979457 --- /dev/null +++ b/20200123/lists-5.s @@ -0,0 +1,51 @@ + .file "lists-5.c" + .section .rodata.str1.1,"aMS",@progbits,1 +.LC0: + .string "%d\n" + .text + .globl main + .type main, @function +main: +.LFB11: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + pushq %rbx + .cfi_def_cfa_offset 24 + .cfi_offset 3, -24 + subq $72, %rsp + .cfi_def_cfa_offset 96 + movl $3, 48(%rsp) + movl $7, 32(%rsp) + movl $137, 16(%rsp) + movq $0, 24(%rsp) + leaq 16(%rsp), %rax + movq %rax, 40(%rsp) + movl $5, (%rsp) + leaq 32(%rsp), %rax + movq %rax, 8(%rsp) + movq %rsp, 56(%rsp) + leaq 48(%rsp), %rbx + leaq .LC0(%rip), %rbp +.L2: + movl (%rbx), %esi + movq %rbp, %rdi + movl $0, %eax + call printf@PLT + movq 8(%rbx), %rbx + testq %rbx, %rbx + jne .L2 + movl $0, %eax + addq $72, %rsp + .cfi_def_cfa_offset 24 + popq %rbx + .cfi_def_cfa_offset 16 + popq %rbp + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE11: + .size main, .-main + .ident "GCC: (Debian 6.3.0-18+deb9u1) 6.3.0 20170516" + .section .note.GNU-stack,"",@progbits diff --git a/20200123/loesung-3-1.c b/20200123/loesung-3-1.c new file mode 100644 index 0000000000000000000000000000000000000000..11ad08a28a2c16e645e4b55c1b0f4556246fd692 --- /dev/null +++ b/20200123/loesung-3-1.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdint.h> + +uint8_t buffer = 0; + +void bit_array_set (int i, int value) +{ + uint8_t mask = 1 << i; + if (value) + buffer |= mask; + else + buffer &= ~mask; +} + +void bit_array_flip (int i) +{ + uint8_t mask = 1 << i; + buffer ^= mask; +} + +int bit_array_get (int i) +{ + uint8_t mask = 1 << i; + if (buffer & mask) + return 1; + else + return 0; +} + +void output (void) +{ + for (int i = 0; i < 8; i++) + { + if (i % 4 == 0) + printf (" "); + printf ("%d", bit_array_get (i)); + } + printf ("\n"); +} + +int main (void) +{ + output (); + bit_array_set (2, 1); + output (); + bit_array_flip (7); + output (); + bit_array_set (2, 0); + output (); +} diff --git a/20200123/loesung-3-2.c b/20200123/loesung-3-2.c new file mode 100644 index 0000000000000000000000000000000000000000..e510904b033cf7d55737cee55ad08ecb9510f324 --- /dev/null +++ b/20200123/loesung-3-2.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdint.h> + +#define LENGTH 32 + +uint8_t buffer[LENGTH / 8] = { 0, 0, 0, 0 }; + +void bit_array_set (int i, int value) +{ + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + if (value) + buffer[byte_index] |= mask; + else + buffer[byte_index] &= ~mask; +} + +void bit_array_flip (int i) +{ + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + buffer[byte_index] ^= mask; +} + +int bit_array_get (int i) +{ + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + if (buffer[byte_index] & mask) + return 1; + else + return 0; +} + +void output (void) +{ + for (int i = 0; i < LENGTH; i++) + { + if (i % 4 == 0) + printf (" "); + printf ("%d", bit_array_get (i)); + } + printf ("\n"); +} + +int main (void) +{ + output (); + bit_array_set (12, 1); + output (); + bit_array_flip (31); + output (); + bit_array_set (12, 0); + output (); +} diff --git a/20200123/loesung-3-3.c b/20200123/loesung-3-3.c new file mode 100644 index 0000000000000000000000000000000000000000..4177464aebf41205169a6bb02b27ac972e3a4f4e --- /dev/null +++ b/20200123/loesung-3-3.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdint.h> +#include <error.h> + +#define LENGTH 29 +#define BYTES ((LENGTH + 7) / 8) + +uint8_t buffer[BYTES] = { 0, 0, 0, 0 }; + +void check_index (int i) +{ + if (i < 0 || i >= LENGTH) + error (1, 0, "index %d out of range (0, ..., %d)", i, LENGTH - 1); +} + +void bit_array_set (int i, int value) +{ + check_index (i); + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + if (value) + buffer[byte_index] |= mask; + else + buffer[byte_index] &= ~mask; +} + +void bit_array_flip (int i) +{ + check_index (i); + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + buffer[byte_index] ^= mask; +} + +int bit_array_get (int i) +{ + check_index (i); + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + if (buffer[byte_index] & mask) + return 1; + else + return 0; +} + +void output (void) +{ + for (int i = 0; i < LENGTH; i++) + { + if (i % 4 == 0) + printf (" "); + printf ("%d", bit_array_get (i)); + } + printf ("\n"); +} + +int main (void) +{ + output (); + bit_array_set (12, 1); + output (); + bit_array_flip (28); + output (); + bit_array_set (12, 0); + output (); + bit_array_flip (31); + output (); +} diff --git a/20200123/loesung-3-4.c b/20200123/loesung-3-4.c new file mode 100644 index 0000000000000000000000000000000000000000..1930580aae8e8b24528852d1dacf58051f6bd6cc --- /dev/null +++ b/20200123/loesung-3-4.c @@ -0,0 +1,101 @@ +#include <stdio.h> +#include <stdint.h> +#include <error.h> +#include <stdlib.h> + +int length = 0; +int bytes = 0; +uint8_t *buffer = NULL; + +void bit_array_init (int n) +{ + length = n; + bytes = (length + 7) / 8; + if (buffer) + free (buffer); + buffer = malloc (bytes * sizeof (uint8_t)); + for (int i = 0; i < bytes; i++) + buffer[i] = 0; +} + +void bit_array_resize (int new_n) +{ + length = new_n; + bytes = (length + 7) / 8; + buffer = realloc (buffer, bytes * sizeof (uint8_t)); +} + +void bit_array_done (void) +{ + free (buffer); + buffer = NULL; + length = 0; + bytes = 0; +} + +void check_index (int i) +{ + if (!buffer) + error (1, 0, "array not initialised"); + if (i < 0 || i >= length) + error (1, 0, "index %d out of range (0, ..., %d)", i, length - 1); +} + +void bit_array_set (int i, int value) +{ + check_index (i); + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + if (value) + buffer[byte_index] |= mask; + else + buffer[byte_index] &= ~mask; +} + +void bit_array_flip (int i) +{ + check_index (i); + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + buffer[byte_index] ^= mask; +} + +int bit_array_get (int i) +{ + check_index (i); + int byte_index = i / 8; + int bit_index = i % 8; + uint8_t mask = 1 << bit_index; + if (buffer[byte_index] & mask) + return 1; + else + return 0; +} + +void output (void) +{ + for (int i = 0; i < length; i++) + { + if (i % 4 == 0) + printf (" "); + printf ("%d", bit_array_get (i)); + } + printf ("\n"); +} + +int main (void) +{ + bit_array_init (29); + output (); + bit_array_set (12, 1); + output (); + bit_array_flip (28); + output (); + bit_array_set (12, 0); + output (); + bit_array_flip (31); + output (); + bit_array_done (); +} diff --git a/20200123/pwgen.txt b/20200123/pwgen.txt new file mode 100644 index 0000000000000000000000000000000000000000..dedad24d8f985e34a85f538dd15e0e7db6271068 --- /dev/null +++ b/20200123/pwgen.txt @@ -0,0 +1,21 @@ +cassini/home/peter/bo/2019ws/hp/20200123> pwgen 32 +CahleMohngai7joh8Shoo5yaJaejahZ6 lochie8fo2ohdoipoo9daeseegh1niuX +eedo3ahbangocha8aigh0ahc8too2uuL ruPahyoo9oong5eeg6aiW3Ouphaegooj +ufeoh9quae3ahNoh3aeshaafoil4aew5 taigheih5yaed3ea6ohWieXiu5Yu5die +Aeshae7OoQuiewiu5zoh4tai1eeg4wu4 ooquioLahva0ahcohnah7bai4chahrai +Euphoh0shoh4Thiethocuox5Iez9Eith shahvei6kieQuoh4oBae3wahngei4gae +Ae6Yiereg7faihoo6cho1Wu6eegoh8ku veeZahyiziejaesoh0gahw7oogheeng3 +uew0Apoozies9quiex3Ohphohkei8eic awai9shahyei4eiLooK8eipaezohseeh +aenepeNeech0koo4mahR0bae9eeteegh linguuD6phoht0Thohphai7eeCh4phee +pheidau7uchahchahghafiepee6Oht9U aipahquaemeiculemaiGh6fieboohi1I +yaepeenoh3SeQuiena1Xeirim6thooxa ohgh2thoh5Aijei6SheeYooguoNg4ga9 +Ohgush6ieRahsh7ieNaiY7gie3ootho5 eequ4enieSh4koo9EiBobaedooneshai +ahr1lee1ahghaa3paek4ek4Eish4uu7l phoh5aLek0chohR7eiRooV3Nequaisha +ooNieJu0Ojojohna8rutoosoi7feiWai yiek0kaiPeex8taZohyechahphoh3Aiz +oofah3thaibieshiuxie3Rieree8quo7 bohL0am2peew3aijei5Waizauwo9chiX +PeeTaeGhuY5roo8cheid3Geil2ooreex cie2shahs0tha1Da5kaez8oorohg6sha +us0foh4ohXaipoh5euboota6ounaitha tai1Thuepe7eenae9ra5poh0gahzie5w +theeho4queChoo8aekaekaephah4meew ethi4voefieGaiPesaz6eilathieth9i +fieb2rohYooch4zahw6Oujiewievuw4p AiRoo2aa9aePh5aGee2aefeC3iemoque +aidomee2ahThoo1feikohheingei9aeC be3sau2aemuo7thangaiRohve6thoh7o +quuateevoonahNgeh2eetei0it1wibuf zu1heiPhai5aib3Thai3see2gahnahyu diff --git a/20200123/stack-0.c b/20200123/stack-0.c new file mode 100644 index 0000000000000000000000000000000000000000..289aab7602e1e1f9f1e6433b858f295792499b1b --- /dev/null +++ b/20200123/stack-0.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +void push (int x) +{ +} + +int pop (void) +{ + return 42; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-1.c b/20200123/stack-1.c new file mode 100644 index 0000000000000000000000000000000000000000..d1de6e09d718b69a9d255b6c83f6f7815584b430 --- /dev/null +++ b/20200123/stack-1.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + stack[stack_pointer] = x; + stack_pointer++; +} + +int pop (void) +{ + return 42; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-2.c b/20200123/stack-2.c new file mode 100644 index 0000000000000000000000000000000000000000..20bed3660cffea7e0a9803451f69ef56a4a04b93 --- /dev/null +++ b/20200123/stack-2.c @@ -0,0 +1,27 @@ +#include <stdio.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + stack[stack_pointer++] = x; +} + +int pop (void) +{ + return 42; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-3.c b/20200123/stack-3.c new file mode 100644 index 0000000000000000000000000000000000000000..b20a444d628a88101416097c79b34e0669b24d21 --- /dev/null +++ b/20200123/stack-3.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + stack[stack_pointer++] = x; +} + +int pop (void) +{ + stack_pointer--; + return stack[stack_pointer]; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-4.c b/20200123/stack-4.c new file mode 100644 index 0000000000000000000000000000000000000000..0d738f95ff81ce1701b4aa8a12df30094b723851 --- /dev/null +++ b/20200123/stack-4.c @@ -0,0 +1,27 @@ +#include <stdio.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + stack[stack_pointer++] = x; +} + +int pop (void) +{ + return stack[--stack_pointer]; +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-5.c b/20200123/stack-5.c new file mode 100644 index 0000000000000000000000000000000000000000..38a187208208488bdb8e988d93442d57730e421e --- /dev/null +++ b/20200123/stack-5.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + if (stack_pointer < STACK_SIZE) + stack[stack_pointer++] = x; + else + { + fprintf (stderr, "stack overflow\n"); + exit (1); + } +} + +int pop (void) +{ + if (stack_pointer > 0) + return stack[--stack_pointer]; + else + { + fprintf (stderr, "stack underflow\n"); + exit (1); + } +} + +int main (void) +{ + push (3); + push (7); + push (137); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-6.c b/20200123/stack-6.c new file mode 100644 index 0000000000000000000000000000000000000000..be00e160384be5e4af05831547ed74b636c3bdf0 --- /dev/null +++ b/20200123/stack-6.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + if (stack_pointer < STACK_SIZE) + stack[stack_pointer++] = x; + else + { + fprintf (stderr, "stack overflow\n"); + exit (1); + } +} + +int pop (void) +{ + if (stack_pointer > 0) + return stack[--stack_pointer]; + else + { + fprintf (stderr, "stack underflow\n"); + exit (1); + } +} + +int main (void) +{ + for (int i = 0; i < 42; i++) + push (i); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/stack-7.c b/20200123/stack-7.c new file mode 100644 index 0000000000000000000000000000000000000000..b583bc281f9ae3acd673ec9ea4de75720084fb50 --- /dev/null +++ b/20200123/stack-7.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 10 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + if (stack_pointer < STACK_SIZE) + stack[stack_pointer++] = x; + else + { + fprintf (stderr, "stack overflow\n"); + exit (1); + } +} + +int pop (void) +{ + if (stack_pointer > 0) + return stack[--stack_pointer]; + else + { + fprintf (stderr, "stack underflow\n"); + exit (1); + } +} + +int main (void) +{ + push (3); + push (7); + push (137); + for (int i = 0; i < 42; i++) + printf ("%d\n", pop ()); + return 0; +} diff --git a/20200123/test.c b/20200123/test.c new file mode 100644 index 0000000000000000000000000000000000000000..d572ac1cc5d3d3d36de5901ed87326f155d7eaa1 --- /dev/null +++ b/20200123/test.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main (void) +{ + printf ("%d\n", 0b101010); + return 0; +} diff --git a/20200123/tree-1.c b/20200123/tree-1.c new file mode 100644 index 0000000000000000000000000000000000000000..d347186a019709e416529c1b73c32f268e0fce57 --- /dev/null +++ b/20200123/tree-1.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +typedef struct +{ + int content; + node *left, *right; +} node; + +int main (void) +{ + node *root = NULL; + return 0; +} diff --git a/20200123/tree-10.c b/20200123/tree-10.c new file mode 100644 index 0000000000000000000000000000000000000000..26a5575d8c0dbd139b08ca9365ad0a667223841b --- /dev/null +++ b/20200123/tree-10.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + node *save_root = root; + if (content < root->content) + { + root = root->left; + insert (content); + save_root->left = root; + } + else + { + root = root->right; + insert (content); + save_root->right = root; + } + root = save_root; + } +} + +void output (node *root) +{ + if (root) + { + if (root->left) + output (root->left); + printf ("%d\n", root->content); + if (root->right) + output (root->right); + } + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + insert (1117); + insert (42); + insert (13); + output (root); + return 0; +} diff --git a/20200123/tree-11.c b/20200123/tree-11.c new file mode 100644 index 0000000000000000000000000000000000000000..508b24316bf6696abc9225fac3fe7ef398a7e094 --- /dev/null +++ b/20200123/tree-11.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + node *save_root = root; + if (content < root->content) + { + root = root->left; + insert (content); + save_root->left = root; + } + else + { + root = root->right; + insert (content); + save_root->right = root; + } + root = save_root; + } +} + +void output (node *root) +{ + if (root) + { + output (root->left); + printf ("%d\n", root->content); + output (root->right); + } + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + insert (1117); + insert (42); + insert (13); + output (root); + return 0; +} diff --git a/20200123/tree-12.c b/20200123/tree-12.c new file mode 100644 index 0000000000000000000000000000000000000000..ff07e64e6dceb10266dc6d376d68fd15bc59e68b --- /dev/null +++ b/20200123/tree-12.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + node *save_root = root; + if (content < root->content) + { + root = root->left; + insert (content); + save_root->left = root; + } + else + { + root = root->right; + insert (content); + save_root->right = root; + } + root = save_root; + } +} + +void output (node *root) +{ + if (root) + { + output (root->left); + printf ("%d\n", root->content); + output (root->right); + } +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + insert (1117); + insert (42); + insert (13); + output (root); + return 0; +} diff --git a/20200123/tree-13.c b/20200123/tree-13.c new file mode 100644 index 0000000000000000000000000000000000000000..236930ed75a80984bfe343a582320470180db3ad --- /dev/null +++ b/20200123/tree-13.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +node *insert (node *root, int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + return p; + else + { + if (content < root->content) + { + root->left = insert (root->left, content); + return root; + } + else + { + root->right = insert (root->right, content); + return root; + } + } +} + +void output (node *root) +{ + if (root) + { + output (root->left); + printf ("%d\n", root->content); + output (root->right); + } +} + +int main (void) +{ + root = insert (root, 7); + root = insert (root, 137); + root = insert (root, 3); + root = insert (root, 1117); + root = insert (root, 42); + root = insert (root, 13); + output (root); + return 0; +} diff --git a/20200123/tree-14.c b/20200123/tree-14.c new file mode 100644 index 0000000000000000000000000000000000000000..a95a1fc5acec20d7fa0fcf714af2d5d746899c1c --- /dev/null +++ b/20200123/tree-14.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +node *insert (node *root, int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + return p; + else + { + if (content < root->content) + { + root->left = insert (root->left, content); + return root; + } + else + { + root->right = insert (root->right, content); + return root; + } + } +} + +void output (node *root) +{ + if (root) + { + output (root->left); + printf ("%d\n", root->content); + output (root->right); + } +} + +int main (void) +{ + insert (root, 7); + insert (root, 137); + insert (root, 3); + insert (root, 1117); + insert (root, 42); + insert (root, 13); + output (root); + return 0; +} diff --git a/20200123/tree-15.c b/20200123/tree-15.c new file mode 100644 index 0000000000000000000000000000000000000000..929d253f77de0cc15a6707dd499420965eb9daca --- /dev/null +++ b/20200123/tree-15.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +void insert (node **root, int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (*root == NULL) + *root = p; + else + { + if (content < root->content) + insert (&root->left, content); + else + insert (&root->right, content); + } +} + +void output (node *root) +{ + if (root) + { + output (root->left); + printf ("%d\n", root->content); + output (root->right); + } +} + +int main (void) +{ + node *root = NULL; + insert (&root, 7); + insert (&root, 137); + insert (&root, 3); + insert (&root, 1117); + insert (&root, 42); + insert (&root, 13); + output (root); + return 0; +} diff --git a/20200123/tree-16.c b/20200123/tree-16.c new file mode 100644 index 0000000000000000000000000000000000000000..be7163d4fdf57b897887bb861418e342e5061961 --- /dev/null +++ b/20200123/tree-16.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +void insert (node **root, int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (*root == NULL) + *root = p; + else + { + if (content < (*root)->content) + insert (&(*root)->left, content); + else + insert (&(*root)->right, content); + } +} + +void output (node *root) +{ + if (root) + { + output (root->left); + printf ("%d\n", root->content); + output (root->right); + } +} + +int main (void) +{ + node *root = NULL; + insert (&root, 7); + insert (&root, 137); + insert (&root, 3); + insert (&root, 1117); + insert (&root, 42); + insert (&root, 13); + output (root); + return 0; +} diff --git a/20200123/tree-2.c b/20200123/tree-2.c new file mode 100644 index 0000000000000000000000000000000000000000..7e591c3282329f0211c479dfa1b8c82f3dc755a3 --- /dev/null +++ b/20200123/tree-2.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +int main (void) +{ + node *root = NULL; + return 0; +} diff --git a/20200123/tree-3.c b/20200123/tree-3.c new file mode 100644 index 0000000000000000000000000000000000000000..b88b8671bbda9efe1031d09e3caa60ebeea8ccea --- /dev/null +++ b/20200123/tree-3.c @@ -0,0 +1,31 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +void insert (node *root, int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + root = p; +} + +void output (node *root) +{ + if (root) + printf ("%d\n", root->content); +} + +int main (void) +{ + node *root = NULL; + insert (root, 7); + output (root); + return 0; +} diff --git a/20200123/tree-3a.c b/20200123/tree-3a.c new file mode 100644 index 0000000000000000000000000000000000000000..8031ec72af9bcb97b83c46b296ab5f2464832fcd --- /dev/null +++ b/20200123/tree-3a.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +void insert (node *root, int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + root = p; +} + +void output (node *root) +{ + if (root) + printf ("%d\n", root->content); + else + printf ("root is NULL\n"); +} + +int main (void) +{ + node *root = NULL; + insert (root, 7); + output (root); + return 0; +} diff --git a/20200123/tree-3b.c b/20200123/tree-3b.c new file mode 100644 index 0000000000000000000000000000000000000000..5b47c41b61d5a76380bccefc98f2fd3057ad645b --- /dev/null +++ b/20200123/tree-3b.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + root = p; +} + +void output (void) +{ + if (root) + printf ("%d\n", root->content); + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + output (); + return 0; +} diff --git a/20200123/tree-4.c b/20200123/tree-4.c new file mode 100644 index 0000000000000000000000000000000000000000..db3b53dde184a82e1f85aa46e1f7e76b38e6b775 --- /dev/null +++ b/20200123/tree-4.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; +} + +void output (void) +{ + if (root) + printf ("%d\n", root->content); + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + output (); + return 0; +} diff --git a/20200123/tree-5.c b/20200123/tree-5.c new file mode 100644 index 0000000000000000000000000000000000000000..628d8803131ca8ff163a085ee8ff4cb6ede0bc88 --- /dev/null +++ b/20200123/tree-5.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + if (content > root->content) + root->right = p; + else + root->left = p; + } +} + +void output (void) +{ + if (root) + printf ("%d\n", root->content); + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + output (); + return 0; +} diff --git a/20200123/tree-6.c b/20200123/tree-6.c new file mode 100644 index 0000000000000000000000000000000000000000..f38634d004a020104e65e04cf437c87085dd0d18 --- /dev/null +++ b/20200123/tree-6.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + if (content > root->content) + root->right = p; + else + root->left = p; + } +} + +void output (void) +{ + if (root) + { + if (root->left) + printf ("%d\n", root->left->content); + printf ("%d\n", root->content); + if (root->right) + printf ("%d\n", root->right->content); + } + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + output (); + return 0; +} diff --git a/20200123/tree-7.c b/20200123/tree-7.c new file mode 100644 index 0000000000000000000000000000000000000000..df1dd66b3e1a5892235bcaa894888f8becb4de85 --- /dev/null +++ b/20200123/tree-7.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + node *save_root = root; + if (content > root->content) + { + root = root->left; + insert (content); + } + else + { + root = root->right; + insert (content); + } + root = save_root; + } +} + +void output (void) +{ + if (root) + { + if (root->left) + { + node *save_root = root; + root = root->left; + output (); + root = save_root; + } + printf ("%d\n", root->content); + if (root->right) + { + node *save_root = root; + root = root->right; + output (); + root = save_root; + } + } + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + insert (1117); + insert (42); + insert (13); + output (); + return 0; +} diff --git a/20200123/tree-8.c b/20200123/tree-8.c new file mode 100644 index 0000000000000000000000000000000000000000..fd3cafcad82fb4b9bbc6d7c905c1005d7c202f6e --- /dev/null +++ b/20200123/tree-8.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + node *save_root = root; + if (content > root->content) + { + root = root->left; + insert (content); + save_root->left = root; + } + else + { + root = root->right; + insert (content); + save_root->right = root; + } + root = save_root; + } +} + +void output (void) +{ + if (root) + { + if (root->left) + { + node *save_root = root; + root = root->left; + output (); + root = save_root; + } + printf ("%d\n", root->content); + if (root->right) + { + node *save_root = root; + root = root->right; + output (); + root = save_root; + } + } + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + insert (1117); + insert (42); + insert (13); + output (); + return 0; +} diff --git a/20200123/tree-9.c b/20200123/tree-9.c new file mode 100644 index 0000000000000000000000000000000000000000..6b44c1108c747f4e6021ddf92d771d77288af18d --- /dev/null +++ b/20200123/tree-9.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct node +{ + int content; + struct node *left, *right; +} node; + +node *root = NULL; + +void insert (int content) +{ + node *p = malloc (sizeof (node)); + p->content = content; + p->left = NULL; + p->right = NULL; + if (root == NULL) + root = p; + else + { + node *save_root = root; + if (content < root->content) + { + root = root->left; + insert (content); + save_root->left = root; + } + else + { + root = root->right; + insert (content); + save_root->right = root; + } + root = save_root; + } +} + +void output (void) +{ + if (root) + { + if (root->left) + { + node *save_root = root; + root = root->left; + output (); + root = save_root; + } + printf ("%d\n", root->content); + if (root->right) + { + node *save_root = root; + root = root->right; + output (); + root = save_root; + } + } + else + printf ("root is NULL\n"); +} + +int main (void) +{ + insert (7); + insert (137); + insert (3); + insert (1117); + insert (42); + insert (13); + output (); + return 0; +}