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 /list-tail-node.c | |
| parent | 2863f4f1d2a6a6a8704454824d6c291d2e0d4b5c (diff) | |
| download | data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.tar.gz data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.tar.bz2 data-structures-154874afda4a8df885e51c01f7681f04fb0b8e61.zip | |
neue Verzeichnisstruktur
Diffstat (limited to 'list-tail-node.c')
| -rw-r--r-- | list-tail-node.c | 197 |
1 files changed, 0 insertions, 197 deletions
diff --git a/list-tail-node.c b/list-tail-node.c deleted file mode 100644 index edfcf31..0000000 --- a/list-tail-node.c +++ /dev/null | |||
| @@ -1,197 +0,0 @@ | |||
| 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<-- list_tail_type */ | ||
| 10 | typedef int T; | ||
| 11 | |||
| 12 | struct list_node { | ||
| 13 | struct list_node *next; | ||
| 14 | T data; | ||
| 15 | }; | ||
| 16 | |||
| 17 | struct list { | ||
| 18 | struct list_node *head, *tail; | ||
| 19 | }; | ||
| 20 | /* -->8-- */ | ||
| 21 | |||
| 22 | /* --8<-- list_tail_init */ | ||
| 23 | void | ||
| 24 | list_init(struct list *list) | ||
| 25 | { | ||
| 26 | list->head = NULL; | ||
| 27 | } | ||
| 28 | /* -->8-- */ | ||
| 29 | |||
| 30 | /* --8<-- list_tail_create_node */ | ||
| 31 | static struct list_node * | ||
| 32 | create_node(struct list_node *next, T data) | ||
| 33 | { | ||
| 34 | struct list_node *node = malloc(sizeof *node); | ||
| 35 | |||
| 36 | if ( node ) { | ||
| 37 | node->next = next; | ||
| 38 | node->data = data; | ||
| 39 | } | ||
| 40 | return node; | ||
| 41 | } | ||
| 42 | /* -->8-- */ | ||
| 43 | |||
| 44 | /* --8<-- list_tail_push_back */ | ||
| 45 | void | ||
| 46 | list_push_back(struct list *list, T data) | ||
| 47 | { | ||
| 48 | struct list_node *node = create_node(NULL, data); | ||
| 49 | |||
| 50 | if ( node ) { | ||
| 51 | if ( list->head == NULL ) { | ||
| 52 | list->head = node; | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | list->tail->next = node; | ||
| 56 | } | ||
| 57 | list->tail = node; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | ERROR("out of memory"); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | /* -->8-- */ | ||
| 64 | |||
| 65 | /* --8<-- list_tail_front */ | ||
| 66 | void | ||
| 67 | list_push_front(struct list *list, T data) | ||
| 68 | { | ||
| 69 | struct list_node *node = create_node(list->head, data); | ||
| 70 | |||
| 71 | if ( node ) { | ||
| 72 | if ( list->head == NULL ) { // Einfügen in eine leere Liste? | ||
| 73 | list->tail = node; | ||
| 74 | } | ||
| 75 | list->head = node; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | ERROR("out of memory"); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | /* -->8-- */ | ||
| 82 | |||
| 83 | /* --8<-- list_tail_pop_front */ | ||
| 84 | bool | ||
| 85 | list_pop_front(struct list *list, T *data) | ||
| 86 | { | ||
| 87 | if ( list->head ) { | ||
| 88 | struct list_node *next = list->head->next; | ||
| 89 | |||
| 90 | if ( data ) { | ||
| 91 | *data = list->head->data; | ||
| 92 | } | ||
| 93 | free(list->head); | ||
| 94 | list->head = next; | ||
| 95 | |||
| 96 | return true; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | return false; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | /* -->8-- */ | ||
| 103 | |||
| 104 | /* --8<-- list_tail_insert_next */ | ||
| 105 | void | ||
| 106 | list_insert_next(struct list *list, struct list_node *node, T data) | ||
| 107 | { | ||
| 108 | if ( node == NULL ) { // Am Anfang einfügen | ||
| 109 | list_push_front(list, data); | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | struct list_node *new_node = create_node(node->next, data); | ||
| 113 | |||
| 114 | if ( new_node ) { | ||
| 115 | if ( node->next == NULL ) { // Am Ende einfügen | ||
| 116 | list->tail = new_node; | ||
| 117 | } | ||
| 118 | node->next = new_node; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | ERROR("out of memory"); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | /* -->8-- */ | ||
| 126 | |||
| 127 | /* --8<-- list_tail_delete_next */ | ||
| 128 | void | ||
| 129 | list_delete_next(struct list *list, struct list_node *node, T *data) | ||
| 130 | { | ||
| 131 | if ( node == NULL ) { // Am Anfang entfernen | ||
| 132 | list_pop_front(list, data); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | if ( node->next ) { | ||
| 136 | struct list_node *old_node = node->next; | ||
| 137 | node->next = node->next->next; | ||
| 138 | |||
| 139 | if ( node->next == NULL ) { | ||
| 140 | list->tail = node; | ||
| 141 | } | ||
| 142 | |||
| 143 | if ( data ) { | ||
| 144 | *data = old_node->data; | ||
| 145 | } | ||
| 146 | free(old_node); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | /* -->8-- */ | ||
| 151 | |||
| 152 | /* --8<-- list_tail_empty */ | ||
| 153 | bool | ||
| 154 | list_empty(struct list *list) | ||
| 155 | { | ||
| 156 | return list->head == NULL; | ||
| 157 | } | ||
| 158 | /* -->8-- */ | ||
| 159 | |||
| 160 | /* --8<-- list_tail_free */ | ||
| 161 | void | ||
| 162 | list_free(struct list *list) | ||
| 163 | { | ||
| 164 | struct list_node *item, *next; | ||
| 165 | |||
| 166 | for ( item = list->head; item; item = next ) { | ||
| 167 | next = item->next; | ||
| 168 | free(item); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | /* -->8-- */ | ||
| 172 | |||
| 173 | int | ||
| 174 | main() | ||
| 175 | { | ||
| 176 | struct list list[1]; | ||
| 177 | |||
| 178 | list_init(list); | ||
| 179 | |||
| 180 | for ( int i = 0; i != 10; ++i ) | ||
| 181 | list_push_front(list, -i); | ||
| 182 | |||
| 183 | for ( int i = 0; i != 10; ++i ) | ||
| 184 | list_push_back(list, i); | ||
| 185 | |||
| 186 | while ( !list_empty(list) ) { | ||
| 187 | int i; | ||
| 188 | if ( list_pop_front(list, &i) ) | ||
| 189 | printf("%d\n", i); | ||
| 190 | else | ||
| 191 | ERROR("this shouldn't happen!"); | ||
| 192 | } | ||
| 193 | |||
| 194 | list_free(list); | ||
| 195 | |||
| 196 | return EXIT_SUCCESS; | ||
| 197 | } | ||
