added and fixed stack
This commit is contained in:
parent
e9801283e2
commit
712ff65af4
|
@ -3,7 +3,7 @@ CC=gcc
|
||||||
all: test_method_dispatcher
|
all: test_method_dispatcher
|
||||||
|
|
||||||
method_dispatcher: clean
|
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
|
test_method_dispatcher: method_dispatcher
|
||||||
valgrind ./method_dispatcher
|
valgrind ./method_dispatcher
|
||||||
|
|
20
stack.c
20
stack.c
|
@ -10,17 +10,31 @@ char bci_stack_t_push(bci_stack_t * stack, uint16_t value)
|
||||||
}
|
}
|
||||||
node->next = *stack;
|
node->next = *stack;
|
||||||
node->value = value;
|
node->value = value;
|
||||||
stack = &node;
|
*stack = node;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result)
|
char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result)
|
||||||
{
|
{
|
||||||
|
bci_stack_t this_node;
|
||||||
if(!*stack)
|
if(!*stack)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
*result = (*stack)->value;
|
this_node = *stack;
|
||||||
*stack = (*stack)->next;
|
*result = this_node->value;
|
||||||
|
*stack = this_node->next;
|
||||||
|
free(this_node);
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
6
stack.h
6
stack.h
|
@ -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_push(bci_stack_t * stack, uint16_t value);
|
||||||
char bci_stack_t_pop(bci_stack_t * stack, uint16_t * result);
|
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
|
#endif
|
||||||
|
|
12
test/Makefile
Normal file
12
test/Makefile
Normal 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
29
test/test_stack.c
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user