diff options
| author | Thomas Schmucker <ts@its1.de> | 2022-04-09 09:43:53 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2022-04-09 09:43:53 +0200 |
| commit | 154874afda4a8df885e51c01f7681f04fb0b8e61 (patch) | |
| tree | 274817eb2793584b0e3d856de5504a614f2b4e89 /src/stack.c | |
| parent | 2863f4f1d2a6a6a8704454824d6c291d2e0d4b5c (diff) | |
| download | data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.tar.gz data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.tar.bz2 data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.zip | |
neue Verzeichnisstruktur
Diffstat (limited to 'src/stack.c')
| -rw-r--r-- | src/stack.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..48b9eba --- /dev/null +++ b/src/stack.c | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | /* Standard C */ | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | |||
| 6 | /* Project */ | ||
| 7 | #include "util.h" | ||
| 8 | |||
| 9 | /* --8<-- stack_type */ | ||
| 10 | typedef int T; | ||
| 11 | |||
| 12 | struct stack_item { | ||
| 13 | struct stack_item *next; | ||
| 14 | T data; | ||
| 15 | }; | ||
| 16 | |||
| 17 | struct stack { | ||
| 18 | struct stack_item *head; | ||
| 19 | }; | ||
| 20 | /* -->8-- */ | ||
| 21 | |||
| 22 | /* --8<-- stack_init */ | ||
| 23 | void | ||
| 24 | stack_init(struct stack *stack) | ||
| 25 | { | ||
| 26 | stack->head = NULL; | ||
| 27 | } | ||
| 28 | /* -->8-- */ | ||
| 29 | |||
| 30 | /* --8<-- stack_push */ | ||
| 31 | void | ||
| 32 | stack_push(struct stack *stack, T data) | ||
| 33 | { | ||
| 34 | struct stack_item *new_item; | ||
| 35 | |||
| 36 | if ( (new_item = malloc(sizeof *new_item)) != NULL ) { | ||
| 37 | new_item->data = data; | ||
| 38 | new_item->next = stack->head; | ||
| 39 | stack->head = new_item; | ||
| 40 | } | ||
| 41 | else | ||
| 42 | ERROR("out of memory"); | ||
| 43 | } | ||
| 44 | /* -->8-- */ | ||
| 45 | |||
| 46 | /* --8<-- stack_pop */ | ||
| 47 | bool | ||
| 48 | stack_pop(struct stack *stack, T *data) | ||
| 49 | { | ||
| 50 | if ( stack->head ) { | ||
| 51 | struct stack_item *next = stack->head->next; | ||
| 52 | |||
| 53 | if ( data ) { | ||
| 54 | *data = stack->head->data; | ||
| 55 | } | ||
| 56 | free(stack->head); | ||
| 57 | stack->head = next; | ||
| 58 | |||
| 59 | return true; | ||
| 60 | } | ||
| 61 | else | ||
| 62 | return false; | ||
| 63 | } | ||
| 64 | /* -->8-- */ | ||
| 65 | |||
| 66 | /* --8<-- stack_empty */ | ||
| 67 | bool | ||
| 68 | stack_empty(struct stack *stack) | ||
| 69 | { | ||
| 70 | return stack->head == NULL; | ||
| 71 | } | ||
| 72 | /* -->8-- */ | ||
| 73 | |||
| 74 | /* --8<-- stack_free */ | ||
| 75 | void | ||
| 76 | stack_free(struct stack *stack) | ||
| 77 | { | ||
| 78 | struct stack_item *item, *next; | ||
| 79 | |||
| 80 | for ( item = stack->head; item; item = next ) { | ||
| 81 | next = item->next; | ||
| 82 | free(item); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | /* -->8-- */ | ||
| 86 | |||
| 87 | int | ||
| 88 | main() | ||
| 89 | { | ||
| 90 | struct stack stack[1]; | ||
| 91 | |||
| 92 | stack_init(stack); | ||
| 93 | |||
| 94 | for ( int i = 0; i != 10; ++i ) | ||
| 95 | stack_push(stack, i); | ||
| 96 | |||
| 97 | /* | ||
| 98 | while ( !stack_empty(stack) ) { | ||
| 99 | int i; | ||
| 100 | if ( stack_pop(stack, &i) ) | ||
| 101 | printf("%d\n", i); | ||
| 102 | else | ||
| 103 | ERROR("this shouldn't happen!"); | ||
| 104 | } | ||
| 105 | */ | ||
| 106 | |||
| 107 | stack_free(stack); | ||
| 108 | |||
| 109 | return EXIT_SUCCESS; | ||
| 110 | } | ||
