- Min Stack
typedef struct Stack{
int data;
int min;
struct Stack *next;
} MinStack;
MinStack* minStackCreate() {
MinStack *ptr = malloc(sizeof(MinStack));
ptr->next = NULL;
ptr->min = INT_MAX;
return ptr;
}
void minStackPush(MinStack* obj, int val) {
MinStack *ptr = malloc(sizeof(MinStack));
ptr->data = val;
if(obj->next == NULL)ptr->min = ptr->data;
else ptr->min=fmin(obj->next->min,ptr->data);
ptr->next = obj->next;
obj->next = ptr;
}
void minStackPop(MinStack* obj) {
MinStack *ptr = malloc(sizeof(MinStack));
ptr = obj->next;
obj->next = ptr->next;
}
int minStackTop(MinStack* obj) {
return obj->next->data;
}
int minStackGetMin(MinStack* obj) {
return obj->next->min;
}
void minStackFree(MinStack* obj) {
MinStack* ptr = (MinStack*)malloc(sizeof(MinStack));
while(obj->next!=NULL)
{
ptr = obj->next;
obj->next=ptr->next;
free(ptr);
}
}
/**
* Your MinStack struct will be instantiated and called as such:
* MinStack* obj = minStackCreate();
* minStackPush(obj, val);
* minStackPop(obj);
* int param_3 = minStackTop(obj);
* int param_4 = minStackGetMin(obj);
* minStackFree(obj);
*/