3 Commits
5 changed files with 95 additions and 5 deletions
+15
View File
@@ -12,8 +12,23 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // 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) 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. // 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, // Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
+6
View File
@@ -41,9 +41,15 @@ unitTests: stacktest
test_stack: stacktest.o stack.o test_stack: stacktest.o stack.o
$(CC) $(FLAGS) $^ -o stacktest $(CC) $(FLAGS) $^ -o stacktest
test_numbers: numberstest.o numbers.o
$(CC) $(FLAGS) $^ -o numberstest
test_stack.o: stacktest.c test_stack.o: stacktest.c
$(CC) -c $(FLAGS) $< -o $@ $(CC) -c $(FLAGS) $< -o $@
test_numbers.o: numberstest.c
$(CC) -c $(FLAGS) $< -o $@
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
+1
View File
@@ -1,3 +1,4 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
+61
View File
@@ -0,0 +1,61 @@
#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;
}
+7
View File
@@ -8,12 +8,16 @@
* `clearStack`: gibt den gesamten Speicher frei. */ * `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
//Neue Node wird erstellt, Speicher wird zugewiesen
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode)); StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
if (newNode == NULL) { if (newNode == NULL) {
return NULL; return NULL;
} }
//Data und der zeiger auf den nächsten Node werden zugewiesen
newNode->data = data; newNode->data = data;
newNode->next = stack; newNode->next = stack;
return newNode; return newNode;
@@ -23,9 +27,11 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
//Wenn Stack leer, wird NULL zurückgegeben
if(stack == NULL) { if(stack == NULL) {
return NULL; return NULL;
} }
//Nächster Node wird gespeichert, aktuellerr Node wird freigegeben
StackNode *next = stack->next; StackNode *next = stack->next;
free(stack); free(stack);
return next; return next;
@@ -43,6 +49,7 @@ if(stack == NULL) {
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
//Speichert den aktuellen Node, freed ihn dann und geht zum nächsten
StackNode *temp; StackNode *temp;
while(stack != NULL) { while(stack != NULL) {
temp = stack; temp = stack;