#include"conio.h" #include "stdio.h" #include "stdlib.h" #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char SElemType; typedef int Status; #define OK 1 #define OVERFLOW 0 #define ERROR 0 typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack(SqStack &s){//构造一个空栈 s.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!s.base)exit(OVERFLOW); s.top=s.base; s.stacksize=STACK_INIT_SIZE; return OK; }//InitStack SElemType Getop(SqStack s){//若栈不空,则返回用e返回s栈元素 SElemType e; if(s.top==s.base)return ERROR; e=*(s.top-1); return e; }//Getop Status Push(SqStack &s,SElemType e){//插入元素e为新的栈顶元素 if(s.top-s.base>=s.stacksize){//栈满,追加存储空间 s.base=(SElemType *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SEle mType)); if(!s.base)exit(OVERFLOW);//存储分配失败 s.top=s
四则运算实现
最新推荐文章于 2022-07-07 20:47:50 发布