【问题标题】:Stacks in C --- pop() function [closed]C中的堆栈---pop()函数[关闭]
【发布时间】:2013-09-25 01:42:39
【问题描述】:

我需要创建一个 Stack,它有一个 pop 函数,可以将元素从堆栈中弹出。它需要用 C 语言编写并包含两个浮点变量(float1、float2)。我不知道该怎么做,有人可以帮忙。

【问题讨论】:

  • 这是作业吗...??
  • 这绝对是功课。您提供答案并没有帮助!

标签: c stack


【解决方案1】:

如果您需要帮助,我能提供的最好方法就是向您提问。为了编写弹出操作,我们需要先进行推送操作。我对吗?否则,你怎么会弹出一些东西?您需要将 2 个浮点数推入此结构,对吗?如果我告诉你写一个保存 2 个浮点数的 push 函数,你会怎么写这样的函数?

【讨论】:

    【解决方案2】:

    使用 std::stack。这是documentation

    【讨论】:

      【解决方案3】:

      如果你用 c 编写,有一种通用的编程方法。这是一个示例,如下所示: 头

      typedef void *ElementAddr;
      typedef void (*PfCbFree)(ElementAddr);
      
      typedef struct StackRecord
      {
              ElementAddr *array;
              int     elemsize;
              int     loglength;
              int     alloclength;
              PfCbFree    freefn;
      } *Stack;
      /* Create a new stack */
      Stack stack_create(int elemsize, PfCbFree freefn);
      
      /* Dispose the stack */
      void stack_dispose(Stack stk);
      
      /* Make the given stack empty */
      void stack_make_empty(Stack stk);
      
      /* Return true if the stack is empty */
      int stack_is_empty(Stack stk);
      
      /* Insert a new element onto stack */
      void stack_push(Stack stk, ElementAddr elemaddr);
      
      /* Delete the top element off the stack */
      void stack_pop(Stack stk);
      
      /* Fetch the top element from the stack */
      void stack_top(Stack stk, ElementAddr elemaddr);
      
      /* Fetch & Delete the top element from the stack */
      void stack_top_and_pop(Stack stk, ElementAddr elemaddr);
      

      .cpp

      #define MIN_STACK_SIZE (4)
      
      /* Create a new stack */
      Stack 
      stack_create(int elemsize, PfCbFree freefn)
      {
              Stack stk;
      
              stk = malloc(sizeof(struct StackRecord));
      
              if ( stk == NULL) {
                      fprintf(stderr, "Out of memory\n");
                      exit(1);
              }
      
              stk->array = malloc(elemsize * MIN_STACK_SIZE);
              if (stk->array == NULL) {
                      fprintf(stderr, "Out of memory\n");
                      exit(1);
              }
              stk->elemsize = elemsize;
              stk->loglength = 0; 
              stk->alloclength = MIN_STACK_SIZE;
      }
      
      /* Dispose the stack*/
      void 
      stack_dispose(Stack stk)
      {
              stack_make_empty(stk);
              free(stk->array);
              free(stk);
      }
      
      /* Make the given stack empty*/
      void 
      stack_make_empty(Stack stk)
      {
              if ( stk->freefn ) {
                      int i;
                      for ( i = 0; i < stk->loglength; ++i) {
                              stk->freefn((char *)stk->array + 
                                          i * stk->elemsize);
                      }
              }
              stk->loglength = 0;
      }
      
      /* Return true if the stack is empty*/
      int 
      stack_is_empty(Stack stk)
      {
              return stk->loglength == 0;
      }
      
      static void 
      stack_grow(Stack stk)
      {
              stk->alloclength *= 2;
              stk->array = realloc(stk->array, 
                                   stk->alloclength * stk->elemsize);
      }
      
      /* Insert a new element onto stack */
      void 
      stack_push(Stack stk, ElementAddr elemaddr)
      {
              ElementAddr target;
              if ( stk->loglength == stk->alloclength )
                      stack_grow(stk);
              target = (char *)stk->array + stk->loglength * stk->elemsize;
              memcpy(target, elemaddr, stk->elemsize);
              stk->loglength++;   
      }
      
      /* Delete the top element off the stack */
      void 
      stack_pop(Stack stk)
      {
              ElementAddr target;
              if ( stack_is_empty(stk) ) {
                      fprintf(stderr, "Empty stack\n");
                      exit(1);
              }
              if ( stk->freefn ) {
                      target = (char *)stk->array + 
                               (stk->loglength-1) * stk->elemsize;
              stk->freefn(target);
              }
              stk->loglength--;
      }
      
      /* Fetch the top element from the stack */
      void 
      stack_top(Stack stk, ElementAddr elemaddr)
      {
              void *target = (char *)stk->array + 
                             (stk->loglength-1) * stk->elemsize;
              memcpy(elemaddr, target, stk->elemsize);
      }
      
      /* Fetch & Delete the top element from the stack */
      void 
      stack_top_and_pop(Stack stk, ElementAddr elemaddr)
      {
              ElementAddr target;
              if ( stack_is_empty(stk) ) {
                      fprintf(stderr, "Empty stack\n");
                      exit(1);
              }
              target = (char *)stk->array + 
                       (stk->loglength-1) * stk->elemsize;
              memcpy(elemaddr, target, stk->elemsize);
              stk->loglength--;
      }
      

      您可以将它用于任何数据类型。这是一个测试代码

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include "generic-stack.h"
      
      void strfreefn(ElementAddr elemaddr)
      {
              free(*(char **)elemaddr);
      }
      
      int 
      main(int argc, char **argv)
      {
              Stack int_stk, str_stk;
              int i;
              char *names[] = { 
                      "C", "C++", "Jave", "C#", "Python", 
                      "PHP", "Basic", "Objective C", "Matlab", "Golang" 
              };
      
              /* test integer stack */
              printf("Test integer stack\n");
              int_stk = stack_create(sizeof(int), NULL);
              for ( i = 0; i < 10; ++i ) {
                      stack_push(int_stk, &i);
              }
      
              while ( !stack_is_empty(int_stk) ) {
                      int val;
                      stack_top_and_pop(int_stk, &val);
                      printf("%d\n", val);
              }
      
              stack_dispose(int_stk);
      
              /* test string stack */
              printf("Test string stack\n");
              str_stk = stack_create(sizeof(char *), strfreefn);
              for ( i = 0; i < 10; ++i ) {
                      char *copy = strdup(names[i]);
                      char *dest;
                      stack_push(str_stk, ©);
              }
      
              while ( !stack_is_empty(str_stk) ) {
                      char *dest;
                      stack_top_and_pop(str_stk, &dest);
                      printf("%s\n", dest);
                      free(dest);
              }
              stack_dispose(str_stk);
      
              return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-24
        • 2015-04-07
        • 2021-03-18
        • 2020-02-14
        • 2015-10-08
        • 2013-04-11
        • 1970-01-01
        • 2023-04-01
        • 2016-01-31
        相关资源
        最近更新 更多