Added stack function

This commit is contained in:
Thilo 2025-12-07 15:16:00 +01:00
parent bed93125ef
commit 9363ed22aa
6 changed files with 114 additions and 0 deletions

28
.vscode/tasks.json vendored Normal file
View File

@ -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"
}

BIN
doble_initial.exe Normal file

Binary file not shown.

View File

@ -1 +1,2 @@
player_name;4980
player1;3999

47
stack.c
View File

@ -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;
}
}

View File

@ -8,6 +8,12 @@ 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);

32
test_stack.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}