Stack.c
De la WikiLabs
Versiunea din 27 martie 2014 09:55, autor: Rhobincu (discuție | contribuții) (Pagină nouă: <syntaxhighlight lang="c"> #include "stack.h" #include <stdlib.h> struct stack * create_stack(unsigned int capacity){ struct stack * new_stack; new_stack = (struct stack*)mal...)
#include "stack.h"
#include <stdlib.h>
struct stack * create_stack(unsigned int capacity){
struct stack * new_stack;
new_stack = (struct stack*)malloc(sizeof(struct stack));
new_stack->data = (char*)malloc(capacity * sizeof(char));
new_stack->capacity = capacity;
new_stack->head = 0;
return new_stack;
}
void delete_stack(struct stack * old_stack){
free(old_stack->data);
free(old_stack);
}
int is_full(struct stack * my_stack){
return my_stack->head == my_stack->capacity;
}
int is_empty(struct stack * my_stack){
return !my_stack->head; // head == 0
}
void stack_push(struct stack * my_stack, char element){
if(is_full(my_stack)){
return;
}
my_stack->data[my_stack->head] = element;
my_stack->head++;
}
char stack_pop(struct stack * my_stack){
if(is_empty(my_stack)){
return 0;
}
return my_stack->data[--my_stack->head];
}