//Author: Guo R.H
//Date:06.12.23
//USTC
#include
#include
#define STACK_INI_SIZE 25
#define STACKINC 10
typedef struct
{
int *base;
int *top;
int stacksize;
}Stack;
void InitStack (Stack *s);
void DestroyStack (Stack *s);
void ClearStack (Stack *s);
int StackEmpty (Stack *s);
int StackLength (Stack *s);
void GetTop (Stack *s, int *e);
void Push(Stack *s, int e);
void Pop(Stack *s, int *e);
int main()
{
Stack *ps, st;
int i, temp;
ps = &st;
InitStack(ps);
for(i=0; i<50; i++)
Push(ps, i);
for(i=0; i<50; i++)
{
Pop(ps, &temp);
printf("%d ", temp);
}
return 0;
}
void InitStack (Stack *s)
{
s->base = (int *)malloc(STACK_INI_SIZE*sizeof(int));
if (!s->base)
exit(0);
s->top = s->base;
s->stacksize = STACK_INI_SIZE;
}
void DestroyStack (Stack *s)
{
free (s->base);
}
void ClearStack (Stack *s)
{
s->top = s->base;
}
int StackEmpty (Stack *s)
{
if (s->top==s->base)
return 0;
else
return 1;
}
int StackLength (Stack *s)
{
return s->stacksize;
}
void GetTop (Stack *s, int *e)
{
if(s->top==s->base)
return;
*e = *(s->top-1);
}
void Push(Stack *s, int e)
{
if(s->top - s->base >=s->stacksize)
{
s->base = (int *)realloc(s->base,(s->stacksize+STACKINC)*sizeof(int));
if(!s->base)
exit(0);
s->top = s->base + s->stacksize;
s->stacksize += STACKINC;
}
*s->top++ = e;
}
void Pop(Stack *s, int *e)
{
if(s->top == s->base)
return;
*e = * --s->top;
}
阅读(947) | 评论(0) | 转发(0) |