数据结构-习题集答案-(C语言版严蔚敏) 联系客服

发布时间 : 星期三 文章数据结构-习题集答案-(C语言版严蔚敏)更新完毕开始阅读57039dfb9f3143323968011ca300a6c30c22f1d1

为同色区域的点。

解:

#include #include

typedef struct{

int x; int y;

}PosType; typedef struct{

#include \

#define M 8 #define N 8

ElemType g[M][N];

void CreateGDS(ElemType g[M][N]); void ShowGraphArray(ElemType g[M][N]);

void RegionFilling(ElemType g[M][N],PosType CurPos,int NewColor);

int main() { }

void RegionFilling(ElemType g[M][N],PosType CurPos,int FillColor) {

Stack s;

PosType StartPos; StartPos.x=5; StartPos.y=5; int FillColor=6;

RegionFilling(g,StartPos,FillColor); cout<

}ElemType;

}

void CreateGDS(ElemType g[M][N]) {

int i,j; for(i=0;i

for(j=0;j

g[i][j].seat.x=i; g[i][j].seat.y=j; g[i][j].Visited=0; g[i][j].Color=0;

}

if(CurPos.x

Push(s,g[CurPos.x][CurPos.y-1]); Push(s,g[CurPos.x][CurPos.y+1]); !g[CurPos.x][CurPos.y-1].Visited && g[CurPos.x][CurPos.y-1].Color==OldColor if(CurPos.y>0 &&

Push(s,g[CurPos.x-1][CurPos.y]); !g[CurPos.x][CurPos.y+1].Visited && g[CurPos.x][CurPos.y+1].Color==OldColor if(CurPos.y

Push(s,g[CurPos.x+1][CurPos.y]); !g[CurPos.x-1][CurPos.y].Visited && g[CurPos.x-1][CurPos.y].Color==OldColor if(CurPos.x>0 &&

!g[CurPos.x+1][CurPos.y].Visited && g[CurPos.x+1][CurPos.y].Color==OldColor

InitStack(s); ElemType e;

int OldColor=g[CurPos.x][CurPos.y].Color;

Push(s,g[CurPos.x][CurPos.y]); while(!StackEmpty(s)){

Pop(s,e); CurPos=e.seat;

g[CurPos.x][CurPos.y].Color=FillColor; g[CurPos.x][CurPos.y].Visited=1;

}

void ShowGraphArray(ElemType g[M][N]) { }

3.21 假设表达式有单字母变量和双目四则运算符构成。试写一个算法,将一个通常书写形式且书写正确的表达式转换为逆波兰表达式。

解:

// 输入的表达式串必须为#...#格式

void InversePolandExpression(char Buffer[]) {

Push(s,Buffer[i]); i++;

while(Buffer[i]!='#'){

if(!IsOperator(Buffer[i])){ // 是操作数 } else{

// 是操作符 GetTop(s,e);

if(Prior(e,Buffer[i])){// 当栈顶优先权高于当前序列时,退栈 } else{

Pop(s,e); Buffer[j]=e; j++;

Buffer[j]=Buffer[i]; i++; j++;

Stack s; InitStack(s); int i=0,j=0; ElemType e; int i,j;

for(i=0;i

for(j=0;j

cout<

for(j=2;j<4;j++)

g[i][j].Color=3;

for(i=5;i

for(j=3;j<6;j++)

g[i][j].Color=3;

}

Status IsOpertor(char c) { }

Status Prior(char c1,char c2) { }

3.22 如题3.21的假设条件,试写一个算法,对以逆波兰式表示的表达式求值。

解:

char CalVal_InverPoland(char Buffer[]) {

Stack Opnd; InitStack(Opnd); int i=0; char c;

char ch[]=\int i=0,j=0;

while(ch[i] && ch[i]!=c1) i++;

if(i==2) i--; // 加和减可认为是同级别的运算符 if(i==4) i--; // 乘和除可认为是同级别的运算符 while(ch[j] && ch[j]!=c2) j++; if(j==2) j--; if(j==4) j--;

if(i>=j) return TRUE; else return FALSE; char *p=\while(*p){ }

return FALSE;

if(*p==c)

return TRUE; p++; }

while(!StackEmpty(s)){ }

Pop(s,e); Buffer[j]=e; j++; }

}

Push(s,Buffer[i]); i++;