typedef int eletype;
typedef struct node{
eletype *top;
eletype *base;
eletype stacksize;
}stack;
eletype pop(stack *s,eletype *val)
{
if(s->base == s->top)
{
printf("pop stack error!\n");
return -1;
}
return *val = *--s->top;
}
eletype push(stack *s,eletype *val)
{
if((s->base - s->top) == s->stacksize)
{
printf("Stack has full!\n");
return -1;
}
return *s->top++= *val;
}
eletype initstack(stack *s,eletype size)
{
s->base = (eletype *)malloc(sizeof(eletype)*size);
s->stacksize = size;
s->top = s->base;
return 0;
}
eletype freestack(stack *s)
{
if(s->base)
free(s->base);
}
eletype empytstack(stack *s)
{
if(s->base != s->top)
{
printf("stack is right\n");
}else{
printf("stack is empty!\n");
return -1;
}
}
int main(void)
{
eletype table[6]={1,2,3,4,5,6};
eletype i;
stack stk;
initstack(&stk,6);
printf("push stack->");
for(i = 0;i < 6;i++)
{
push(&stk,&table[i]);
printf("%d,",table[i]);
}
printf("\n");
empytstack(&stk);
i = 0;
for(i = 0;i < 6;i++)
{
pop(&stk,&table[i]);
printf("%d,",table[i]);
}
printf("\n");
freestack(&stk);
printf("Stack free!\n");
return 0;
}
阅读(1307) | 评论(0) | 转发(0) |