generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9437f26146 | |||
| 41e01c3af3 |
15
bintree.c
15
bintree.c
@ -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).
|
||||
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.
|
||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
||||
|
||||
10
makefile
10
makefile
@ -35,8 +35,14 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
unitTests:
|
||||
echo "needs to be implemented"
|
||||
unitTests: stacktest
|
||||
|
||||
|
||||
test_stack: stacktest.o stack.o
|
||||
$(CC) $(FLAGS) $^ -o stacktest
|
||||
|
||||
test_stack.o: stacktest.c
|
||||
$(CC) -c $(FLAGS) $< -o $@
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
|
||||
10
numbers.c
10
numbers.c
@ -16,7 +16,17 @@
|
||||
// creating random numbers.
|
||||
unsigned int *createNumbers(unsigned int len)
|
||||
{
|
||||
if(len < 2)
|
||||
return NULL;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
int count = 0;
|
||||
while(count < len - 1){
|
||||
int value = (rand() % (2 * len)) + 1;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||
|
||||
34
stack.c
34
stack.c
@ -8,26 +8,52 @@
|
||||
* `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;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
|
||||
if(stack == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return stack->data;
|
||||
}
|
||||
|
||||
// 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;
|
||||
stack = stack->next;
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
5
stack.h
5
stack.h
@ -8,7 +8,12 @@ The latest element is taken from the stack. */
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct{
|
||||
void *data;
|
||||
struct StackNode *next;
|
||||
|
||||
|
||||
}StackNode;
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
|
||||
58
stacktest.c
Normal file
58
stacktest.c
Normal file
@ -0,0 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
#include <stdlib.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");
|
||||
}
|
||||
|
||||
}
|
||||
//Schnappt sich oberstes Element
|
||||
int getTop(StackNode *stack){
|
||||
void *data = top(stack);
|
||||
return(int)data;
|
||||
|
||||
}
|
||||
|
||||
//Tests der einzelnen Stackfunktionen
|
||||
int main(){
|
||||
StackNode *stack = NULL;
|
||||
//Test push
|
||||
|
||||
stack = push(stack, (void*)10);
|
||||
check("Nach Push 10", 10, getTop(stack));
|
||||
|
||||
stack = push(stack, (void*)20);
|
||||
check("Nach Push 20", 20, getTop(stack));
|
||||
|
||||
//pop testen
|
||||
stack = pop(stack);
|
||||
check("Nach Pop", 10, getTop(stack));
|
||||
|
||||
//clearStack testen
|
||||
clearStack(stack);
|
||||
stack = NULL;
|
||||
check("Nach ClearStack", 0, getTop(stack));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user