2014-09-10 42 views
0

我试图确定我的初始化堆栈函数是否获取用户输入的所有值,但是现在我的代码会从我输入的原始值中打印出不同的值。我用了。另外,我正在处理不同的函数来处理堆栈,比如pop,push,到堆栈顶端等等。它会在while循环中工作吗?然而,这里是初始化堆栈功能用多达10个值初始化堆栈

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

点点的东西太多改变有:分配(你分配10层,为空),你scanf函数整数成一个结构,您打印垃圾(因为你分配一个新的堆栈和你返回)和无论如何,你要求printf许多整数,但你通过一个单一的堆栈*值。用stack * pStack = initstack(10)和printf/scanf调用initstack到pStack-> darr ... – 2014-09-10 16:02:31

+0

您对'scanf'和'printf'的使用是不正确的。阅读一两个关于如何使用它们的教程会很有帮助。这是一个初学者。 http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output – 2014-09-10 16:11:22

+0

在C语言中,不应该使用'malloc' - [我是否使用malloc?](http: //stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – crashmstr 2014-09-10 16:49:42

回答

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

太多的问题。您在main()中的stack s;的声明似乎表明您不希望动态分配堆栈本身,而只是其中的元素。如果是这样,为什么你的initStack()中有一个stack *s;声明?似乎你正试图用initStack()做各种事情,这只会增加含糊性。

  • main()更改您的stack s;声明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 
    }