#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
#define MAX_STATCK_SIZE 100
#define STATCK_INCREMENT 10
typedef int elemtype;
typedef struct sqstack
{
int *base;
int *top;
int stacksize;
}sqstack_s;
int sqstack_init(sqstack_s *sq)
{
((sq)->base) = (int*)malloc(sizeof(elemtype)*12);
if(!((sq)->base)) return ERROR;
(sq)->top = (sq)->base;
(sq)->stacksize = MAX_STATCK_SIZE;
}
//入栈操作
int push_stack(sqstack_s *sq, int n)
{
if(sq->top-sq->base>=sq->stacksize)
{
sq->base = (int *)realloc(sq->base,(sq->stacksize+STATCK_INCREMENT)*sizeof(elemtype));
}
if(!sq) return ERROR;
sq->stacksize += STATCK_INCREMENT;
*(sq->top) = n;
++sq->top;
}
//出栈操作
int pop_stack(sqstack_s *sq,int *n)
{
sqstack_s *p;
p = sq;
if(p->base ==p->top) return ERROR;
--p->top;
*n = *(p->top);
}
//清空一个栈
int clear_stack(sqstack_s *sq)
{
if(sq->base ==sq->top) return OK;
else sq->top = sq->base ;
}
//销毁一个栈
int destory_stack(sqstack_s *sq)
{
free(sq->base);
sq->base = sq->top = NULL;
sq->stacksize = 0;
}
//计算一个栈的长度
int stack_length(sqstack_s *sq,int *length)
{
*length = (sq->top) - (sq->base);
printf("%d\n",*length);
}
int main()
{
int a[10];
int len;
int i;
sqstack_s sqs;
printf("hello world\n");
sqstack_init(&sqs);
for(i=0;i<10;i++)
{
push_stack(&sqs,i);
}
//计算一个栈
stack_length(&sqs,&len);
for(i=0;i<10;i++)
{
pop_stack(&sqs,a+i);
}
printf("the statck length is %d\n",len);
}
#include <stdlib.h>
#define ERROR 0
#define OK 1
#define MAX_STATCK_SIZE 100
#define STATCK_INCREMENT 10
typedef int elemtype;
typedef struct sqstack
{
int *base;
int *top;
int stacksize;
}sqstack_s;
int sqstack_init(sqstack_s *sq)
{
((sq)->base) = (int*)malloc(sizeof(elemtype)*12);
if(!((sq)->base)) return ERROR;
(sq)->top = (sq)->base;
(sq)->stacksize = MAX_STATCK_SIZE;
}
//入栈操作
int push_stack(sqstack_s *sq, int n)
{
if(sq->top-sq->base>=sq->stacksize)
{
sq->base = (int *)realloc(sq->base,(sq->stacksize+STATCK_INCREMENT)*sizeof(elemtype));
}
if(!sq) return ERROR;
sq->stacksize += STATCK_INCREMENT;
*(sq->top) = n;
++sq->top;
}
//出栈操作
int pop_stack(sqstack_s *sq,int *n)
{
sqstack_s *p;
p = sq;
if(p->base ==p->top) return ERROR;
--p->top;
*n = *(p->top);
}
//清空一个栈
int clear_stack(sqstack_s *sq)
{
if(sq->base ==sq->top) return OK;
else sq->top = sq->base ;
}
//销毁一个栈
int destory_stack(sqstack_s *sq)
{
free(sq->base);
sq->base = sq->top = NULL;
sq->stacksize = 0;
}
//计算一个栈的长度
int stack_length(sqstack_s *sq,int *length)
{
*length = (sq->top) - (sq->base);
printf("%d\n",*length);
}
int main()
{
int a[10];
int len;
int i;
sqstack_s sqs;
printf("hello world\n");
sqstack_init(&sqs);
for(i=0;i<10;i++)
{
push_stack(&sqs,i);
}
//计算一个栈
stack_length(&sqs,&len);
for(i=0;i<10;i++)
{
pop_stack(&sqs,a+i);
}
printf("the statck length is %d\n",len);
}