Runtime Error .push(-3),getMin ..Can anyone let me know whats wrong ?


  • 0
    S

    typedef struct DataUnitNode{

    int value;
    int curMin;
    

    } DUNode;

    typedef struct {

    int limit;
    int top;
    DUNode *pData;
    

    } MinStack;

    void minStackCreate(MinStack *stack, int maxSize) {

    stack->limit=maxSize;
    stack->top=-1;
    stack->pData=(DUNode*)malloc(maxSize*sizeof(DUNode));
    

    }

    void minStackPush(MinStack *stack, int element) {

    if(stack->top>=stack->limit)
    	return;
    
    stack->top++;
    //set val
    stack->pData[stack->top].value=stack->pData[stack->top].curMin=element;//0th lement min is same
    
    //set min
    
    if(stack->top>0 &&  stack->pData[stack->top-1].curMin<element)
    	stack->pData[stack->top].curMin=stack->pData[stack->top-1].curMin;
    

    }

    void minStackPop(MinStack *stack) {
    if(stack->top==-1){
    return;
    }
    stack->top--;

    return;
    

    }

    int minStackTop(MinStack *stack) {

    if(stack->top!=-1){
        int val=stack->pData[stack->top].value;
    	return val;
    	}
    else
    	return 0;
    

    }

    int minStackGetMin(MinStack *stack) {

    if(stack->top!=-1){
        int val=stack->pData[stack->top].curMin;
    	return val;//
    	}
    else
    	return 0;
    

    }

    void minStackDestroy(MinStack *stack) {

    stack->limit = 0;
    stack->top = -1;
    free(stack->pData);
    free(stack);
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.