【问题标题】:Initializing the stack with up to 10 values使用最多 10 个值初始化堆栈
【发布时间】:2014-09-10 15:58:05
【问题描述】:

我正在尝试确保我的初始化堆栈函数是否获取用户输入的所有值,但现在我的代码打印出与我输入的原始值不同的值。我用了。另外,我正在研究与堆栈一起使用的不同功能,例如弹出、推送、进入堆栈顶部等。它会在 do while 循环中工作吗?不过这里是初始化栈函数

typedef struct stack
{
    int* darr;
    int size;
    int top;
}stack;

stack * initStack(int elements)
{
    stack *s;

    s = (stack *)malloc(sizeof(stack));
    s->darr = (int *)malloc(sizeof(int)*elements);
    s->size = 0;
    s->top = elements;

    return s;
}

在主()中

int main()
{
    stack s;
    int i;

    printf("Hello user, please enter 10 different values to build your stack: \n");

    for(i = 0; i < 10; i++)
    {
        scanf("%d", initStack(i));
    }

    printf("\nYou entered: \n%d\n\n", initStack(i));

    return 0;
}

【问题讨论】:

  • 有太多东西需要改变:分配(分配 10 个堆栈,为空),将整数扫描到结构中,打印垃圾(因为分配了新堆栈并返回)无论如何,您要求 printf 许多整数,但您传递了一个 stack* 值。使用 stack* pStack = initstack(10) 和 printf/scanf 调用一次 initstack 到 pStack->darr...
  • 您对scanfprintf 的使用不正确。阅读一两个关于如何使用它们的教程会很有帮助。这是一个首发。 codingunit.com/…
  • 在 C 中,你不应该转换 malloc - Do I cast the result of malloc?

标签: c initialization stack


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct stack {
    int* darr;
    int size;
    int top;
}stack;

stack *initStack(int elements){
    stack *s;

    s = (stack *)malloc(sizeof(stack));
    s->darr = (int *)malloc(sizeof(int)*elements);
    s->size = 0;
    s->top = elements;

    return s;
}

void dropStack(stack *s){
    free(s->darr);
    free(s);
}

void push(stack *s, int v){
    if(s->top == 0){
        fprintf(stderr, "stack full!\n");
        return ;
    }
    s->darr[--s->top] = v;
    ++s->size;
}

bool isEmpty(stack *s){
    return s->size == 0;
}

int pop(stack *s){//need check by isEmpty before pop
    --s->size;
    return s->darr[s->top++];
}

int main(void) {
    stack *s = initStack(10);
    int i;

    printf("Hello user, please enter 10 different values to build your stack: \n");

    for(i = 0; i < 10; i++){
        int value;
        scanf("%d", &value);
        push(s, value);
    }

    while(!isEmpty(s))
        printf("\nYou entered: \n%d\n", pop(s));

    dropStack(s);
    return 0;
}

【讨论】:

    【解决方案2】:

    这里的问题太多了。您在main() 中声明stack s; 似乎表明您不想动态分配堆栈本身,而只想动态分配它的元素。如果是这样,为什么您的initStack() 中有stack *s; 声明?似乎您正在尝试使用initStack() 来做各种事情,这只会增加歧义。

    • main() 中的stack s; 声明更改为stack *s;

    • 在您的initStack() 中分配内存并将值返回给main() 中的指针。只调用一次并使用不同的函数进行推送和弹出操作。当您为每个调用分配内存时,您基本上是在创建多个堆栈。

    • 您似乎只想要堆栈中的十个元素。所以你不应该为每次通话分配s-&gt;darrelement 大小。执行一次并传入大小值:

      stack* initSize(int numElements) {
          stack *s = NULL;
          if ( s == NULL ) {
              s = malloc(sizeof(stack));
              s->darr = malloc(sizeof(int) * numElements);
          }
          s->size = 0;
          s->top = 0;
          //s->limit = numElements; Add a new member like this to your structure to check during insertion, so that we don't overflow
          return s;
      }
      
    • 你的 push() 看起来像这样:

      int push(stack *s, int item) {
          if (s != NULL) {
              if (s->size < s->limit) {
                  s->darr[s->size] = item;
                  s->top = s->size;
                  s->size++;
                  return 0; //indicates success
              } else {
                  retrun -1; //indicates failure, stack is full
              }
          }
          return -1; //invalid stack pointer 
      }
      
    • 您的pop() 看起来像这样:

      void pop(stack *s) {
          if(s != NULL) {
              s->size--;
              s->top = s->size; 
          }
      }
      
    • 您的top() 看起来像这样:

      int top(stack *s) {
          if (s != NULL) {
              return s->darr[s->top];
          }
          return -1;//something to indicate that it is an invalid stack, if you are going to store -1 as an item, this return will be confusing, so pick a unique value 
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2016-04-18
      • 2017-09-24
      相关资源
      最近更新 更多