I have tried to implement a Stack and associated functions in C, in order to understand the data structure better. Just wanted to know what do you think about my code overall.
Furthermore, I am using 0
as a value to set "empty section of the stack", and I know this is ambiguous - any suggestion on how to improve that aspect or anything else in the code?
/*Data Structure: Stack*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack{
//Data_Strucure: Stack of intgers
int *stack;
int size_of_stack;
int elem_in_stack;
};
struct stack creat_stack(unsigned int);
int push(struct stack *, int);
int pop(struct stack *);
int empty(struct stack *);
int peek(struct stack *);
void print_the_stack(struct stack);
int main(int argc, char *argv[]){
int elem, ret;
struct stack new_stack = creat_stack(5);
for(int i = 0, elem = 5; i < new_stack.size_of_stack; i++)
if(push(&new_stack, elem--) == 1)
fprintf(stderr, "Unable to allocate more elements into the stack.");
print_the_stack(new_stack);
ret = pop(&new_stack);
printf("[POP] -> %d\n", ret);
elem = 10;
if(push(&new_stack, elem) == 1)
fprintf(stderr, "Unable to allocate '%d' into the stack. No more space avaible.\n", elem);
print_the_stack(new_stack);
if((ret = peek(&new_stack)) == 1)
fprintf(stderr, "Stack is empty");
printf("[PEEK] -> %d\n", ret);
printf("[EMPTYING]...\n");
empty(&new_stack);
print_the_stack(new_stack);
}
struct stack creat_stack(unsigned int size){
struct stack tmp;
if((tmp.stack = malloc(sizeof(int) * size)) == NULL){
fprintf(stderr, "Unable to allocate memory for the Stack.\n");
exit(1);
}
tmp.size_of_stack = size;
tmp.elem_in_stack = 0;
for(int i = 0; i < tmp.size_of_stack; i++)
tmp.stack[i] = 0;
return tmp;
}
int push(struct stack *stack, int nw_elem){
int pos = stack->size_of_stack - stack->elem_in_stack;
if(stack->elem_in_stack == stack->size_of_stack)
return 1;
stack->stack[pos - 1] = nw_elem;
stack->elem_in_stack++;
}
int pop(struct stack *stack){
int ret;
if(stack->elem_in_stack > 0){
int pos = stack->size_of_stack - stack->elem_in_stack;
ret = stack->stack[pos];
stack->elem_in_stack--;
stack->stack[pos] = 0;
return ret;
}
fprintf(stderr, "Stack is empty\n");
exit(1);
}
int empty(struct stack *stack){
for(int i = 0; i < stack->size_of_stack; i++)
stack->stack[i] = 0;
stack->size_of_stack = 0;
stack->elem_in_stack = 0;
}
int peek(struct stack *stack){
int ret;
if(stack->elem_in_stack > 0){
int pos = stack->size_of_stack - stack->elem_in_stack;
return stack->stack[pos];
}
else
return 1;
}
void print_the_stack(struct stack stack){
printf("[CURRENT STACK STATE] -> { ");
for(int i = 0; i < stack.size_of_stack; i++)
printf("%d ", stack.stack[i]);
printf("}\n");
}
Plus, let me know if the code runs on your machine. On other implementation try, that was the major problem.
Thanks in advance for any useful help
stack_contract_after_pop()
is a little bit unclear. Suppose you have 4 elements intostorage_array
; whenstack_pop()
is called, the number of elements is decremented and the function is called. Following the if: 3 * 4 = 12 -> 12 <= 4 fails then, no allocation will take place, where instead 1 block of memory should have been trimmed. Suppose the other case where you have a storage of 10 and 3 elements: 8<=10 TRUE -> 10 > 8 TRUE -> then realloc set storege to 2. Maybe is me, but, could you please explain how it works please? \$\endgroup\$