aboutsummaryrefslogtreecommitdiff
path: root/stack.c
diff options
context:
space:
mode:
Diffstat (limited to 'stack.c')
-rw-r--r--stack.c14
1 files changed, 7 insertions, 7 deletions
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