//#pragma once
#include "stdafx.h"
#define MAXLEN 100
typedef struct
{
char ch;
int flag;
}sltype;
typedef struct
{
sltype *base;
sltype *top;
int stacksize;
}sqstack;
bool initstack(sqstack &s);
bool push(sqstack &s,sltype e);
bool pop(sqstack &s,sltype &e);
bool gettop(sqstack &s,sltype &e);
void destore(sqstack &s);
bool initstack(sqstack &s)
{
s.base = (sltype *)malloc(MAXLEN * sizeof(sltype));
if(NULL == s.base)
return false;
s.top = s.base;
s.stacksize = MAXLEN;
return true;
}
bool push(sqstack &s,sltype e)
{
if(s.top - s.base >= s.stacksize)
{
s.base = (sltype *)realloc(s.base,(s.stacksize+MAXLEN)*sizeof(sltype));
if(NULL == s.base)
return false;
s.top = s.base + s.stacksize;
s.stacksize += MAXLEN;
}
*s.top ++ = e;
return true;
}
bool pop(sqstack &s,sltype &e)
{
if(s.top == s.base)
return false;
e = *(--s.top);
return true;
}
bool gettop(sqstack &s,sltype &e)
{
if(s.top == s.base )
return false;
e = *(s.top - 1);
return true;
}
void destore(sqstack &s)
{
sltype e = {0,0};
if ( s.top != s.base)
pop(s,*&e);
else
{
free(s.base);
s.base = NULL;
}
}