added and fixed stack

This commit is contained in:
Daniel Knüttel 2018-09-26 18:22:36 +02:00
parent e9801283e2
commit 712ff65af4
5 changed files with 65 additions and 4 deletions

View File

@ -3,7 +3,7 @@ CC=gcc
all: test_method_dispatcher
method_dispatcher: clean
$(CC) ../method_dispatcher.c test_method_dispatcher.c -o method_dispatcher
$(CC) -g ../method_dispatcher.c test_method_dispatcher.c -o method_dispatcher
test_method_dispatcher: method_dispatcher
valgrind ./method_dispatcher

20
stack.c
View File

@ -10,17 +10,31 @@ char bci_stack_t_push(bci_stack_t * stack, uint16_t value)
}
node->next = *stack;
node->value = value;
stack = &node;
*stack = node;
return 0;
}
char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result)
{
bci_stack_t this_node;
if(!*stack)
{
return 1;
}
*result = (*stack)->value;
*stack = (*stack)->next;
this_node = *stack;
*result = this_node->value;
*stack = this_node->next;
free(this_node);
return 0;
}
void bci_stack_t_del(bci_stack_t * stack)
{
bci_stack_t this_node;
while(*stack)
{
this_node = *stack;
*stack = this_node->next;
free(this_node);
}
}

View File

@ -11,4 +11,10 @@ typedef struct bci_stack_s
char bci_stack_t_push(bci_stack_t * stack, uint16_t value);
char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result);
/*
* Deletes the entire (!) stack.
*
* */
void bci_stack_t_del(bci_stack_t * stack);
#endif

12
test/Makefile Normal file
View File

@ -0,0 +1,12 @@
CC=gcc
all: test_stack
stack: clean
$(CC) -g ../stack.c test_stack.c -o stack
test_stack: stack
valgrind ./stack
clean:
-rm stack

29
test/test_stack.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include "../stack.h"
int main(void)
{
bci_stack_t stack = NULL;
int i;
uint16_t res;
char status = 0;
for(i = 0; i < 10; i++)
{
bci_stack_t_push(&stack, (uint16_t) i);
}
while(!status)
{
status = bci_stack_t_pop(&stack, &res);
printf("%d\n", res);
}
for(i = 0; i < 10; i++)
{
bci_stack_t_push(&stack, (uint16_t) i);
}
bci_stack_t_del(&stack);
return 0;
}