Last executed input : empty. I get this runtime error when the system execute my C code.


  • 0
    D
    This is my C code. please give me some hints. thx.
    
    #include <stdio.h>
    
    typedef struct StackNode{
        int value;
        struct StackNode *next;
    } StackNode;
    
    typedef struct {
        int maxSize;
        int length;
        StackNode* front;
        StackNode* rear;
    } Stack;
    
    
    /* Create a stack */
    void stackCreate(Stack *stack, int maxSize) {
        stack = (Stack*)malloc(sizeof(Stack));
        stack->maxSize = maxSize;
        stack->length = 0;
        stack->front = stack->rear = NULL;
        puts("Create success.\n");
    }
    
    /* Push element x onto stack */
    void stackPush(Stack *stack, int element) {
        if(stack->length < stack->maxSize){
            if(stack->length == 0){
                StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));
                newNode->value = element;
                newNode->next = NULL;
                stack->front = stack->rear = newNode;
                stack->length++;
            }else{
                StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));
                newNode->value = element;
                newNode->next = NULL;
                stack->rear->next = newNode;
                stack->rear = stack->rear->next;
                stack->length++;
            }
        }else{
            puts("The Stack is full.\n");
        }
        puts("Push success.\n");
    }
    
    /* Removes the element on top of the stack */
    void stackPop(Stack *stack) {
        if(stack->length == 0) puts("The Stack is empty.\n");
        else if(stack->length == 1){
            StackNode* n = stack->front;
            stack->front = stack->rear = NULL;
            stack->length--;
            free(n);
        }else{
            StackNode* n = stack->front;
            stack->front = stack->front->next;
            stack->length--;
            free(n);
        }
        puts("Pop success.\n");
    }
    
    /* Get the top element */
    int stackTop(Stack *stack) {
        if(stack->length == 0){ puts("Get success.\n"); return -1;}
        else{puts("Get success.\n"); return stack->front->value;}
    }
    
    /* Return whether the stack is empty */
    bool stackEmpty(Stack *stack) {
        if(stack->length == 0){ puts("Check success.\n"); return true;}
        else{ puts("Check success.\n"); return false;}
    }
    
    /* Destroy the stack */
    void stackDestroy(Stack *stack) {
        while(stack->length != 0 && stack->length != 1){
            StackNode* n = stack->front;
            stack->front = stack->front->next;
            stack->length--;
            free(n);
        }
        free(stack->front);
        free(stack);
        puts("Destory success.\n"); 
        return true;
    }

  • 0
    S

    i got the same result.

    have you fix it ?


  • 0
    E

    I got the same problem


  • 1
    E

    You shouldn't free(stack) in function stackDestroy(), because stack should be used in other test cases.
    delete free(stack) can fix the problem.


Log in to reply
 

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