leetcode-剑指30-OK

// language: c
// 剑指30,已经通过,待发,目前博客园发文章的系统出问题了
// https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/

// 结点的结构体定义,每个节点记录值与当前最小值
typedef struct Node{
    int val;
    struct Node *next;
    int min;
} Node;


typedef struct {
    struct Node *top;
    int size;
} MinStack;


/** initialize your data structure here. */

MinStack* minStackCreate() {
    MinStack *A = (MinStack *)malloc(sizeof(MinStack));
    A->top = NULL;
    A->size = 0;
    return A;
}

void minStackPush(MinStack* obj, int x) {
    Node *newnode=(Node*)malloc(sizeof(Node));
    newnode->val = x;
    newnode->next = obj->top;
    if(obj->size == 0)
        newnode->min =x;
    else if(obj->top->min < x)
        newnode->min = obj->top->min;
    else
        newnode->min =x;
    obj->top = newnode;
    obj->size++;
}


void minStackPop(MinStack* obj) {
    if(obj == NULL)
        return;
    if(obj->top == NULL)
        return;
    Node *willbepoped = obj->top;
    obj->top = obj->top->next;
    free(willbepoped);
    obj->size--;
}


int minStackTop(MinStack* obj) {
    // if(obj == NULL)
    //     return 1000;
    // if(obj->top == NULL)
    //     return 1000;
    return obj->top->val;
}

int minStackMin(MinStack* obj) {
    // if(obj == NULL)
    //     return 1000;
    // if(obj->top == NULL)
    //     return 1000;
    return obj->top->min;
}

void minStackFree(MinStack* obj) {
    free(obj);
}

/**
 * Your MinStack struct will be instantiated and called as such:
 * MinStack* obj = minStackCreate();
 * minStackPush(obj, x);
 
 * minStackPop(obj);
 
 * int param_3 = minStackTop(obj);
 
 * int param_4 = minStackMin(obj);
 
 * minStackFree(obj);
*/
posted @ 2021-01-27 09:47  RougeBW  阅读(48)  评论(0编辑  收藏  举报