5

我有一个实现堆栈的 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”,它是计算数组最小元素的命令,程序会永远等待输入。在搜索了很长一段时间后,我仍然不知道为什么会发生这种情况。

4

2 回答 2

4

scanf 不要等待,但你有无限循环问题。在 functionminimum()中,您只能有条件地更新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

minis a_stack->head->dataa_nodeis a_stack->headsomin == a_node->date并且min > a_node->data总是评估false,因为您a_nodeif正文中进行了更新。

此外,我发现您的函数中有内存泄漏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;
}
于 2013-08-18T16:44:42.627 回答
0

此外,在:

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

我认为它应该返回 void 而不是 int 。

并且 handle_input() 中的 min_cmd 未使用。

于 2013-08-18T16:54:37.637 回答