generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9afecd8502 |
22
stack.c
22
stack.c
@ -8,9 +8,29 @@
|
||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
static StackNode *createEmptyElement()
|
||||
{
|
||||
return malloc(sizeof(StackNode));
|
||||
}
|
||||
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
|
||||
|
||||
7
stack.h
7
stack.h
@ -8,6 +8,11 @@ The latest element is taken from the stack. */
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct node
|
||||
{
|
||||
void *data;
|
||||
struct node* next;
|
||||
}StackNode;
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
@ -22,4 +27,4 @@ void *top(StackNode *stack);
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
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