generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2dc724e065 | |||
| 127c7aa8e7 | |||
|
|
8c0ff19529 | ||
|
|
b5e5a8052f | ||
|
|
7a20acd8f6 | ||
|
|
09db456716 | ||
|
|
fc5f249554 | ||
|
|
1536413888 | ||
|
|
c06f7c2b61 |
69
bintree.c
69
bintree.c
@ -12,7 +12,53 @@
|
||||
// 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(data!= NULL && dataSize > 0)
|
||||
{
|
||||
if(root == NULL) //Abbruchbedingung: Keine Wurzel vorhanden, deshalb fügen wir hier einen neuen Knote ein
|
||||
{
|
||||
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
if(newNode == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
newNode->data = malloc(dataSize);
|
||||
if(newNode->data == NULL)
|
||||
{
|
||||
free(newNode);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(newNode->data, data, dataSize);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
|
||||
if(isDuplicate!= NULL)
|
||||
{
|
||||
*isDuplicate = 0;
|
||||
}
|
||||
|
||||
return newNode;
|
||||
}
|
||||
int cmp = compareFct(root->data, data);
|
||||
if(cmp > 0)
|
||||
{
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else if(cmp < 0){
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isDuplicate) {
|
||||
*isDuplicate = 1;
|
||||
}
|
||||
else {
|
||||
// isDuplicate == NULL → trotzdem ein Duplikat einfügen (z.B. rechts)
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 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.
|
||||
@ -20,17 +66,40 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
// push the top node and push all its left nodes.
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
if(root == NULL)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
stackNode.top(root);
|
||||
}
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Erst linken Knoten löschen
|
||||
clearTree(root->left);
|
||||
|
||||
// Dann rechten Knoten löschen
|
||||
clearTree(root->right);
|
||||
|
||||
// Dann eigenen Speicher freigeben
|
||||
free(root->data);
|
||||
free(root);
|
||||
}
|
||||
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
if(root == NULL)
|
||||
{
|
||||
return numNodes;
|
||||
}
|
||||
return 1 + treeSize(root->left) + treeSize(root->right); //1, weil eine Wurzel gefunden wurde und dann immer plus eins fuer einen Teilbaum
|
||||
|
||||
}
|
||||
BIN
doble_initial.exe
Normal file
BIN
doble_initial.exe
Normal file
Binary file not shown.
@ -1 +1,2 @@
|
||||
Silvana;9944
|
||||
player1;3999
|
||||
|
||||
41
stack.c
41
stack.c
@ -10,24 +10,57 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
|
||||
if (!data)
|
||||
{
|
||||
return stack; //Nichts pushen
|
||||
}
|
||||
//if(stack && data){
|
||||
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
||||
if(!t)
|
||||
{
|
||||
return NULL; //Speicherfehler
|
||||
}
|
||||
t->next = stack;
|
||||
t->data = data;
|
||||
return t; //Gibt den ersten StackNode des Stacks zurueck
|
||||
//}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
|
||||
if(stack)
|
||||
{
|
||||
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
|
||||
free(stack);
|
||||
return t;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
|
||||
if(stack)
|
||||
{
|
||||
return stack->data;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
|
||||
while(stack)
|
||||
{
|
||||
StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
|
||||
stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
|
||||
free(tmp->data);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
4
stack.h
4
stack.h
@ -8,6 +8,10 @@ The latest element is taken from the stack. */
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct StackNode {
|
||||
void* data;
|
||||
struct StackNode *next;
|
||||
}StackNode;
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
64
test_stack.c
Normal file
64
test_stack.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
|
||||
//Testfunkionen zu push, pull, top & clearStack schreiben
|
||||
|
||||
void test(char *name, int condition) {
|
||||
if (condition) {
|
||||
printf("[OK] %s\n", name);
|
||||
} else {
|
||||
printf("[FAIL] %s\n", name);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
StackNode *stack = NULL;
|
||||
|
||||
// Werte dynamisch anlegen
|
||||
int *val1 = malloc(sizeof(int));
|
||||
*val1 = 5;
|
||||
stack = push(stack, val1);
|
||||
test("push(5) legt 5 oben auf den Stack", *(int*)stack->data == 5);
|
||||
|
||||
int *val2 = malloc(sizeof(int));
|
||||
*val2 = 6;
|
||||
stack = push(stack, val2);
|
||||
test("push(6) legt 6 oben auf den Stack", *(int*)stack->data == 6);
|
||||
|
||||
int *val3 = malloc(sizeof(int));
|
||||
*val3 = 24;
|
||||
stack = push(stack, val3);
|
||||
test("push(24) legt 24 oben auf den Stack", *(int*)stack->data == 24);
|
||||
|
||||
// Test top()
|
||||
int t = *(int*)top(stack);
|
||||
test("top() liefert 24", t == 24);
|
||||
|
||||
// Test pop()
|
||||
StackNode *tmp;
|
||||
|
||||
tmp = stack;
|
||||
stack = pop(stack);
|
||||
free(tmp->data); // Daten freigeben
|
||||
free(tmp); // Knoten freigeben
|
||||
test("pop() entfernt 24, 6 ist jetzt oben", *(int*)stack->data == 6);
|
||||
|
||||
tmp = stack;
|
||||
stack = pop(stack);
|
||||
free(tmp->data);
|
||||
free(tmp);
|
||||
test("pop() entfernt 6, 5 ist jetzt oben", *(int*)stack->data == 5);
|
||||
|
||||
tmp = stack;
|
||||
stack = pop(stack);
|
||||
free(tmp->data);
|
||||
free(tmp);
|
||||
test("pop() entfernt 5, Stack ist jetzt leer", stack == NULL);
|
||||
|
||||
// Am Ende Stack leeren (falls noch Elemente übrig)
|
||||
clearStack(stack);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user