-
Notifications
You must be signed in to change notification settings - Fork 119
/
155.c
113 lines (98 loc) · 2.54 KB
/
155.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
typedef struct StackNode {
int val;
struct StackNode *next;
} StackNode;
typedef struct MinStack {
int size;
StackNode *top;
StackNode *min;
} MinStack;
void minStackCreate(MinStack *stack, int maxSize) {
/* I implement a linked stack, so I don't need maxSize */
if (stack) {
stack->min = NULL;
stack->top = NULL;
stack->size = 0;
}
}
void minStackPush(MinStack *stack, int element) {
if (stack) {
StackNode *new_node = (StackNode *)calloc(1, sizeof(StackNode));
new_node->val = element;
new_node->next = stack->top;
stack->top = new_node;
stack->size++;
/* update min pointer */
StackNode *new_min = (StackNode *)calloc(1, sizeof(StackNode));
new_min->val = element;
new_min->next = stack->min;
if (stack->min) {
if (stack->min->val >= element){
stack->min = new_min;
}
}
else {
stack->min = new_min;
}
}
}
void minStackPop(MinStack *stack) {
if (stack && stack->top) {
StackNode *t = stack->top;
stack->top = stack->top->next;
if (stack->min && t <= stack->min) {
StackNode *m = stack->min;
stack->min = stack->min->next;
free(m);
}
stack->size--;
free(t);
}
}
int minStackTop(MinStack *stack) {
if (stack && stack->top) {
return stack->top->val;
}
return 0;
}
int minStackGetMin(MinStack *stack) {
if (stack && stack->min) {
return stack->min->val;
}
return 0;
}
void minStackDestroy(MinStack *stack) {
if (stack) {
while (stack->size != 0) {
minStackPop(stack);
}
while (stack->min != NULL) {
StackNode *m = stack->min;
stack->min = stack->min->next;
free(m);
}
/**
*free(stack);
*stack = NULL;
*/
}
}
int main() {
MinStack stack;
minStackCreate(&stack, 10);
int i;
for (i = 0; i < 5; i++) {
minStackPush(&stack, i);
}
printf("top: %d\n", minStackTop(&stack));
printf("min: %d\n", minStackGetMin(&stack));
for (i = 1; i < 5; i++) {
minStackPush(&stack, -i);
}
printf("top: %d\n", minStackTop(&stack));
printf("min: %d\n", minStackGetMin(&stack));
minStackDestroy(&stack);
return 0;
}