typedef int stackData;
typedef struct {
stackData *base ;
stackData *top ;
int stacksize;
}SqStack;
int InitStack(SqStack *S );
int DestroyStack(SqStack *S );
int ClearStack(SqStack *S );
int StackEmpty(SqStack *S );
int StackFull(SqStack *S );
int StackLength(SqStack *S );
int GetTop(SqStack *S , stackData *e );
int Pop(SqStack *S , stackData *e );
int Push(SqStack *S , stackData *e );
int PrintStack(SqStack *S );
int InitStack(SqStack *S )
{
S->base = (stackData *) malloc(STACK_INIT_SIZE * sizeof(stackData));
if (!S->base){
printf ("InitStack malloc fial" );
return -1 ;
}
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return 0 ;
}
int DestroyStack(SqStack *S )
{
free(S->base);
S->base = NULL;
S->top = NULL;
S->stacksize = 0 ;
return 0 ;
}
int ClearStack(SqStack *S )
{
while (S->base != S->top){
*( S->top --) = 0 ;
}
}
int StackEmpty(SqStack *S )
{
if (S->base == S->top)
return 1 ;
else
return 0 ;
}
int StackFull(SqStack *S )
{
if (S->stacksize <= (S->top - S->base))
return 1 ;
else
return 0 ;
}
int StackLength(SqStack *S )
{
return S->top - S->base;
}
int GetTop(SqStack *S , stackData *e )
{
if (S->top == S->base)
{
printf ("GetTop stack is empty\n" );
return -1 ;
}
*e = *( S->top-1 );
return 0 ;
}
int Pop(SqStack *S , stackData *e )
{
if (S->top == S->base)
{
printf ("Pop stack is empty\n" );
return -1 ;
}
*e = *( --(S->top));
return 0 ;
}
int Push(SqStack *S , stackData *e )
{
if ((S->top - S->base) >= S->stacksize)
{
S->base = (stackData *) realloc(S->base , (S->stacksize + STACK_ADD_SIZE) * sizeof(stackData));
if (!S->base)
{
printf ("Push realloc fial " );
return -1 ;
}
S->top = S->base + S->stacksize;
S->stacksize = S->stacksize + STACK_ADD_SIZE;
}
*( S->top) = *e ;
(S->top)++;
return 0 ;
}
int PrintStack(SqStack *S )
{
if (StackEmpty(S))
{
printf ("PrintStack StackEmpty\n " );
return 0 ;
}
int x = S->top - S->base;
int y = 1 ;
printf ("PrintStack:" );
for (; x >= y ; y ++){
printf ("% d " , *( S->top - y ));
}
printf ("\n" );
return 0 ;
}
int main(int argc , char *argv [])
{
SqStack stack;
int n = 12 ;
//stack Data e,m ;
InitStack(&stack);
//scanf ("%d " , n);
printf ("-------1------\n" );
Push(&stack , &n);
printf ("-------2------\n" );
n =13 ;
Push(&stack , &n);
n =13 ;
Push(&stack , &n);
n =13 ;
Push(&stack , &n);
PrintStack(&stack);
printf ("stacklenght:%d \n" ,StackLength(&stack));
int m = 0 ;
Pop(&stack , &m );
printf ("Pop :%d \n" ,m );
printf ("stacklenght:%d \n" ,StackLength(&stack));
while (n)
{
m = n%8 ;
printf ("-------2------\n" );
Push(&stack , &m );
n = n / 8 ;
}
printf ("-------%d ----%d --\n" ,StackEmpty(&stack),StackFull(&stack));
PrintStack(&stack);
/*while (!StackEmpty(stack))
{
Pop(stack ,&e);
printf("%d",e);
}*/
}