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