stack.c eingebunden

This commit is contained in:
Kristin 2025-12-07 19:05:11 +01:00
parent 68563ec297
commit a3d0585ac1
4 changed files with 42 additions and 30 deletions

View File

@ -12,22 +12,34 @@
// Adds a copy of data's pointer destination to the tree using compareFct for // Adds a copy of data's pointer destination to the tree using compareFct for
// ordering. Accepts duplicates if isDuplicate is NULL, otherwise ignores // ordering. Accepts duplicates if isDuplicate is NULL, otherwise ignores
// duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // 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, TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize,
CompareFctType compareFct, int *isDuplicate) { CompareFctType compareFct, int *isDuplicate) {
// isDuplicate initialisieren (auf 0 setzen)
if (isDuplicate) { if (isDuplicate) {
*isDuplicate = 0; *isDuplicate = 0;
} }
// leerer Baum
if (root == NULL) { if (root == NULL) {
TreeNode *node = malloc(sizeof(TreeNode)); TreeNode *node = malloc(sizeof(TreeNode));
node->data = malloc(dataSize); node->data = malloc(dataSize);
memcpy(node->data, data, dataSize); copyData(node->data, data, dataSize);
node->left = NULL; node->left = NULL;
node->right = NULL; node->right = NULL;
return node; return node;
} }
// mit compareFct <0 links >0 rechts =0 Duplikat
int cmp = compareFct(data, root->data); int cmp = compareFct(data, root->data);
if (cmp < 0) { if (cmp < 0) {
@ -36,7 +48,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize,
root->right = root->right =
addToTree(root->right, data, dataSize, compareFct, isDuplicate); addToTree(root->right, data, dataSize, compareFct, isDuplicate);
} else { } else {
// Duplikat // isDuplicate auf 1 setzen
if (isDuplicate) { if (isDuplicate) {
*isDuplicate = 1; *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 root node and all left nodes first. On returning the next element, push
// the top node and push all its left nodes. // the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) { void *nextTreeData(TreeNode *root) {
static StackNode *stack = NULL; static StackNode *stack = NULL;
// Neue Iteration starten // Neue Iteration starten
@ -58,23 +69,20 @@ void *nextTreeData(TreeNode *root) {
clearStack(&stack); clearStack(&stack);
TreeNode *curr = root; TreeNode *curr = root;
// Alle linken Kinder pushen
while (curr != NULL) { while (curr != NULL) {
stack = push(stack, curr); stack = push(stack, curr);
curr = curr->left; curr = curr->left;
} }
} }
// Wenn leer → fertig
if (stack == NULL) if (stack == NULL)
return NULL; return NULL; // alles durchlaufen
// Oberstes Element lesen // Oberstes Element abrufen
TreeNode *node = (TreeNode *)stack->data; TreeNode *node = (TreeNode *)top(stack);
stack = pop(stack); stack = pop(stack);
// Jetzt den rechten Teilbaum verarbeiten // Rechten Teilbaum pushen
TreeNode *curr = node->right; TreeNode *curr = node->right;
while (curr != NULL) { while (curr != NULL) {
stack = push(stack, curr); stack = push(stack, curr);
@ -86,7 +94,6 @@ void *nextTreeData(TreeNode *root) {
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) { void clearTree(TreeNode *root) {
if (root == NULL) if (root == NULL)
return; return;

View File

@ -5,19 +5,24 @@
typedef int (*CompareFctType)(const void *arg1, const void *arg2); typedef int (*CompareFctType)(const void *arg1, const void *arg2);
typedef struct node typedef struct node {
{ void *data;
void *data; struct node *left;
struct node *left; struct node *right;
struct node *right;
} TreeNode; } TreeNode;
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates void copyData(void *dest, const void *src, size_t size);
// 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); // Adds a copy of data's pointer destination to the tree using compareFct for
// 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. // ordering. Accepts duplicates if isDuplicate is NULL, otherwise ignores
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element, // duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
// push the top node and push all its left nodes. 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); void *nextTreeData(TreeNode *root);
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root); void clearTree(TreeNode *root);

View File

@ -1,3 +1,5 @@
Kristin;6962
Kristin;5975
krisp;4986 krisp;4986
krisp;4985 krisp;4985
Kristin;4972 Kristin;4972

View File

@ -24,11 +24,12 @@ doble_initial:
# -------------------------- # --------------------------
# Selbst implementiertes Programm bauen # Selbst implementiertes Programm bauen
# -------------------------- # --------------------------
# alle Objektdateien
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o 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) doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble $(CC) $(FLAGS) $^ -o doble
# Regel Kompilieren allgemein
$(program_obj_files): %.o: %.c $(program_obj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@ $(CC) -c $(FLAGS) $^ -o $@
@ -42,8 +43,7 @@ BINARY_TEST_BIN = runBinaryTests
# --- Stack Tests --- # --- Stack Tests ---
stackTests: stack.o test_stack.o stackTests: stack.o test_stack.o
$(CC) $(FLAGS) -I$(unityfolder) -o $(STACK_TEST_BIN) \ $(CC) $(FLAGS) -I$(unityfolder) -o $(STACK_TEST_BIN) stack.o test_stack.o $(unityfolder)/unity.c
stack.o test_stack.o $(unityfolder)/unity.c
test_stack.o: test_stack.c test_stack.o: test_stack.c
$(CC) $(FLAGS) -I$(unityfolder) -c test_stack.c -o test_stack.o $(CC) $(FLAGS) -I$(unityfolder) -c test_stack.c -o test_stack.o
@ -51,8 +51,7 @@ test_stack.o: test_stack.c
# --- Numbers Tests --- # --- Numbers Tests ---
numbersTests: numbers.o bintree.o stack.o test_numbers.o numbersTests: numbers.o bintree.o stack.o test_numbers.o
$(CC) $(FLAGS) -I$(unityfolder) -o $(NUMBERS_TEST_BIN) \ $(CC) $(FLAGS) -I$(unityfolder) -o $(NUMBERS_TEST_BIN) numbers.o bintree.o stack.o test_numbers.o $(unityfolder)/unity.c
numbers.o bintree.o stack.o test_numbers.o $(unityfolder)/unity.c
test_numbers.o: test_numbers.c test_numbers.o: test_numbers.c
$(CC) $(FLAGS) -I$(unityfolder) -c test_numbers.c -o test_numbers.o $(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 --- # --- Binary Tree Tests ---
binaryTests: bintree.o stack.o test_binary.o binaryTests: bintree.o stack.o test_binary.o
$(CC) $(FLAGS) -I$(unityfolder) -o $(BINARY_TEST_BIN) \ $(CC) $(FLAGS) -I$(unityfolder) -o $(BINARY_TEST_BIN) bintree.o stack.o test_binary.o $(unityfolder)/unity.c
bintree.o stack.o test_binary.o $(unityfolder)/unity.c
test_binary.o: test_binary.c test_binary.o: test_binary.c
$(CC) $(FLAGS) -I$(unityfolder) -c test_binary.c -o test_binary.o $(CC) $(FLAGS) -I$(unityfolder) -c test_binary.c -o test_binary.o