使用最多10个值初始化堆栈

时间:2014-09-10 15:58:05

标签: c initialization stack

我正在尝试确保我的初始化堆栈函数是否获取了用户输入的所有值,但是现在我的代码打印出与我输入的原始值不同的值。我用了。另外,我正在使用不同的函数来处理堆栈,例如pop,push,to stack of stack等。它会在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;
}

在main()

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;
}

2 个答案:

答案 0 :(得分:0)

#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;
}

答案 1 :(得分:0)

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

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

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

  • 看起来你只需要十个元素。因此,您不应为每次通话分配s->darr element大小的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; } 。做一次并传递大小值:

    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 } 看起来像这样:

    {{1}}