diff options
author | scratko <m@scratko.xyz> | 2024-03-31 21:05:36 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-03-31 21:05:36 +0300 |
commit | 7d07ff825dc7cff7ee80b04346e0d2c49a91e190 (patch) | |
tree | f3bf4c72c22107b0d74143f09af39b0071106540 /stack.c | |
download | arithmetic-expression-computator-7d07ff825dc7cff7ee80b04346e0d2c49a91e190.tar.gz arithmetic-expression-computator-7d07ff825dc7cff7ee80b04346e0d2c49a91e190.tar.bz2 arithmetic-expression-computator-7d07ff825dc7cff7ee80b04346e0d2c49a91e190.zip |
Initial commit
Diffstat (limited to 'stack.c')
-rw-r--r-- | stack.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -0,0 +1,68 @@ +#include "stack.h" +#include "calls.h" + +#define QUANTITY_ADDRESSES 5 + +enum address_control { get_address, set_address }; +void* heap_base; + +void init_stack(stack *st) +{ + *st = 0; +} + +static void* address_management(void* address, enum address_control state) +{ + enum { null = 0 }; + static void* available_addresses[QUANTITY_ADDRESSES]; + int i; + if(state == set_address) { + for(i = 0; i < QUANTITY_ADDRESSES; ++i) { + if(!available_addresses[i]) { + available_addresses[i] = address; + return null; + } + } + return null; + } + for(i = 0; i < QUANTITY_ADDRESSES; ++i) { + if(available_addresses[i]) { + address = available_addresses[i]; + available_addresses[i] = 0; + return address; + } + } + return null; +} + + +void push_stack(stack *st, long num) +{ + struct item *tmp; + tmp = address_management(0, get_address); + if(!tmp) + tmp = sys_alloc(sizeof(struct item)); + if(!heap_base) + heap_base = tmp; + tmp->data = num; + tmp->next = *st; + *st = tmp; +} + +long pop_stack(stack *st) +{ + long num = (*st)->data; + address_management(*st, set_address); + *st = (*st)->next; + return num; +} + +int is_empty_stack(const stack *st) +{ + return *st ? 0 : 1; +} + +long top_stack(const stack *st) +{ + return (*st)->data; +} |