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.
Following is the code of "stack.c" this code contains the definitions of the functions.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
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; }