1 Commits
Author SHA1 Message Date
od49ukup 7c6f6b0370 numbers.c fertig 2025-12-07 13:15:44 +01:00
5 changed files with 5 additions and 95 deletions
-15
View File
@@ -12,22 +12,7 @@
// 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)
{
if(root == NULL){
TreeNode *node;
node = (TreeNode*)malloc(sizeof(TreeNode));
if(node == NULL){
return NULL;
}
node->data = malloc(dataSize);
if(node->data == NULL){
free(node);
return NULL;
}
memcpy(node->data, data, dataSize);
node->left;
node->right;
}
}
// 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.
-6
View File
@@ -41,15 +41,9 @@ unitTests: stacktest
test_stack: stacktest.o stack.o
$(CC) $(FLAGS) $^ -o stacktest
test_numbers: numberstest.o numbers.o
$(CC) $(FLAGS) $^ -o numberstest
test_stack.o: stacktest.c
$(CC) -c $(FLAGS) $< -o $@
test_numbers.o: numberstest.c
$(CC) -c $(FLAGS) $< -o $@
# --------------------------
# Clean
# --------------------------
-1
View File
@@ -1,4 +1,3 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
-61
View File
@@ -1,61 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "numbers.h"
//Funktion, um nachzuprüfen, ob Test bestanden wurde
void check(char *msg, int expected, int actual){
printf("%s: ", msg);
printf("Erwartet: %d, Tatsächlich: %d\n", expected, actual);
if(expected == actual){
printf("Test bestanden\n");
}else{
printf("Test nicht bestanden\n");
}
}
int main(){
//prüfen, ob len < 2 NULL zurückgibt
unsigned int *numbers = createNumbers(1);
check("createNumbers mit len < 2", 1, numbers == NULL);
if(numbers != NULL){
free(numbers);
}
//prüfen, ob len = 10 ein Array zurückgibt
int len = 10;
numbers = createNumbers(len);
check("createNumbers mit len = 10", 1, numbers != NULL);
//prüfen, ob alle Zahlen zwischen 1 und 2*len liegen
int OK = 1;
for(int i = 0; i < len; i++){
if(numbers[i] < 1 || numbers[i] > 2*len){
OK = 0;
break;
}
}
check("Zahlenbereich bei createNumbers", 1, OK);
//prüfen, ob genau eine Zahl doppelt vorkommt
int duplikat = 0;
for(int i = 0; i < len; i++){
for(int j = i+1; j < len; j++){
if(numbers[i] == numbers[j]){
duplikat++;
}
}
}
check("Genau ein Duplikat bei createNumbers", 1, duplikat == 1);
free(numbers);
//Tests getDuplicate
int testArray1[] = {1,2,3,4,5,5,6,7,8,9};
int result = getDuplicate(testArray1, 10);
check("getDuplicate Test 1", 5, result);
int testArray2[] = {10,20,30,40,50,60,70,80,90,90};
result = getDuplicate(testArray2, 10);
check("getDuplicate Test 2", 90, result);
//prüfen bei ungültiger länge
result = getDuplicate(testArray2, 1);
check("getDuplicate mit len < 2", 0, result);
return 0;
}
+2 -9
View File
@@ -8,18 +8,14 @@
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
//Neue Node wird erstellt, Speicher wird zugewiesen
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
if (newNode == NULL) {
return NULL;
}
//Data und der zeiger auf den nächsten Node werden zugewiesen
newNode->data = data;
newNode->next = stack;
newNode ->data = data;
newNode ->next = stack;
return newNode;
}
@@ -27,11 +23,9 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.)
StackNode *pop(StackNode *stack)
{
//Wenn Stack leer, wird NULL zurückgegeben
if(stack == NULL) {
return NULL;
}
//Nächster Node wird gespeichert, aktuellerr Node wird freigegeben
StackNode *next = stack->next;
free(stack);
return next;
@@ -49,7 +43,6 @@ if(stack == NULL) {
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
//Speichert den aktuellen Node, freed ihn dann und geht zum nächsten
StackNode *temp;
while(stack != NULL) {
temp = stack;