diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..cb8d469 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "C:/ProgramData/mingw64/mingw64/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..92df9c3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/Users/Max-R/I2Pr/info2Praktikum-DobleSpiel", + "program": "c:/Users/Max-R/I2Pr/info2Praktikum-DobleSpiel/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb879da --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/doble_initial.exe b/doble_initial.exe new file mode 100644 index 0000000..cd81778 Binary files /dev/null and b/doble_initial.exe differ diff --git a/makefile b/makefile index 514505e..038841f 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,7 @@ $(program_obj_files): %.o: %.c # -------------------------- # Unit Tests # -------------------------- - +unitTests: TEST_BIN = runTests unitTests: stack.o test_stack.o diff --git a/runTests.exe b/runTests.exe new file mode 100644 index 0000000..2b458d5 Binary files /dev/null and b/runTests.exe differ diff --git a/stack.c b/stack.c index 930fc2d..ef70b49 100644 --- a/stack.c +++ b/stack.c @@ -10,77 +10,74 @@ } StackNode;*/ // 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, * `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 *createNode(void *data) { + StackNode *node = + malloc(sizeof(StackNode)); // Speicher reservieren, Speicherplatz für das + // struct StackNode - StackNode *node = - malloc(sizeof(StackNode)); // Speicher reservieren, Speicherplatz für das - // struct StackNode + if (node == NULL) + return NULL; // Speicher konnte nicht reserviert werden - 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 - 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 + return node; // pointer auf den neuen Knoten zurückgeben } // 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 - 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; + } - if (newNode == NULL) { // wenn Speicher nicht reserviert werden konnte, wird - // stack unverändert zurückgegeben - return stack; - } + newNode->next = stack; // pointer verschieben - newNode->next = stack; // pointer verschieben - - return newNode; // Zeiger auf neuen Speicherbereich zurückgeben + return newNode; // Zeiger auf neuen Speicherbereich zurückgeben } -// 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) { +// 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) + return NULL; - if (stack == NULL) - return NULL; + StackNode *nextNode = stack->next; - StackNode *nextNode = stack->next; + free(stack); - free(stack); + stack = NULL; - stack = NULL; - - return nextNode; + return nextNode; } // Returns the data of the top element. void *top(StackNode *stack) { return stack != NULL ? stack->data : NULL; } // Clears stack and releases all memory. -void clearStack(StackNode *stack) { +void clearStack(StackNode *stack) +{ + while (stack != NULL) + { - while (stack != NULL) { - - StackNode *next = stack->next; - stack->data = NULL; - stack->prev = NULL; - stack->next = NULL; - - free(stack); - stack = next; - } - - return NULL; + StackNode *next = stack->next; + free(stack); + stack = next; + stack->data = NULL; + stack->next = NULL; + stack->prev = NULL; + } } \ No newline at end of file diff --git a/stack.h b/stack.h index c7bbef4..b1c8f86 100644 --- a/stack.h +++ b/stack.h @@ -19,6 +19,16 @@ typedef struct StackNode { StackNode *createNode(void *data); +typedef struct StackNode { + + void *data; + struct StackNode *next; + struct StackNode *prev; + +} StackNode; + +StackNode *createNode(void *data); + // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); diff --git a/stack.o b/stack.o new file mode 100644 index 0000000..177a3f5 Binary files /dev/null and b/stack.o differ diff --git a/test_stack.c b/test_stack.c index 2ad909f..dd9a6ca 100644 --- a/test_stack.c +++ b/test_stack.c @@ -56,4 +56,4 @@ int main(void) { RUN_TEST(test_clearStack); return UNITY_END(); -} +} \ No newline at end of file diff --git a/test_stack.o b/test_stack.o new file mode 100644 index 0000000..7332b5b Binary files /dev/null and b/test_stack.o differ