Stack is a linear table that is only inserted or deleted at the end of the table. For the stack, the end of the table has a special meaning, called the top of the stack. Correspondingly, the header end is called the bottom of the stack ). Empty Tables without elements are called empty stacks.
stack_s.c#include "stdio.h"#include "malloc.h"#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int Status;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef 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;}Status StackEmpty (SqStack S){ if (S.top==S.base) return TRUE; else return FALSE;}Status GetTop(SqStack S,SElemType *e){ if (S.top==S.base) return ERROR; *e=*(S.top-1); return OK;}Status Push(SqStack *S,SElemType e){ if ((S->top-S->base)>=S->stacksize){ S->base=(SElemType *) realloc (S->base,(S->stacksize +STACKINCREMENT)*sizeof(SElemType)); if (!S->base) exit (OVERFLOW); S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT;} *(S->top)=e; S->top++; return OK;}Status POP(SqStack *S,SElemType *e){ if (S->top==S->base) return ERROR; S->top--; *e=*(S->top); return OK;}Status Output (SElemType e){ printf("%d",e); return OK;} typedef int SElemType;#include <stype.h>#include "stack_s.c" Status in (char c,char op[]){ char *P; p=op; while(*p!='\0'){ if (c==*P) return TRUE; p++; } return FALSE;}char Precede(char a,char b){ int i,j; char pre[][7]={ {'>','>','<','<','<','>','>'}, {'>','>','<','<','<','>','>'}, {'>','>','>','>','<','>','>'}, {'>','>','>','>','<','>','>'}, {'<','<','<','<','<','=','0'}, {'>','>','>','>','0','>','>'}, {'<','<','<','<','<','0','='}; switch(a){ case '+':i=0;break; case '-':i=1;break; case '*':i=2;break; case '/':i=3;break; case '(':i=4;break; case ')':i=5;break; case '#':i=6;break; } switch(b){ case '+':j=0;break; case '-':j=1;break; case '*':j=2;break; case '/':j=3;break; case '(':j=4;break; case ')':j=5;break; case '#':j=6;break; } return Pre[i][j];}int Operate(int a,char theta, int b){ int i,j,result; i=a; j=b; switch(theta) { case '+':result =i+j;break; case '-':result =i-j;break; case '*':result =i*j;break; case '/':result =i/j;break; } return result;}int getNext(int *n){ char c; *n=0; while((c=getchar())==''); if (!=isdigit(c)){ *n=c; return 1; } do { *n=*n*10+(c-'0'); c=getchar(); }while (isdigit (c)); ungetc(c,stdin); return 0;}int EvaluateExpression(){ int n; int flag; int c; char x,theta; int a,b; char op[]="+ - * / ( ) #"; SqStack OPTR; SqStack OPND; InitStack (& OPTR);Push (&OPTR,'#'); InitStack(& OPND); flag=getNext(&c); GetTop(OPTR,&x); while (c!='#' || x!='#'){ if (flag ==0){ Push (&OPND,c); flag =getNext(&c); } else{ GetTop(OPTR,&x); switch(Precede(x,c)){ case '<': Push(&OPTR,c); flag=getNext(&c); break; case '=': Pop(&OPTR,&x); flag=getNext(&c); break; case '>': Pop(&OPTR,&theta); Pop(&OPND,&b); Pop(&OPND,&a); Push(&OPND,Operate (a,theta,b)); break; } } GetTop(OPTR,&x); } GetTop(OPND,&c); return c;}