aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dlist.c8
-rw-r--r--queue.c5
-rw-r--r--stack.c14
-rw-r--r--stack2.c23
4 files changed, 30 insertions, 20 deletions
diff --git a/dlist.c b/dlist.c
index a2a5cfd..32258e8 100644
--- a/dlist.c
+++ b/dlist.c
@@ -112,7 +112,9 @@ dlist_pop_front(struct dlist *dlist, T *data)
112 else 112 else
113 element->next->prev = NULL; 113 element->next->prev = NULL;
114 114
115 *data = element->data; 115 if ( data != NULL ) {
116 *data = element->data;
117 }
116 free(element); 118 free(element);
117 119
118 return true; 120 return true;
@@ -134,7 +136,9 @@ dlist_pop_back(struct dlist *dlist, T *data)
134 else 136 else
135 element->prev->next = NULL; 137 element->prev->next = NULL;
136 138
137 *data = element->data; 139 if ( data != NULL ) {
140 *data = element->data;
141 }
138 free(element); 142 free(element);
139 143
140 return true; 144 return true;
diff --git a/queue.c b/queue.c
index e26acbb..2fb9c72 100644
--- a/queue.c
+++ b/queue.c
@@ -47,7 +47,10 @@ queue_get(struct queue *queue, T *data)
47{ 47{
48 if ( queue->head != NULL ) { 48 if ( queue->head != NULL ) {
49 struct queue_item *next = queue->head->next; 49 struct queue_item *next = queue->head->next;
50 *data = queue->head->data; 50
51 if ( data != NULL ) {
52 *data = queue->head->data;
53 }
51 free(queue->head); 54 free(queue->head);
52 queue->head = next; 55 queue->head = next;
53 56
diff --git a/stack.c b/stack.c
index fa89974..775269d 100644
--- a/stack.c
+++ b/stack.c
@@ -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
11struct stack_item { 11struct stack_item {
12 struct stack_item *next; 12 struct stack_item *next;
13 T data; 13 T data;
14}; 14};
15 15
16struct stack { 16struct stack {
@@ -31,7 +31,7 @@ stack_push(struct stack *stack, T data)
31 if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { 31 if ( (new_item = malloc(sizeof(*new_item))) != NULL ) {
32 new_item->data = data; 32 new_item->data = data;
33 new_item->next = stack->head; 33 new_item->next = stack->head;
34 stack->head = new_item; 34 stack->head = new_item;
35 } 35 }
36 else 36 else
37 ERROR("out of memory"); 37 ERROR("out of memory");
@@ -42,7 +42,10 @@ stack_pop(struct stack *stack, T *data)
42{ 42{
43 if ( stack->head != NULL ) { 43 if ( stack->head != NULL ) {
44 struct stack_item *next = stack->head->next; 44 struct stack_item *next = stack->head->next;
45 *data = stack->head->data; 45
46 if ( data != NULL ) {
47 *data = stack->head->data;
48 }
46 free(stack->head); 49 free(stack->head);
47 stack->head = next; 50 stack->head = next;
48 51
@@ -93,6 +96,3 @@ main()
93 96
94 return EXIT_SUCCESS; 97 return EXIT_SUCCESS;
95} 98}
96
97
98
diff --git a/stack2.c b/stack2.c
index 57dce4f..4c77ac2 100644
--- a/stack2.c
+++ b/stack2.c
@@ -1,13 +1,13 @@
1#include <stdbool.h>
1#include <stdio.h> 2#include <stdio.h>
2#include <stdlib.h> 3#include <stdlib.h>
3#include <stdbool.h>
4 4
5#include "util.h" 5#include "util.h"
6 6
7typedef int T; 7typedef int T;
8 8
9struct stack { 9struct stack {
10 T *array; 10 T * array;
11 size_t sz, p; 11 size_t sz, p;
12}; 12};
13 13
@@ -15,8 +15,8 @@ void
15stack_init(struct stack *stack) 15stack_init(struct stack *stack)
16{ 16{
17 stack->array = NULL; 17 stack->array = NULL;
18 stack->sz = 0; 18 stack->sz = 0;
19 stack->p = 0; 19 stack->p = 0;
20} 20}
21 21
22bool 22bool
@@ -25,7 +25,7 @@ stack_push(struct stack *stack, T data)
25 if ( stack->array == NULL ) { 25 if ( stack->array == NULL ) {
26 if ( (stack->array = malloc(10 * sizeof(*stack->array))) != NULL ) { 26 if ( (stack->array = malloc(10 * sizeof(*stack->array))) != NULL ) {
27 stack->sz = 10; 27 stack->sz = 10;
28 stack->p = 0; 28 stack->p = 0;
29 } 29 }
30 else { 30 else {
31 ERROR("out of memory"); 31 ERROR("out of memory");
@@ -35,11 +35,11 @@ stack_push(struct stack *stack, T data)
35 35
36 if ( stack->p == stack->sz ) { 36 if ( stack->p == stack->sz ) {
37 size_t new_sz; 37 size_t new_sz;
38 T *new_array; 38 T * new_array;
39 39
40 if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof(*stack->array))) != NULL ) { 40 if ( (new_array = realloc(stack->array, (new_sz = (stack->sz * 3 / 2)) * sizeof(*stack->array))) != NULL ) {
41 stack->array = new_array; 41 stack->array = new_array;
42 stack->sz = new_sz; 42 stack->sz = new_sz;
43 } 43 }
44 else { 44 else {
45 ERROR("out of memory"); 45 ERROR("out of memory");
@@ -55,7 +55,11 @@ bool
55stack_pop(struct stack *stack, T *data) 55stack_pop(struct stack *stack, T *data)
56{ 56{
57 if ( stack->p != 0 ) { 57 if ( stack->p != 0 ) {
58 *data = stack->array[--stack->p]; 58 --stack->p;
59
60 if ( data != NULL ) {
61 *data = stack->array[stack->p];
62 }
59 return true; 63 return true;
60 } 64 }
61 else 65 else
@@ -97,4 +101,3 @@ main()
97 101
98 return EXIT_SUCCESS; 102 return EXIT_SUCCESS;
99} 103}
100