#include
#include "stack.h"
int stack_init(STACK * stack,int size)
{
stack->buffer=(int *)calloc(size,sizeof(int));
if (stack->buffer==NULL) return NULL;
stack->pointer=0;
stack->size=size;
stack->flag_full=0;
return 1;
}
void push(STACK *stack,int c)
{
stack->buffer[stack->pointer]=c;
if (stack->pointer==stack->size-1)
{
stack->pointer=0;
stack->flag_full=1;
}
else stack->pointer++;
}
int pop(STACK *stack)
{
if (stack->pointer==0)
{
if (stack->flag_full==0) return 0;
else stack->pointer=stack->size-1;
}
else stack->pointer--;
return stack->buffer[stack->pointer];
}
float middle_stack(STACK *stack)
{
register i,j;
long tmp=0;
if (stack->flag_full==0) j=stack->pointer;
else j=stack->size;
for (i=0;ibuffer[i];
return (tmp/(float)j);
}
void delete_stack(STACK *stack)
{
free (stack->buffer);
stack->buffer=0;
}
int stack_init_f(STACK_f * stack,int size)
{
stack->buffer=(float *)calloc(size,sizeof(float));
if (stack->buffer==NULL) return NULL;
stack->pointer=0;
stack->size=size;
stack->flag_full=0;
return 1;
}
void push_f(STACK_f *stack,float c)
{
stack->buffer[stack->pointer]=c;
if (stack->pointer==stack->size-1)
{
stack->pointer=0;
stack->flag_full=1;
}
else stack->pointer++;
}
float pop_f(STACK_f *stack)
{
if (stack->pointer==0)
{
if (stack->flag_full==0) return 0;
else stack->pointer=stack->size-1;
}
else stack->pointer--;
return stack->buffer[stack->pointer];
}
float middle_stack_f(STACK_f *stack)
{
register i,j;
float tmp=0;
if (stack->flag_full==0) j=stack->pointer;
else j=stack->size;
for (i=0;ibuffer[i];
return (tmp/(float)j);
}
void delete_stack_f(STACK_f *stack)
{
free (stack->buffer);
stack->buffer=0;
}
/*
main()
{
STACK s;
int i;
stack_init(&s,5);
push(&s,5);
push(&s,10);
push(&s,-230);
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
delete_stack(&s);
puts(" ");
stack_init(&s,5);
for (i=0;i<7;i++) push(&s,i);
printf("middle=%d",middle_stack(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
printf("\n%d", pop(&s));
}
*/