generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
No commits in common. "600c414daa9a95820b1d3d89315ba6828b731b16" and "8a29c029ff41bb3c54fd4871a0e6d1b079ae4932" have entirely different histories.
600c414daa
...
8a29c029ff
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"stdio.h": "c",
|
|
||||||
"unity.h": "c"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
13
bintree.h
13
bintree.h
@ -17,20 +17,11 @@ typedef struct node
|
|||||||
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);
|
||||||
// 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,
|
||||||
// push the right top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
// Hilfsfunktion pushAllLeft (node) auf rechten nachfolger
|
|
||||||
// wenn neuer Baum dann pushallleft auf wurzel
|
|
||||||
// immer aufräumen!!!
|
|
||||||
// kein vorsortiertes array, sonst entarteter Baum
|
|
||||||
|
|
||||||
void *nextTreeData(TreeNode *root);
|
void *nextTreeData(TreeNode *root);
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
// sortierte Ausgabe
|
|
||||||
// aufrufen wie mit strtok
|
|
||||||
// welcher Baum durchlaufen
|
|
||||||
// immer null aufrufen bis fertig durchlaufen weil null zurückgegeben wird
|
|
||||||
void clearTree(TreeNode *root);
|
void clearTree(TreeNode *root);
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root);
|
unsigned int treeSize(const TreeNode *root);
|
||||||
//rekursiv die Anzahl der gesetzten Knoten ermitteln
|
|
||||||
#endif
|
#endif
|
||||||
5
makefile
5
makefile
@ -36,10 +36,7 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
test_numbers:
|
test_numbers:
|
||||||
$(CC) -o test_numbers test_numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
|
$(CC) -o test_numbers test__numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
|
||||||
|
|
||||||
test_stack:
|
|
||||||
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
|
|||||||
50
stack.c
50
stack.c
@ -1,65 +1,39 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
// TODO: grundlegende Stackfunktionen implementieren:
|
//TODO: grundlegende Stackfunktionen implementieren:
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
/* * `push`: legt ein Element oben auf den Stack,
|
||||||
* `pop`: entfernt das oberste Element,
|
* `pop`: entfernt das oberste Element,
|
||||||
* `top`: liefert das oberste Element zurück,
|
* `top`: liefert das oberste Element zurück,
|
||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
static StackNode *createNewElement()
|
|
||||||
{
|
|
||||||
return malloc(sizeof(StackNode));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
|
typedef struct stackNode
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
struct stackNode *next;
|
||||||
|
} StackNode;
|
||||||
|
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
StackNode *node = createNewElement();
|
|
||||||
if (node != NULL)
|
|
||||||
{
|
|
||||||
node->data = data;
|
|
||||||
node->next = stack;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
return stack;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack != NULL)
|
|
||||||
{
|
|
||||||
StackNode *currentElement = stack;
|
|
||||||
stack = stack->next;
|
|
||||||
|
|
||||||
free(currentElement);
|
|
||||||
}
|
|
||||||
return stack;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack != NULL)
|
|
||||||
{
|
|
||||||
return stack->data;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
StackNode *nextElement = stack;
|
|
||||||
|
|
||||||
while (stack != NULL)
|
|
||||||
{
|
|
||||||
nextElement = stack->next;
|
|
||||||
|
|
||||||
free(stack);
|
|
||||||
|
|
||||||
stack = nextElement;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
5
stack.h
5
stack.h
@ -8,11 +8,6 @@ The latest element is taken from the stack. */
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
typedef struct stackNode
|
|
||||||
{
|
|
||||||
void *data;
|
|
||||||
struct stackNode *next;
|
|
||||||
} StackNode;
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
|||||||
38
test_stack.c
38
test_stack.c
@ -1,38 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "unity/unity.h"
|
|
||||||
#include "stack.h"
|
|
||||||
|
|
||||||
void test_stackOrderIsCorrect()
|
|
||||||
{
|
|
||||||
int expectedValues[] = {1, 2, 3, 4};
|
|
||||||
const unsigned int exprectedLen = sizeof(expectedValues) / sizeof(expectedValues[0]);
|
|
||||||
unsigned int observedLen = 0;
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
for(int i = 0; i < exprectedLen; i++)
|
|
||||||
{
|
|
||||||
stack = push(stack, &expectedValues[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = exprectedLen-1; i >= 0 && stack != NULL; i--)
|
|
||||||
{
|
|
||||||
TEST_ASSERT_EQUAL(expectedValues[i], *(int *)top(stack));
|
|
||||||
observedLen++;
|
|
||||||
stack = pop(stack);
|
|
||||||
}
|
|
||||||
TEST_ASSERT_EQUAL_UINT32(exprectedLen, observedLen);
|
|
||||||
|
|
||||||
clearStack(stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUp(void) {}
|
|
||||||
void tearDown(void) {}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
RUN_TEST(test_stackOrderIsCorrect);
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user