diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-08-27 08:26:59 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-08-27 08:26:59 +0200 |
| commit | 4b3ab034720dc4d5afda8db866de96caf2aa1c29 (patch) | |
| tree | 7ab07e58b264d6a40bffd5f2fbe81ed84b0c7df2 | |
| parent | fc01271dfd420f5dae9c074cd357e8108c6fa0b1 (diff) | |
| download | data-structures-4b3ab034720dc4d5afda8db866de96caf2aa1c29.tar.gz data-structures-4b3ab034720dc4d5afda8db866de96caf2aa1c29.tar.bz2 data-structures-4b3ab034720dc4d5afda8db866de96caf2aa1c29.zip | |
zwei neue Funktionen, um das nächste Element einer Liste hinzuzufügen oder zu löschen.
| -rw-r--r-- | list.c | 30 |
1 files changed, 30 insertions, 0 deletions
| @@ -32,6 +32,23 @@ list_add(struct list_item *next, T data) | |||
| 32 | return new_item; | 32 | return new_item; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | void | ||
| 36 | list_insert_next(struct list_item *node, T data) | ||
| 37 | { | ||
| 38 | struct list_item *new_item; | ||
| 39 | |||
| 40 | // TODO: Debuggen und nochmal richtig drüber nachdenken! | ||
| 41 | new_item = malloc(sizeof *new_item); | ||
| 42 | if ( new_item != NULL ) { | ||
| 43 | new_item->next = node->next; | ||
| 44 | new_item->data = data; | ||
| 45 | |||
| 46 | node->next = new_item; | ||
| 47 | } | ||
| 48 | else | ||
| 49 | ERROR("out of memory"); | ||
| 50 | } | ||
| 51 | |||
| 35 | struct list_item * | 52 | struct list_item * |
| 36 | list_delete(struct list_item *list, T data) | 53 | list_delete(struct list_item *list, T data) |
| 37 | { | 54 | { |
| @@ -56,6 +73,19 @@ list_delete(struct list_item *list, T data) | |||
| 56 | return list; | 73 | return list; |
| 57 | } | 74 | } |
| 58 | 75 | ||
| 76 | void | ||
| 77 | list_delete_next(struct list_item *list) | ||
| 78 | { | ||
| 79 | // TODO: Debuggen und nochmal richtig drüber nachdenken! | ||
| 80 | if ( list->next != NULL ) { | ||
| 81 | struct list_item *temp = list->next; | ||
| 82 | |||
| 83 | list->next = list->next->next; | ||
| 84 | |||
| 85 | free(temp); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 59 | size_t | 89 | size_t |
| 60 | list_length(struct list_item *list) | 90 | list_length(struct list_item *list) |
| 61 | { | 91 | { |
