blob: 679f67ba7cc18dc0acad3e347596541668ab1b32 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#include <stdio.h>
#include <stdlib.h>
#include "min_stack.h"
static Node *new(int data, Node *next) {
Node *node = malloc(sizeof(Node));
node->next = next;
node->data = data;
return node;
}
Stack *initialize(void) {
Stack *self = malloc(sizeof(Stack));
self->head = NULL;
self->min = NULL;
self->size = 0;
return self;
}
int size(Stack *self) {
return self->size;
}
void push(Stack *self, int data) {
if (!self->min || (data < self->min->data))
self->min = new(data, self->min);
self->head = new(data, self->head);
self->size++;
}
void each(Node *head, Visitor block) {
Node *tmp = head;
while(tmp) {
(*block)(tmp);
tmp = tmp->next;
}
}
int min(Stack *self) {
if(self->min)
return self->min->data;
if (self->head) {
int min = self->head->data;
Node *tmp = self->head->next;
while(tmp) {
if (tmp->data < min)
min = tmp->data;
tmp = tmp->next;
}
return min;
}
return (int)NULL;
}
int pop(Stack *self) {
if (!self->head)
return (int)NULL;
Node *current = self->head;
int data = current->data;
if (data == self->min->data)
self->min = self->min->next;
self->head = current->next;
self->size--;
current->next = NULL;
free(current);
return data;
}
void print_node(Node *node) {
printf("[%d]", node->data);
}
void inspect(Stack *stack) {
printf("\t");
each(stack->head, &print_node);
printf("\n");
}
|