generated from freudenreichan/info2Praktikum-DobleSpiel
wip
This commit is contained in:
parent
28e76fdbb7
commit
9afecd8502
22
stack.c
22
stack.c
@ -8,9 +8,29 @@
|
|||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
|
static StackNode *createEmptyElement()
|
||||||
|
{
|
||||||
|
return malloc(sizeof(StackNode));
|
||||||
|
}
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
|
StackNode *newElement = createEmptyElement();
|
||||||
|
newElement->data = data;
|
||||||
|
newElement->next = NULL;
|
||||||
|
if (stack == NULL)
|
||||||
|
{
|
||||||
|
stack = newElement;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StackNode *currentElement = stack;
|
||||||
|
while(currentElement->next != NULL)
|
||||||
|
{
|
||||||
|
currentElement = currentElement->next;
|
||||||
|
}
|
||||||
|
currentElement->next = newElement;
|
||||||
|
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
|
||||||
|
|||||||
5
stack.h
5
stack.h
@ -8,6 +8,11 @@ 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 node
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
struct node* 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);
|
||||||
|
|||||||
20
test_stack.c
Normal file
20
test_stack.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void testPushAddsElementCorrectly(void)
|
||||||
|
{
|
||||||
|
int a = 10;
|
||||||
|
StackNode *node = push(NULL, &a);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(node);
|
||||||
|
TEST_ASSERT_EQUAL_INT(a, *((int*)node->data)); //void Zeiger in integer-Zeiger umwandeln
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(testPushAddsElementCorrectly);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user