#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Exclamation Designing Stack Header file


    Dear Friends ,
    I am developing one header file for implementation of stack. My library is containing only two functions push and pop.I am confuse is it good for program to declare structure in header file ? I want your answer with detailed description of reasons.
    I urge you to give your best comments on my code.Any single problem you think can happen kindly share it here. :tntworth: :tntworth: :tntworth: :tntworth: :tntworth: :tntworth: :tntworth:

    Below code is of "stack.h". This file contains declaration of functions and variables used threw out the program.
    Code:
    #ifndef STACK_H_INCLUDED
    #define STACK_H_INCLUDED
    
    #include<stdio.h>
    #include<stdlib.h>
    
    struct Stack {
        int value;
        struct Stack *previous;
    };
    
    extern struct Stack *stack;
    extern void push(int value);
    extern int pop(void);
    
    
    #endif // STACK_H_INCLUDED
    Following is the code of "stack.c" this code contains the definitions of the functions.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include "stack.h" // my header file.
    
    struct Stack *stack=NULL;
    
    void push(int value)
    {
        struct Stack *temprary = malloc(sizeof(struct Stack));
    
        if(stack == NULL) {
            stack = malloc(sizeof(struct Stack));
            stack->previous = NULL;
        }
        else {
            temprary->previous = stack;
            stack = temprary;
        }
    
        stack->value = value;
    }
    int pop(void)
    {
        int value;
        struct Stack *temprary;
    
    
        if(stack == NULL) {
            printf("\nSorry, The stack is empty.\n");
            return 0;
        }
        else if(stack->previous == NULL) {
            value = stack->value;
            stack = NULL ;
            return value;
        }
        else {
            value = stack->value;
            temprary = stack;
            stack = stack->previous;
            free(temprary);
        }
        return value;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Code:
    #ifndef STACK_H_INCLUDED
    #define STACK_H_INCLUDED
    
    extern void push(int value);
    extern int pop(void);
    
    #endif // STACK_H_INCLUDED
    This is all you should need.

    The actual struct can be declared in stack.c.
    No user of your stack needs to know how you actually store the stack, you only provide a push/pop interface.

    Oh, and your "push" calls malloc twice the first time around. One of those allocations is a memory leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Smile


    Originally Posted by salem
    [code]
    The actual struct can be declared in stack.c.
    No user of your stack needs to know how you actually store the stack, you only provide a push/pop interface.
    Thank you for your comment.
    Reason behind doing this is to provide the facility to access the Top value of the stack. I mean if the user want to know the TOP most value of the stack he/she can access it by
    This way in their program.

    Code:
    printf("%d",stack->value);

    But , after reading your comment i think how if I will convert it to the the function which will return the value of top of stack ?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    > Reason behind doing this is to provide the facility to access the Top value of the stack.
    So provide a "peek" function which returns the top of the stack, but doesn't pop it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Exclamation


    Originally Posted by salem
    > Reason behind doing this is to provide the facility to access the Top value of the stack.
    So provide a "peek" function which returns the top of the stack, but doesn't pop it.
    Yes. By doing this , it will provide some abstraction to my program. There is one more question.

    In the "pop" function ,if you look at this condition
    Code:
     if(stack == NULL) {
            printf("\nSorry, The stack is empty.\n");
            return 0;
        }
    what should be the return value for this ? i mean if stack is empty that means it has no value.I am returning "0" here. But it is also possible that actual value in stack is "0". And instead of "0" if i return some negative value then it is also possible that stack may have negative value. In a nut shell what is the best value i should return if the stack is empty ?

IMN logo majestic logo threadwatch logo seochat tools logo