-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_stack.c
183 lines (135 loc) · 2.97 KB
/
min_stack.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include "lib/helpers.h"
#include "lib/list_node.h"
struct ValInfo
{
int val;
struct ListNode hook;
};
void ValInfoDump(struct ValInfo *val_info)
{
printf(" %d", val_info->val);
}
void ValInfoInit(struct ValInfo *val_info, int val)
{
val_info->val = val;
ListNodeInit(&val_info->hook, val_info);
}
struct PairInfo
{
int val;
int cn;
struct ListNode hook;
};
void PairInfoDump(struct PairInfo *pair)
{
printf(" %d(%d)", pair->val, pair->cn);
}
void PairInfoInit(struct PairInfo *pair, int val, int cn)
{
pair->val = val;
pair->cn = cn;
ListNodeInit(&pair->hook, pair);
}
struct MinStack
{
struct ListNode list;
struct ListNode list_min; // save pair
};
int MinStackGetMin(struct MinStack *min_stack)
{
struct ListNode *list_node = NULL;
struct PairInfo *pair = NULL;
if (ListIsEmpty(&min_stack->list_min))
return INT_MIN; // error
list_node = min_stack->list_min.next;
pair = list_node->container;
return pair->val;
}
int MinStackPop(struct MinStack *min_stack)
{
struct ListNode *list_node = ListPopFront(&min_stack->list);
struct ValInfo *val_info = NULL;
int val = 0;
struct PairInfo *pair = NULL;
if (NULL == list_node)
return INT_MIN; // error
val_info = list_node->container;
val = val_info->val;
free(val_info);
list_node = min_stack->list_min.next;
pair = list_node->container;
if (--pair->cn == 0)
{
ListPopFront(&min_stack->list_min);
free(pair);
}
return val;
}
void MinStackPush(struct MinStack *min_stack, int val)
{
struct PairInfo *pair = NULL;
struct ValInfo *val_info = calloc(1, sizeof(struct ValInfo));
ValInfoInit(val_info, val);
ListAddFront(&min_stack->list, &val_info->hook);
if (! ListIsEmpty(&min_stack->list_min))
{
struct ListNode *list_node = min_stack->list_min.next;
pair = list_node->container;
if (pair->val <= val)
{
++pair->cn;
return;
}
}
pair = calloc(1, sizeof(struct PairInfo));
PairInfoInit(pair, val, 1);
ListAddFront(&min_stack->list_min, &pair->hook);
}
void MinStackDump(struct MinStack *min_stack)
{
struct ListNode *head = &min_stack->list, *p = NULL;
printf("list ");
for (p = head->next; p != head; p = p->next)
{
struct ValInfo *val_info = p->container;
ValInfoDump(val_info);
}
printf("\n");
head = &min_stack->list_min;
printf("min %d/", MinStackGetMin(min_stack));
for (p = head->next; p != head; p = p->next)
{
struct PairInfo *pair = p->container;
PairInfoDump(pair);
}
printf("\n");
}
void MinStackInit(struct MinStack *min_stack)
{
ListInit(&min_stack->list);
ListInit(&min_stack->list_min);
}
void TestMinStack(int num)
{
struct MinStack min_stack;
int val = 0;
MinStackInit(&min_stack);
srand(time(NULL));
while (num > 0)
{
MinStackPush(&min_stack, rand() % 100);
MinStackDump(&min_stack);
--num;
}
while ((val = MinStackPop(&min_stack)) != INT_MIN)
MinStackDump(&min_stack);
}
int main(int argc, char *argv[])
{
TestMinStack(atoi(argv[1]));
return 0;
}