Compare commits
21 Commits
ef8340ba6b
...
c3ce82150e
| Author | SHA1 | Date | |
|---|---|---|---|
| c3ce82150e | |||
| 6dd9514f80 | |||
| 2f3fee4cb2 | |||
| 0a30e04157 | |||
| e4a7f9ac28 | |||
| 42243120eb | |||
| 5857955dc0 | |||
| 3fdc249d86 | |||
| a6850b83c3 | |||
| 36e85dd4f4 | |||
| df9dedb1ad | |||
| cc318031be | |||
| 179965193e | |||
| 99477d3b58 | |||
| d4f6132544 | |||
| 782ff596bd | |||
| eb868c356a | |||
| 8151f21b38 | |||
| 7d54dac71a | |||
| 0e7802fb30 | |||
| 2c10463852 |
@ -1,7 +1,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
//TODO: grundlegende Stackfunktionen implementieren:
|
//DONE: grundlegende Stackfunktionen implementieren:
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
/* * `push`: legt ein Element oben auf den Stack,
|
||||||
* `pop`: entfernt das oberste Element,
|
* `pop`: entfernt das oberste Element,
|
||||||
* `top`: liefert das oberste Element zurück,
|
* `top`: liefert das oberste Element zurück,
|
||||||
@ -10,47 +10,52 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
StackNode *newNode = malloc(sizeof(StackNode));
|
// ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben
|
||||||
if (!newNode)
|
if (data != NULL)
|
||||||
return stack;
|
{
|
||||||
|
StackNode *newNode;
|
||||||
|
newNode = (StackNode *)malloc(sizeof(StackNode));
|
||||||
newNode->data = data;
|
newNode->data = data;
|
||||||
newNode->next = stack;
|
|
||||||
|
|
||||||
return newNode;
|
// ,-→ bedeutet Liste war leer - das neue Element hat keinen Nachfolger (next = NULL)
|
||||||
|
if (stack == NULL) {
|
||||||
|
newNode->next = NULL;
|
||||||
|
} else {
|
||||||
|
newNode->next = stack;
|
||||||
|
}
|
||||||
|
stack = newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
// freed by caller.)
|
// (Pointer to data has to be freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if(stack == NULL)
|
if(stack != NULL)
|
||||||
return NULL;
|
{
|
||||||
|
StackNode *nextNode = stack->next;
|
||||||
StackNode *newTopElement = stack->next;
|
|
||||||
free(stack);
|
free(stack);
|
||||||
|
stack = nextNode;
|
||||||
return newTopElement;
|
}
|
||||||
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (!stack)
|
if (stack != NULL) {
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return stack->data;
|
return stack->data;
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
StackNode *current = stack;
|
while (stack != NULL) {
|
||||||
|
stack = pop(stack);
|
||||||
while (current)
|
|
||||||
{
|
|
||||||
StackNode *next = current->next;
|
|
||||||
free(current);
|
|
||||||
current = next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -7,11 +7,11 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//DONE: passenden Datentyp als struct anlegen
|
||||||
typedef struct stack
|
typedef struct Node
|
||||||
{
|
{
|
||||||
void *data;
|
void *data;
|
||||||
struct stack *next;
|
struct Node* next;
|
||||||
} StackNode;
|
} StackNode;
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
|
|||||||
@ -1,9 +1,148 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "unity.h"
|
#include "unity/unity.h"
|
||||||
#include "unity_internals.h"
|
#include "unity/unity_internals.h"
|
||||||
|
|
||||||
void setUp(void) { }
|
/* wird nicht mehr gebraucht
|
||||||
void tearDown(void) { }
|
|
||||||
|
// Ein Blick in den Stack - listet den Stack-Inhalt der Reihe nach auf
|
||||||
|
void inspectStack(StackNode *stack)
|
||||||
|
{
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->data);
|
||||||
|
while (stack->next != NULL)
|
||||||
|
{
|
||||||
|
printf(" %d", *(int*)stack->next->data);
|
||||||
|
stack = stack->next;
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Der Stack ist leer\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Setup-Funktionen von Unity, die zumindest als Platzhalter definiert sein müssen
|
||||||
|
void setUp(void) {};
|
||||||
|
void tearDown(void) {};
|
||||||
|
|
||||||
|
void test_createNodeAbortsOnZeroData(void)
|
||||||
|
{
|
||||||
|
void *data = NULL;
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = push(stack, data);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(stack, NULL);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_createdNodeIsFirstInList(void)
|
||||||
|
{
|
||||||
|
int data = 42;
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = push(stack, &data);
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(&data, stack->data);
|
||||||
|
TEST_ASSERT_NULL(stack->next);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_createNodeCorrectOrder(void)
|
||||||
|
{
|
||||||
|
int data[3] = {0, 1, 2};
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = push(stack, &data[2]);
|
||||||
|
stack = push(stack, &data[1]);
|
||||||
|
stack = push(stack, &data[0]);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(&data[0], stack->data);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(&data[1], stack->next->data);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(&data[2], stack->next->next->data);
|
||||||
|
TEST_ASSERT_NULL(stack->next->next->next);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_removeNodeAbortsOnZero(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
TEST_ASSERT_NULL(pop(stack));
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_removeOnlyNodeEmptysList(void)
|
||||||
|
{
|
||||||
|
int data = 42;
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = pop(push(stack, &data));
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_removeNodeRemovesFirst(void)
|
||||||
|
{
|
||||||
|
int data[2] = {0, 1};
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = push(stack, &data[1]);
|
||||||
|
stack = push(stack, &data[0]);
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(&data[1], stack->data);
|
||||||
|
TEST_ASSERT_NULL(stack->next);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_outputTopFailsOnZero(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(top(stack));
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_outputTopCorrect(void)
|
||||||
|
{
|
||||||
|
int data[2] = {0, 1};
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = push(stack, &data[1]);
|
||||||
|
stack = push(stack, &data[0]);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(top(stack), stack->data);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Speicher freigabe lässt sich nicht mit Unity testen, am besten Programm mit valgrind ausführen
|
||||||
|
void test_clearStackFreesMemory(void)
|
||||||
|
{
|
||||||
|
int data[2] = {22, 17};
|
||||||
|
StackNode *lowNode = (StackNode *)malloc(sizeof(StackNode));
|
||||||
|
lowNode->next = NULL;
|
||||||
|
lowNode->data = &data[0];
|
||||||
|
StackNode *highNode = (StackNode *)malloc(sizeof(StackNode));
|
||||||
|
highNode->next = lowNode;
|
||||||
|
highNode->data = &data[1];
|
||||||
|
StackNode *stack = highNode;
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(lowNode);
|
||||||
|
TEST_ASSERT_NULL(highNode);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
void test_push_should_add_new_node_at_top(void)
|
void test_push_should_add_new_node_at_top(void)
|
||||||
{
|
{
|
||||||
@ -94,6 +233,29 @@ int main()
|
|||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
|
printf("\n============================\n Stack tests\n============================\n");
|
||||||
|
printf("-> Create Nodes (push)\n");
|
||||||
|
RUN_TEST(test_createNodeAbortsOnZeroData);
|
||||||
|
RUN_TEST(test_createdNodeIsFirstInList);
|
||||||
|
RUN_TEST(test_createNodeCorrectOrder);
|
||||||
|
|
||||||
|
printf("\n-> Remove Nodes (pop)\n");
|
||||||
|
RUN_TEST(test_removeNodeAbortsOnZero);
|
||||||
|
RUN_TEST(test_removeOnlyNodeEmptysList);
|
||||||
|
RUN_TEST(test_removeNodeRemovesFirst);
|
||||||
|
|
||||||
|
printf("\n-> Output Top (top)\n");
|
||||||
|
RUN_TEST(test_outputTopFailsOnZero);
|
||||||
|
RUN_TEST(test_outputTopCorrect);
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> 0c075ea (stackTest file added)
|
||||||
|
=======
|
||||||
|
=======
|
||||||
|
>>>>>>> ef8340ba6b9e7a4d6bac8d0eb9b4b987a9316088
|
||||||
RUN_TEST(test_push_should_add_new_node_at_top);
|
RUN_TEST(test_push_should_add_new_node_at_top);
|
||||||
RUN_TEST(test_push_multiple_should_chain_nodes_correctly);
|
RUN_TEST(test_push_multiple_should_chain_nodes_correctly);
|
||||||
RUN_TEST(test_top_should_return_null_on_empty_stack);
|
RUN_TEST(test_top_should_return_null_on_empty_stack);
|
||||||
@ -102,5 +264,9 @@ int main()
|
|||||||
RUN_TEST(test_pop_should_remove_single_element);
|
RUN_TEST(test_pop_should_remove_single_element);
|
||||||
RUN_TEST(test_pop_should_remove_top_node_only);
|
RUN_TEST(test_pop_should_remove_top_node_only);
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
|
>>>>>>> 0d561c0 (added stackTest)
|
||||||
|
=======
|
||||||
|
>>>>>>> ef8340ba6b9e7a4d6bac8d0eb9b4b987a9316088
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
80
I2_Dobble/test_stack.c
Normal file
80
I2_Dobble/test_stack.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include <bits/types/stack_t.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void inspectStack(StackNode *stack)
|
||||||
|
{
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->value);
|
||||||
|
while (stack->next != NULL)
|
||||||
|
{
|
||||||
|
printf(" %d", *(int*)stack->next->value);
|
||||||
|
stack = stack->next;
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Der Stack ist leer\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
StackNode *stack;
|
||||||
|
stack = NULL; // initialisierung mit NULL -> leere Liste
|
||||||
|
|
||||||
|
printf("...ein Element wird eingefügt...\n");
|
||||||
|
int toBeRemoved = 42;
|
||||||
|
stack = push(stack, &toBeRemoved);
|
||||||
|
|
||||||
|
inspectStack(stack);
|
||||||
|
|
||||||
|
printf("...das Element wird wieder entfernt...\n");
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
inspectStack(stack);
|
||||||
|
|
||||||
|
printf("...pop auf leeren Stack...\n");
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
inspectStack(stack);
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
int data[5] = {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
// alle 5 werte der reihe nach auf den Satck legen - 1 unten ... 5 oben
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
stack = push(stack, &data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//alle Elemente mit Test-Funktion ausgeben
|
||||||
|
inspectStack(stack);
|
||||||
|
|
||||||
|
// Elemente stück für Stück ausgeben und entfernen
|
||||||
|
printf("1. Element war: %d\n", *(int*)top(stack));
|
||||||
|
stack = pop(stack);
|
||||||
|
printf("2. Element war: %d\n", *(int*)top(stack));
|
||||||
|
stack = pop(stack);
|
||||||
|
printf("3. Element war: %d\n", *(int*)top(stack));
|
||||||
|
stack = pop(stack);
|
||||||
|
printf("4. Element war: %d\n", *(int*)top(stack));
|
||||||
|
stack = pop(stack);
|
||||||
|
printf("5. Element war: %d\n", *(int*)top(stack));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("============================\n Stack tests\n============================\n");
|
||||||
|
RUN_TEST(test_createNodeFailsOnZeroData);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
|
*/
|
||||||
Loading…
x
Reference in New Issue
Block a user