发一个stack头文件,自己写的,求意见
#include <assert.h>#include <stdlib.h>
#ifndef _STACK_DEFINED
typedef struct
{
int size;
int max;
int count;
unsigned char* data;
int top;
}stack;
stack init_stack(int size);
void cleanup_stack(stack* s);
void expand_stack(stack* s, int length);
void top_stack(stack s, void* d);
void push_stack(stack* s, void* d);
void pop_stack(stack* s, void* d);
int empty_stack(stack s);
stack init_stack(int size)
{
stack s;
s.size=size;
s.max=0;
s.count=0;
s.data=0;
s.top=-1;
return s;
}
void cleanup_stack(stack* s)
{
s->max=0;
s->count=0;
s->data=0;
s->top=-1;
}
void expand_stack(stack* s, int length)
{
void* v=realloc(s->data,(s->max+length)*s->size);
assert(v);
s->data=v;
s->max+=length;
}
void top_stack(stack s, void* d)
{
if(empty_stack(s))
exit(0);
memcpy(d,&(s.data[s.top*s.size]),s.size);
}
void push_stack(stack* s, void* d)
{
if(s->count==s->max)
expand_stack(s,10);
memcpy(&(s->data[(s->top+1)*s->size]),d,s->size);
s->count++;
s->top++;
}
void pop_stack(stack* s, void* d)
{
memcpy(d,&(s->data[s->top*s->size]),s->size);
s->data[s->top*s->size]=0;
s->top--;
s->count--;
}
int empty_stack(stack s)
{
return s.top==-1;
}
#define _STACK_DEFINED
#endif
stack.rar
(871 Bytes)