0

I'm trying to make sure if my initialization stack function gets all the values enter by user, but right now my codes print out different values from the original values I enter. I use %d. Also, I'm working on different functions to work with the stack as such as pop, push, goes to top of stack, etc. Will it do work in do while loop? However, here is the initialization stack function

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

in the 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;
}
Cam9191
  • 33
  • 8
  • Little bit too many things to change there: allocation (you allocate 10 stacks, empty), you scanf an integer into a struct, you print garbage (because you allocate a new stack and you return that) and anyway you ask to printf many integers but you pass one single stack* value. Call initstack just once with stack* pStack = initstack(10) and printf/scanf into pStack->darr... – Adriano Repetti Sep 10 '14 at 16:02
  • Your use of `scanf` and `printf` are not right. It'll be helpful to read a tutorial or two on how to use them. Here's a starter. http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output – R Sahu Sep 10 '14 at 16:11
  • In C, you should not cast `malloc` - [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – crashmstr Sep 10 '14 at 16:49

2 Answers2

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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Too many issues here. Your declaration of stack s; in main() seems to indicate you don't want to dynamically allocate the stack itself, but only its elements. If so, why do you have a stack *s; declaration in your initStack()? It seems like you are trying to use initStack() to do various things, which only adds to the ambiguity.

  • Change your declaration of stack s; in main() to stack *s;

  • In your initStack() allocate memory and return the value to the pointer in main(). Call it only once and use a different function to do push and pop operations. When you allocate memory for every call you are basically creating multiple stacks.

  • It seems like you only want ten elements in the stack. So you shouldn't allocate your s->darr for element size for every call. Do it once and pass in the size value:

    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;
    }
    
  • Your push() would look something like this:

    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 
    }
    
  • Your pop() would look something like this:

    void pop(stack *s) {
        if(s != NULL) {
            s->size--;
            s->top = s->size; 
        }
    }
    
  • Your top() would look something like this:

    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 
    }
    
shikamaru
  • 54
  • 4