diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..15bd63e --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "C:\\ProgramData\\mingw64\\mingw64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/doble_initial.exe b/doble_initial.exe new file mode 100644 index 0000000..5d14d7f Binary files /dev/null and b/doble_initial.exe differ diff --git a/highscores.txt b/highscores.txt index 4edd5a7..204bdab 100644 --- a/highscores.txt +++ b/highscores.txt @@ -1 +1,2 @@ +player_name;4980 player1;3999 diff --git a/stack.c b/stack.c index e3a90d4..8e7076e 100644 --- a/stack.c +++ b/stack.c @@ -10,24 +10,71 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { + StackNode *newNode = (StackNode *)malloc(sizeof(StackNode)); + + if(newNode == NULL) + { + perror("Fehler! Der Speicher für den neuen StackNode konnte nicht allokiert werden!\n"); + return stack; + } + + newNode->data = data; //saves data in newNode + newNode->next = stack; //pushes the newNode on top of stack (newNode->next points to previous Top-Node) + 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) { + if (stack == NULL) + { + perror("Fehler! Stack darf zum poppen nicht leer sein!\n"); + return stack; + } + + StackNode *old = stack; //temporary pointer to old top of stack + stack= stack->next; //update stack pointer to point to next element + + free(old); //free the memory of the old StackNode (old->data is not beeing freed) + + return stack; } // Returns the data of the top element. void *top(StackNode *stack) { + if (stack == NULL) + { + perror("Fehler! Kann von einem leeren Stack nicht das oberste Element lesen!\n"); + return NULL; + } + void *data = stack->data; + return data; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { + StackNode *current = stack; + StackNode *next; + while (current != NULL) + { + next = current->next; + + if (current->data != NULL) + { + free(current->data); + current->data = NULL; + } + + free(current); + current = NULL; + + current = next; + } } \ No newline at end of file diff --git a/stack.h b/stack.h index f7d542d..79f2562 100644 --- a/stack.h +++ b/stack.h @@ -8,6 +8,12 @@ The latest element is taken from the stack. */ #include //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); diff --git a/test_stack.c b/test_stack.c new file mode 100644 index 0000000..b0579e9 --- /dev/null +++ b/test_stack.c @@ -0,0 +1,32 @@ +#include +#include +#include "stack.h" + +void test_on_empty_stack(void) +{ + StackNode *stack = malloc(sizeof(StackNode)); + + StackNode *a = pop(stack); + + printf("%s", stack); +} + +void test_push(void) +{ + int *test_value = malloc(sizeof(int)); + test_value = (int*)1; + StackNode *stack = malloc(sizeof(StackNode)); + + StackNode *new_stack = push(stack, test_value); + + +} + +int main() +{ + printf("============================\nStack Tests\n============================\n"); + + test_on_empty_stack; + + return EXIT_SUCCESS; +} \ No newline at end of file