From a3d0585ac16bac87fa6f532fa53b1657137ac5e6 Mon Sep 17 00:00:00 2001 From: Kristin Date: Sun, 7 Dec 2025 19:05:11 +0100 Subject: [PATCH] stack.c eingebunden --- bintree.c | 29 ++++++++++++++++++----------- bintree.h | 27 ++++++++++++++++----------- highscores.txt | 2 ++ makefile | 14 ++++++-------- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/bintree.c b/bintree.c index 82d2789..bf5a4ff 100644 --- a/bintree.c +++ b/bintree.c @@ -12,22 +12,34 @@ // Adds a copy of data's pointer destination to the tree using compareFct for // ordering. Accepts duplicates if isDuplicate is NULL, otherwise ignores // duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). + +void copyData(void *dest, const void *src, size_t size) { + unsigned char *d = dest; + const unsigned char *s = src; + for (size_t i = 0; i < size; i++) { + d[i] = s[i]; + } +} + TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) { + // isDuplicate initialisieren (auf 0 setzen) if (isDuplicate) { *isDuplicate = 0; } + // leerer Baum if (root == NULL) { TreeNode *node = malloc(sizeof(TreeNode)); node->data = malloc(dataSize); - memcpy(node->data, data, dataSize); + copyData(node->data, data, dataSize); node->left = NULL; node->right = NULL; return node; } + // mit compareFct <0 links >0 rechts =0 Duplikat int cmp = compareFct(data, root->data); if (cmp < 0) { @@ -36,7 +48,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); } else { - // Duplikat + // isDuplicate auf 1 setzen if (isDuplicate) { *isDuplicate = 1; } @@ -50,7 +62,6 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, // the root node and all left nodes first. On returning the next element, push // the top node and push all its left nodes. void *nextTreeData(TreeNode *root) { - static StackNode *stack = NULL; // Neue Iteration starten @@ -58,23 +69,20 @@ void *nextTreeData(TreeNode *root) { clearStack(&stack); TreeNode *curr = root; - - // Alle linken Kinder pushen while (curr != NULL) { stack = push(stack, curr); curr = curr->left; } } - // Wenn leer → fertig if (stack == NULL) - return NULL; + return NULL; // alles durchlaufen - // Oberstes Element lesen - TreeNode *node = (TreeNode *)stack->data; + // Oberstes Element abrufen + TreeNode *node = (TreeNode *)top(stack); stack = pop(stack); - // Jetzt den rechten Teilbaum verarbeiten + // Rechten Teilbaum pushen TreeNode *curr = node->right; while (curr != NULL) { stack = push(stack, curr); @@ -86,7 +94,6 @@ void *nextTreeData(TreeNode *root) { // Releases all memory resources (including data copies). void clearTree(TreeNode *root) { - if (root == NULL) return; diff --git a/bintree.h b/bintree.h index 25e16b2..1ced26b 100644 --- a/bintree.h +++ b/bintree.h @@ -5,19 +5,24 @@ typedef int (*CompareFctType)(const void *arg1, const void *arg2); -typedef struct node -{ - void *data; - struct node *left; - struct node *right; +typedef struct node { + void *data; + struct node *left; + struct node *right; } TreeNode; -// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates -// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). -TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate); -// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction. -// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element, -// push the top node and push all its left nodes. +void copyData(void *dest, const void *src, size_t size); + +// Adds a copy of data's pointer destination to the tree using compareFct for +// ordering. Accepts duplicates if isDuplicate is NULL, otherwise ignores +// duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). +TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, + CompareFctType compareFct, int *isDuplicate); +// Iterates over the tree given by root. Follows the usage of strtok. If tree is +// NULL, the next entry of the last tree given is returned in ordering +// direction. Use your implementation of a stack to organize the iterator. Push +// the root node and all left nodes first. On returning the next element, push +// the top node and push all its left nodes. void *nextTreeData(TreeNode *root); // Releases all memory resources (including data copies). void clearTree(TreeNode *root); diff --git a/highscores.txt b/highscores.txt index cf3230a..77970c8 100644 --- a/highscores.txt +++ b/highscores.txt @@ -1,3 +1,5 @@ +Kristin;6962 +Kristin;5975 krisp;4986 krisp;4985 Kristin;4972 diff --git a/makefile b/makefile index bc65fb7..805ac68 100644 --- a/makefile +++ b/makefile @@ -24,11 +24,12 @@ doble_initial: # -------------------------- # Selbst implementiertes Programm bauen # -------------------------- +# alle Objektdateien program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o - +# alle ausführbaren Dateien zu ausführbarem Programm linken doble : main.o $(program_obj_files) $(CC) $(FLAGS) $^ -o doble - +# Regel Kompilieren allgemein $(program_obj_files): %.o: %.c $(CC) -c $(FLAGS) $^ -o $@ @@ -42,8 +43,7 @@ BINARY_TEST_BIN = runBinaryTests # --- Stack Tests --- stackTests: stack.o test_stack.o - $(CC) $(FLAGS) -I$(unityfolder) -o $(STACK_TEST_BIN) \ - stack.o test_stack.o $(unityfolder)/unity.c + $(CC) $(FLAGS) -I$(unityfolder) -o $(STACK_TEST_BIN) stack.o test_stack.o $(unityfolder)/unity.c test_stack.o: test_stack.c $(CC) $(FLAGS) -I$(unityfolder) -c test_stack.c -o test_stack.o @@ -51,8 +51,7 @@ test_stack.o: test_stack.c # --- Numbers Tests --- numbersTests: numbers.o bintree.o stack.o test_numbers.o - $(CC) $(FLAGS) -I$(unityfolder) -o $(NUMBERS_TEST_BIN) \ - numbers.o bintree.o stack.o test_numbers.o $(unityfolder)/unity.c + $(CC) $(FLAGS) -I$(unityfolder) -o $(NUMBERS_TEST_BIN) numbers.o bintree.o stack.o test_numbers.o $(unityfolder)/unity.c test_numbers.o: test_numbers.c $(CC) $(FLAGS) -I$(unityfolder) -c test_numbers.c -o test_numbers.o @@ -60,8 +59,7 @@ test_numbers.o: test_numbers.c # --- Binary Tree Tests --- binaryTests: bintree.o stack.o test_binary.o - $(CC) $(FLAGS) -I$(unityfolder) -o $(BINARY_TEST_BIN) \ - bintree.o stack.o test_binary.o $(unityfolder)/unity.c + $(CC) $(FLAGS) -I$(unityfolder) -o $(BINARY_TEST_BIN) bintree.o stack.o test_binary.o $(unityfolder)/unity.c test_binary.o: test_binary.c $(CC) $(FLAGS) -I$(unityfolder) -c test_binary.c -o test_binary.o