stack, with error
This commit is contained in:
parent
242c0e8e26
commit
9dd8161e10
30
stack.c
30
stack.c
@ -10,24 +10,54 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
// Neues Stack-Element erstellen
|
||||||
|
StackNode *newNode = malloc(sizeof(StackNode));
|
||||||
|
if (!newNode) {
|
||||||
|
return stack; // oder NULL, je nach Fehlerstrategie
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
newNode->data = data;
|
||||||
|
newNode->next = stack; // bisheriger Stack wird nach unten geschoben
|
||||||
|
|
||||||
|
return newNode; // neuer Kopf des Stacks
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if (stack == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
StackNode *newTop = stack->next;
|
||||||
|
|
||||||
|
// Daten gehen verloren!
|
||||||
|
// Caller KANN sie nicht freigeben.
|
||||||
|
free(stack);
|
||||||
|
|
||||||
|
return newTop;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if (stack == NULL)
|
||||||
|
return NULL; // kein Element im Stack
|
||||||
|
|
||||||
|
return stack->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
StackNode *current = stack;
|
||||||
|
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
StackNode *next = current->next;
|
||||||
|
free(current);
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
4
stack.h
4
stack.h
@ -8,6 +8,10 @@ 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);
|
||||||
|
|||||||
92
test_stack.c
Normal file
92
test_stack.c
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
void test_push_and_top(void);
|
||||||
|
|
||||||
|
void test_pop(void);
|
||||||
|
|
||||||
|
void test_clearStack(void);
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
RUN_TEST(test_push_and_top);
|
||||||
|
RUN_TEST(test_pop);
|
||||||
|
RUN_TEST(test_clearStack);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_push_and_top(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
int *a = malloc(sizeof(int)); *a = 10;
|
||||||
|
int *b = malloc(sizeof(int)); *b = 20;
|
||||||
|
int *c = malloc(sizeof(int)); *c = 30;
|
||||||
|
|
||||||
|
stack = push(stack, a);
|
||||||
|
stack = push(stack, b);
|
||||||
|
stack = push(stack, c);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(30, *(int*)top(stack));
|
||||||
|
|
||||||
|
// Cleanup data manually
|
||||||
|
void *data;
|
||||||
|
stack = pop(stack, &data); free(data);
|
||||||
|
stack = pop(stack, &data); free(data);
|
||||||
|
stack = pop(stack, &data); free(data);
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_pop(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
int *x = malloc(sizeof(int)); *x = 111;
|
||||||
|
int *y = malloc(sizeof(int)); *y = 222;
|
||||||
|
|
||||||
|
stack = push(stack, x);
|
||||||
|
stack = push(stack, y);
|
||||||
|
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
stack = pop(stack, &data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(222, *(int*)data);
|
||||||
|
free(data);
|
||||||
|
|
||||||
|
stack = pop(stack, &data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(111, *(int*)data);
|
||||||
|
free(data);
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_clearStack(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
int *p = malloc(sizeof(int)); *p = 7;
|
||||||
|
int *q = malloc(sizeof(int)); *q = 8;
|
||||||
|
|
||||||
|
stack = push(stack, p);
|
||||||
|
stack = push(stack, q);
|
||||||
|
|
||||||
|
// Pop to free data first!
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
stack = pop(stack, &data); free(data);
|
||||||
|
stack = pop(stack, &data); free(data);
|
||||||
|
|
||||||
|
clearStack(&stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user