簡體   English   中英

C程序在scanf上永遠等待

[英]C program waits forever on scanf

我有一個實現堆棧的C程序。

#include <stdio.h>
#include <stdlib.h>


struct node{
    int data;
    struct node *link;
};

struct stack{
    struct node *head;
    struct node *data_node;
};

int push(struct stack *a_stack, int i){
    a_stack->data_node = malloc(sizeof(struct node));
    if(a_stack->data_node == NULL){
        puts("Error: Cannot allocate sufficient memory.");
        exit(1);
    }
    a_stack->data_node->data = i;
    a_stack->data_node->link = a_stack->head;
    a_stack->head= a_stack->data_node;
    return 0;
}

int pop(struct stack *a_stack){
    if(a_stack->head==NULL){
        return '\n';
    }
    int temp = a_stack->head->data;
    a_stack->data_node = a_stack->head;
    a_stack->head = a_stack->head->link;
    free(a_stack->data_node);
    return temp;
}

int minimum(struct stack *a_stack){
    if(a_stack->head==NULL){
        return '\n';
    }
    int min = a_stack->head->data;
    struct node *a_node = a_stack->head;
    while(a_node!=NULL){
        if(min>a_node->data){
            min = a_node->data;
            a_node = a_node->link;
        }
    }
    return min;
}

int init_stack(struct stack *a_stack){
    a_stack->head = NULL;
    a_stack->data_node = NULL;
}

int handle_input(struct stack *test){

    char* input_string = (char*)malloc(20);
    scanf("%s", input_string);
    // gets(input_string);

    char* pop_cmd = "-";
    char* min_cmd = "min";
    int num;

    if (strcmp(pop_cmd, input_string) == 0){
        printf("%d\n", pop(test));
    }

    else{
        if (input_string[0] == 'm'){
            printf("%d\n", minimum(test));
        }
        else{
            num = atoi(input_string);
            push(test, num);
        }   
    }

    return 0;
}


int main(void){

    int no_of_input, counter;

    struct stack test;
    init_stack(&test);

    scanf("%d", &no_of_input);

    for(counter=no_of_input; counter>0; counter=counter-1){
        handle_input(&test);
    };

    return 0;
}

問題是如果我想輸入'min'這是計算數組最小元素的命令,程序會在輸入時等待。 經過一段時間的搜索,我仍然不知道為什么會這樣。

scanf不等待,但你有無限循環問題。 在函數minimum() ,您只有條件地將a_node更新為鏈接列表中的下一個節點:

  int min = a_stack->head->data;  //note
  struct node *a_node = a_stack->head; //note

   while(a_node!=NULL){
        if(min > a_node->data){<-- "Always evaluates FALSE because: min is a_node->data"
            min = a_node->data;
            a_node = a_node->link; <--"Should NOT be here"
        }
        a_node = a_node->link; <--"but it should be here"
    }

此外, if條件(min > a_node->data)始終評估為false ,原因如下:

mina_stack->head->dataa_nodea_stack->head所以min == a_node->datemin > a_node->data總是計算false因為你在if更新了a_node

另外我發現函數handle_input() 有內存泄漏 你應該顯式地free()動態分配的內存。 閱讀我的建議如下:

int handle_input(struct stack *test){
    char* input_string = malloc(20); <-- "No need to type case"  
    // code here
    free(input_string);  <-- "Add this"
    return 0;
}

另外,在:

int init_stack(struct stack *a_stack){
    a_stack->head = NULL;
    a_stack->data_node = NULL;
}

它應該返回void而不是int我認為。

handle_input()中的min_cmd和未使用的min_cmd。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM