数据结构上机答案(5)

2019-02-15 19:37

struct SqStack { SElemType *base; SElemType *top; int stacksize; };

Status InitStack(SqStack &S) { S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) return ERROR; S.top=S.base; S.stacksize=STACK_INIT_SIZE; 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) return ERROR; S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; }

Status Pop(SqStack &S,SElemType &e) { if(S.top==S.base) return ERROR; e=*--S.top; return OK; }

Status StackEmpty(SqStack &S) { if(S.top==S.base) return 0;

else return 1; }

int main() { int N,e; SqStack S; InitStack(S); scanf(\ while(N) { Push(S,N%8); N=N/8; } while(StackEmpty(S)) { Pop(S,e); printf(\ } return 0; }

2.4括号匹配检验

typedef char SElemType; #include #include #include #include #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status;

#define STACK_INIT_SIZE 10 #define STACKINCREMENT 2

struct SqStack { SElemType *base; SElemType *top; int stacksize; };

Status InitStack(SqStack &S) { S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) return 0; 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 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) return 0; S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; }

Status Pop(SqStack &S,SElemType &e) { if(S.top==S.base) return ERROR; e=*--S.top; return OK; }

void check() {

SqStack s;

SElemType ch[80],*p,e; if(InitStack(s)) {

gets(ch); p=ch; while(*p) switch(*p) {

case '(':

case '[':Push(s,*p++); break; case ')':

case ']':if(!StackEmpty(s)) {

Pop(s,e);

if(*p==')'&&e!='('||*p==']'&&e!='[') {

printf(\ return ; } else {

p++ ; break; } } else {

printf(\ return ; } default: p++; }

if(StackEmpty(s))

printf(\ else

printf(\ } }

int main() { check(); return 1;

}

2.5行编辑程序

typedef char SElemType; #include #include #include #include #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status;

#define STACK_INIT_SIZE 10 #define STACKINCREMENT 2

struct SqStack { SElemType *base; SElemType *top; int stacksize; };

FILE *fp;

Status InitStack(SqStack &S) { S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) return 0; 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 ClearStack(SqStack &S) { S.top=S.base;


数据结构上机答案(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:线塔3 混凝土电杆组立检查及评级记录表

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: