Compare commits

..

No commits in common. "624a7d8b41160cd924a8230f6ebddb2c89f45c76" and "ecb5f360e373f68a777d28a376c7c71aff6839c4" have entirely different histories.

7 changed files with 32 additions and 159 deletions

4
.gitignore vendored
View File

@ -1,3 +1 @@
doble_initial.exe highscores.txt
*.o
*.exe

View File

@ -1,4 +1,4 @@
t #include <string.h> #include <string.h>
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"

View File

@ -1,2 +1 @@
krisp;4986
player1;3999 player1;3999

View File

@ -29,27 +29,21 @@ program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
doble : main.o $(program_obj_files) doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble $(CC) $(FLAGS) $^ -o doble
$(program_obj_files): %.o: %.c $(program_obj_filesobj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@ $(CC) -c $(FLAGS) $^ -o $@
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unitTests:
TEST_BIN = runTests echo "needs to be implemented"
unitTests: stack.o test_stack.o
$(CC) $(FLAGS) -I$(unityfolder) -o $(TEST_BIN) stack.o test_stack.o $(unityfolder)/unity.c
test_stack.o: test_stack.c
$(CC) $(FLAGS) -I$(unityfolder) -c test_stack.c -o test_stack.o
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
rm -f *.o doble
else
del /f *.o doble del /f *.o doble
else
rm -f *.o doble
endif endif

85
stack.c
View File

@ -1,82 +1,33 @@
#include "stack.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "stack.h"
/*typedef struct { //TODO: grundlegende Stackfunktionen implementieren:
/* * `push`: legt ein Element oben auf den Stack,
void *data; * `pop`: entfernt das oberste Element,
struct StackNode *next; * `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
} StackNode;*/
// TODO: grundlegende Stackfunktionen implementieren:
/* `push`: legt ein Element oben auf den Stack,
* `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
// [A] -> [B] -> [C] -> NULL
// head -> stack.next
StackNode *createNode(void *data) {
StackNode *node =
malloc(sizeof(StackNode)); // Speicher reservieren, Speicherplatz für das
// struct StackNode
if (node == NULL)
return NULL; // Speicher konnte nicht reserviert werden
node->data = data; // Zeiger auf data neuer node
node->next = NULL; // nächster Zeiger ist NULL, Ende der Liste
return node; // pointer auf den neuen Knoten zurückgeben
}
// 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)
{
StackNode *newNode = createNode(data); // Speicher für neuen Knoten
// reservieren
if (newNode == NULL) { // wenn Speicher nicht reserviert werden konnte, wird
// stack unverändert zurückgegeben
return stack;
}
newNode->next = stack; // pointer verschieben
return newNode; // Zeiger auf neuen Speicherbereich zurückgeben
} }
// Deletes the top element of the stack (latest added element) and releases its // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// memory. (Pointer to data has to be freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) { StackNode *pop(StackNode *stack)
{
if (stack == NULL)
return NULL;
StackNode *nextNode = stack->next;
free(stack);
stack = NULL;
return nextNode;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) { return stack != NULL ? stack->data : NULL; } void *top(StackNode *stack)
{
}
// Clears stack and releases all memory. // Clears stack and releases all memory.
void *clearStack(StackNode *stack) { void clearStack(StackNode *stack)
{
while (stack != NULL) {
StackNode *next = stack->next;
free(stack);
stack = next;
}
return NULL;
} }

24
stack.h
View File

@ -1,35 +1,25 @@
#ifndef STACK_H #ifndef STACK_H
#define STACK_H #define STACK_H
/* A stack is a special type of queue which uses the LIFO (last in, first out) /* A stack is a special type of queue which uses the LIFO (last in, first out) principle.
principle. This means that with each new element all other elements are pushed This means that with each new element all other elements are pushed deeper into the stack.
deeper into the stack. The latest element is taken from the stack. */ 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;
struct StackNode *prev;
} StackNode;
StackNode *createNode(void *data);
// 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);
// Deletes the top element of the stack (latest added element) and releases its // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// memory. (Pointer to data has to be freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack); StackNode *pop(StackNode *stack);
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack); void *top(StackNode *stack);
// Clears stack and releases all memory. // Clears stack and releases all memory.
StackNode *clearStack(StackNode *stack); void clearStack(StackNode *stack);
#endif #endif

View File

@ -1,59 +0,0 @@
#include "unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
void test_createNode(void) {
int testInt = 26;
StackNode *testNode = createNode(&testInt);
TEST_ASSERT_NOT_NULL(testNode);
TEST_ASSERT_EQUAL_PTR(&testInt, testNode->data);
TEST_ASSERT_NULL(testNode->next);
free(testNode);
}
void test_pushDataToStack(void) {}
void test_deleteTopElement(void) {}
void test_returnData(void) {}
void test_clearStack(void) {
int testInts[] = {1, 2, 3, 4, 5};
StackNode *testStack = NULL;
for (int i = 0; i < 5; i++) {
testStack = push(testStack, &testInts[i]);
}
testStack = clearStack(testStack);
TEST_ASSERT_NULL(testStack);
}
void setUp(void) {}
void tearDown(void) {}
int main(void) {
UNITY_BEGIN();
printf("------------------------stack test------------------------\n");
RUN_TEST(test_createNode);
RUN_TEST(test_pushDataToStack);
RUN_TEST(test_deleteTopElement);
RUN_TEST(test_returnData);
RUN_TEST(test_clearStack);
RUN_TEST(test_clearStack);
return UNITY_END();
}