#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
/*定义堆栈*/
typedef char ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}sqStack;
/*初始化栈*/
void initStack(sqStack *s)
{
/*内存中开辟一段连续空间作为栈空间,首地址赋值给s->base*/
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!s->base) exit(0); /*分配空间失败*/
s->top = s->base; /*最开始,栈顶就是栈底*/
s->stacksize = STACK_INIT_SIZE; /*最大容量为STACK_INIT_SIZE */
}
/*入栈操作,将e压入栈中*/
void Push(sqStack *s, ElemType e){
if(s->top - s->base >= s->stacksize){
/*栈满,追加空间*/
s->base = (ElemType *)realloc(s->base, (s->stacksize +
STACKINCREMENT)*sizeof(ElemType));
if(!s->base) exit(0); /*存储分配失败
经典算法11:任意长度整数加法
最新推荐文章于 2020-07-07 20:59:05 发布