西北大学《数据结构》典型例题(1-5章)(8)

2019-01-12 17:06

while((e=getchar())!='&') push(s,e);

while((e=getchar())!='@') {

if(StackEmpty(s)) return 0; pop(s,c); if(e!=c) return 0; }

if(!StackEmpty(s)) return 0; return 1; }//IsReverse 3.18

Status Bracket_Test(char *str)//判别表达式中小括号是否匹配 {

count=0; for(p=str;*p;p++) {

if(*p=='(') count++; else if(*p==')') count--; if (count<0) return ERROR; }

if(count) return ERROR; //注意括号不匹配的两种情况 return OK; }//Bracket_Test 3.19

Status AllBrackets_Test(char *str)//判别表达式中三种括号是否匹配 {

InitStack(s); for(p=str;*p;p++) {

if(*p=='('||*p=='['||*p=='{') push(s,*p); else if(*p==')'||*p==']'||*p=='}') {

if(StackEmpty(s)) return ERROR; pop(s,c);

if(*p==')'&&c!='(') return ERROR; if(*p==']'&&c!='[') return ERROR;

if(*p=='}'&&c!='{') return ERROR; //必须与当前栈顶括号匹配 } }//for

if(!StackEmpty(s)) return ERROR; return OK; }//AllBrackets_Test 3.20

void Repaint_Color(int g[m][n],int i,int j,int color)//把点(i,j)相邻区域的颜色置换为color {

old=g[i][j]; InitQueue(Qx,Qy);

EnQueue(Qx,i);EnQueue(Qy,j); while(!QueueEmpty(Qx)) {

DeQueue(Qx,x);Dequeue(Qy,y); if(x>1)

if(g[x-1][y]==old) {

g[x-1][y]=color;

EnQueue(Qx,x-1);EnQueue(Qy,y); //修改左邻点的颜色 } if(y>1)

if(g[x][y-1]==old) {

g[x][y-1]=color;

EnQueue(Qx,x);EnQueue(Qy,y-1); //修改上邻点的颜色 } if(x

if(g[x+1][y]==old) {

g[x+1][y]=color;

EnQueue(Qx,x+1);EnQueue(Qy,y); //修改右邻点的颜色 } if(y

if(g[x][y+1]==old) {

g[x][y+1]=color;

EnQueue(Qx,x);EnQueue(Qy,y+1); //修改下邻点的颜色 } }//while }//Repaint_Color

分析:本算法采用了类似于图的广度优先遍历的思想,用两个队列保存相邻同色点的横坐标和纵坐标.递归形式的算法该怎么写呢? 3.21

void NiBoLan(char *str,char *new)//把中缀表达式str转换成逆波兰式new {

p=str;q=new; //为方便起见,设str的两端都加上了优先级最低的特殊符号 InitStack(s); //s为运算符栈 while(*p) {

if(*p是字母)) *q++=*p; //直接输出 else {

c=gettop(s);

if(*p优先级比c高) push(s,*p); else {

while(gettop(s)优先级不比*p低) {

pop(s,c);*(q++)=c; }//while

push(s,*p); //运算符在栈内遵循越往栈顶优先级越高的原则 }//else }//else p++; }//while

}//NiBoLan //参见编译原理教材 3.22

int GetValue_NiBoLan(char *str)//对逆波兰式求值 {

p=str;InitStack(s); //s为操作数栈 while(*p) {

if(*p是数) push(s,*p); else {

pop(s,a);pop(s,b);

r=compute(b,*p,a); //假设compute为执行双目运算的过程 push(s,r); }//else p++; }//while

pop(s,r);return r; }//GetValue_NiBoLan 3.23

Status NiBoLan_to_BoLan(char *str,stringtype &new)//把逆波兰表达式str转换为波兰式new {

p=str;Initstack(s); //s的元素为stringtype类型 while(*p) {

if(*p为字母) push(s,*p); else {

if(StackEmpty(s)) return ERROR; pop(s,a);

if(StackEmpty(s)) return ERROR; pop(s,b);

c=link(link(*p,b),a); push(s,c); }//else p++; }//while pop(s,new);

if(!StackEmpty(s)) return ERROR; return OK;

}//NiBoLan_to_BoLan

分析:基本思想见书后注释.本题中暂不考虑串的具体操作的实现,而将其看作一种抽象数据类型stringtype,对其可以进行连接操作:c=link(a,b). 3.24

Status g(int m,int n,int &s)//求递归函数g的值s {

if(m==0&&n>=0) s=0;

else if(m>0&&n>=0) s=n+g(m-1,2*n); else return ERROR; return OK; }//g 3.25

Status F_recurve(int n,int &s)//递归算法 {

if(n<0) return ERROR; if(n==0) s=n+1; else {

F_recurve(n/2,r); s=n*r; }


西北大学《数据结构》典型例题(1-5章)(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:门电路逻辑功能测试实验报告 - 图文

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

马上注册会员

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