summaryrefslogtreecommitdiff
path: root/src/01
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-05 13:32:24 -0600
committermo khan <mo.khan@gmail.com>2020-07-05 13:32:24 -0600
commit55db5b3cbc9781ec499280627e42ab5be39ca186 (patch)
tree61a529f0f67b466269f33f02c8bf34a70b49a013 /src/01
parentc1eba569296eb591de1cae838e2d342f17ecb950 (diff)
Add parameter to initialize Node with nex
Diffstat (limited to 'src/01')
-rw-r--r--src/01/06/min_stack.c23
1 files changed, 7 insertions, 16 deletions
diff --git a/src/01/06/min_stack.c b/src/01/06/min_stack.c
index 867dce8..679f67b 100644
--- a/src/01/06/min_stack.c
+++ b/src/01/06/min_stack.c
@@ -2,9 +2,9 @@
#include <stdlib.h>
#include "min_stack.h"
-static Node *new(int data) {
+static Node *new(int data, Node *next) {
Node *node = malloc(sizeof(Node));
- node->next = NULL;
+ node->next = next;
node->data = data;
return node;
}
@@ -22,17 +22,10 @@ int size(Stack *self) {
}
void push(Stack *self, int data) {
- if (!self->min)
- self->min = new(data);
- else if (data < self->min->data) {
- Node *tmp = new(data);
- tmp->next = self->min;
- self->min = tmp;
- }
+ if (!self->min || (data < self->min->data))
+ self->min = new(data, self->min);
- Node *node = new(data);
- node->next = self->head;
- self->head = node;
+ self->head = new(data, self->head);
self->size++;
}
@@ -79,13 +72,11 @@ int pop(Stack *self) {
return data;
}
-void print_node(Node *node)
-{
+void print_node(Node *node) {
printf("[%d]", node->data);
}
-void inspect(Stack *stack)
-{
+void inspect(Stack *stack) {
printf("\t");
each(stack->head, &print_node);
printf("\n");