diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-08-06 09:04:46 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-08-06 09:04:46 +0200 |
| commit | 5c2c4fa6d5f72c127d99a5a2952b3b3e39cadc40 (patch) | |
| tree | abb644007d8062c9d3eeefe3a496850157fc83a6 | |
| parent | 06fc363c69a880f1965a9d263d23d9af200f5a9d (diff) | |
| download | data-structures-5c2c4fa6d5f72c127d99a5a2952b3b3e39cadc40.tar.gz data-structures-5c2c4fa6d5f72c127d99a5a2952b3b3e39cadc40.tar.bz2 data-structures-5c2c4fa6d5f72c127d99a5a2952b3b3e39cadc40.zip | |
Fix: Der NEXT-Zeiger in einem neuem Item einer QUEUE muss mit NULL initialisiert werden
| -rw-r--r-- | queue.c | 14 |
1 files changed, 6 insertions, 8 deletions
| @@ -1,7 +1,7 @@ | |||
| 1 | /* Standard C */ | 1 | /* Standard C */ |
| 2 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | 3 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 4 | #include <stdbool.h> | ||
| 5 | 5 | ||
| 6 | /* Project */ | 6 | /* Project */ |
| 7 | #include "util.h" | 7 | #include "util.h" |
| @@ -10,7 +10,7 @@ typedef int T; | |||
| 10 | 10 | ||
| 11 | struct queue_item { | 11 | struct queue_item { |
| 12 | struct queue_item *next; | 12 | struct queue_item *next; |
| 13 | T data; | 13 | T data; |
| 14 | }; | 14 | }; |
| 15 | 15 | ||
| 16 | struct queue { | 16 | struct queue { |
| @@ -30,8 +30,9 @@ queue_put(struct queue *queue, T data) | |||
| 30 | 30 | ||
| 31 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { | 31 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { |
| 32 | struct queue_item *tmp = queue->tail; | 32 | struct queue_item *tmp = queue->tail; |
| 33 | new_item->data = data; | 33 | new_item->data = data; |
| 34 | queue->tail = new_item; | 34 | new_item->next = NULL; |
| 35 | queue->tail = new_item; | ||
| 35 | if ( queue->head == NULL ) | 36 | if ( queue->head == NULL ) |
| 36 | queue->head = queue->tail; | 37 | queue->head = queue->tail; |
| 37 | else | 38 | else |
| @@ -46,7 +47,7 @@ queue_get(struct queue *queue, T *data) | |||
| 46 | { | 47 | { |
| 47 | if ( queue->head != NULL ) { | 48 | if ( queue->head != NULL ) { |
| 48 | struct queue_item *next = queue->head->next; | 49 | struct queue_item *next = queue->head->next; |
| 49 | *data = queue->head->data; | 50 | *data = queue->head->data; |
| 50 | free(queue->head); | 51 | free(queue->head); |
| 51 | queue->head = next; | 52 | queue->head = next; |
| 52 | 53 | ||
| @@ -81,6 +82,3 @@ main() | |||
| 81 | } | 82 | } |
| 82 | return EXIT_SUCCESS; | 83 | return EXIT_SUCCESS; |
| 83 | } | 84 | } |
| 84 | |||
| 85 | |||
| 86 | |||
