RPN计算器:如何释放从堆栈中弹出的元素?

时间:2018-12-13 12:21:35

标签: c struct singly-linked-list

我将首先添加我的代码:

    typedef struct _stack stack;
    typedef struct _stack_element stack_element;

    struct _stack {
        stack_element* top;
    };

    struct _stack_element {
        stack_element* next;
        float value;
    };

    void stack_push(stack* astack, float value)
    {


        struct _stack_element *elem=calloc(1,sizeof(stack_element));

           elem->value=value;


          if(astack->top==NULL){
            astack->top=elem;
          }
          else{
            elem->next=astack->top;
            astack->top=elem;
          }

    }


    float stack_pop(stack* astack)
    {
    float Number;
      if(astack==NULL){
        Number=NAN;
        return Number;
      }
      else{

        Number=astack->top->value;
        astack->top=astack->top->next;
    }
      return Number;
    }

     void process(stack* astack, char* token)
    {
        /* HIER implementieren */
      //  printf("\n<Logik fehlt!>\n");

        if(is_number(token)==1){
          float number=atof(token);
          stack_push(astack, number);
        }

        if(is_add(token)==1){
          float Number1=stack_pop(astack);
          float Number2=stack_pop(astack);
          float result=Number1+Number2;
          stack_push(astack, result);

        }
        if(is_sub(token)==1){
          float Number1=stack_pop(astack);
          float Number2=stack_pop(astack);
          float result=Number2-Number1;
          stack_push(astack, result);

        }
        if(is_mult(token)==1){
          float Number1=stack_pop(astack);
          float Number2=stack_pop(astack);
          float result=Number1*Number2;
          stack_push(astack, result);

        }
        return;
    }

    void print_stack(stack *astack) {
        int counter = 0;
        printf("\n |xxxxx|xxxxxxxxxxxxxxxxxxx|xxxxxxxxxxxxxxxxxxx|xxxxxxxxx|\n");
        printf(" | Nr. | Adresse           | Next              | Wert    |\n");
        printf(" |-----|-------------------|-------------------|---------|\n");
        for (stack_element* elem=astack->top; elem != NULL; elem = elem->next) {
            printf(" | %3d | %17p | %17p | %7.3f |\n", counter, elem, elem->next, elem->value);
            counter++;
        }
        printf(" |xxxxx|xxxxxxxxxxxxxxxxxxx|xxxxxxxxxxxxxxxxxxx|xxxxxxxxx|\n");
    }

    stack* stack_erstellen() {
      struct _stack* Stack =(struct _stack*)calloc(1, sizeof(struct _stack));

      Stack->top=NULL;

    return Stack;

    }

    int main(int argc, char** args)
    {
        stack* astack = stack_erstellen();
        char zeile[MAX_STR];
        char* token;

        intro();
        while (taschenrechner_input(zeile) == 0) {
            // Erstes Token einlesen
            token = strtok(zeile, " ");

            while (token != NULL) {
                printf("Token: %s\n", token);
                // Stackoperationen durchführen    return;

                process(astack, token);
                // Nächstes Token einlesen
                token = strtok(NULL, " ");
                print_stack(astack);
            }

            printf("\nExtrahiere Resultat\n");
            float result = stack_pop(astack);
            print_stack(astack);

            if (astack->top != NULL) {
                while (astack->top != NULL) {
                    stack_pop(astack);   //Räume Stack auf
                }
                printf("\nDoes not Compute: Stack nicht leer!\n");
            } else if (result != result) {
                printf("\nDoes not Compute: Berechnung fehlgeschlagen!\n");
            } else {
                printf("\nDein Ergebnis:\t%7.3f\n\n", result);
            }
        }
        free(astack);
    }

我是编码新手。上面的代码来自大学的作业。我必须创建的功能是:

  • void stack_push(stack* astack, float value)
  • void stack_pop(stack* astack)
  • void process (stack* astack, char* token)
  • stack* stack_erstellen()(翻译为“ create_stack”)

还有一个与此文件一起编译的c文件,但是我不允许对其进行任何更改,因此我没有将其添加到这篇文章中。

计算器似乎可以正常工作,并且print_stack()函数还表明push和pop函数可以按预期工作。

我的问题是我不确定如何释放在push函数中分配的pop函数中的内存。如何释放堆栈中的元素?

am知道要描述这样的技术问题,因此,如果缺少任何内容,我很乐意添加更多信息。

1 个答案:

答案 0 :(得分:0)

  

我的问题是我不确定如何释放在push函数中分配的pop函数中的内存。如何释放堆栈中的元素?

next元素之前,您需要保存free()指针:

    float stack_pop(struct _stack *astack) {
        float Number;
        if (astack == NULL) {
            Number = NAN;
            // return Number; // no need for return here
        } else {
            Number = astack->top->value;
            struct _stack_element *tmp = astack->top->next; // save
            free(astack->top);                              // free
            astack->top = tmp;                              // restore
        }
        return Number;
    }

我喜欢随意格式化代码:)

另外,考虑使用double代替float