#include<iostream.h> typedef struct position { int i; int j; }POSITION; typedef struct node { POSITION pos; struct node* pNext; }NODE,*PNODE; class mystack { private: PNODE pTop,pBottom; public: mystack() { pTop = pBottom = new NODE; pTop->pNext = NULL; } void push(POSITION pos) { PNODE pNew = new NODE; pNew->pos.i = pos.i; pNew->pos.j = pos.j; pNew->pNext = pTop; pTop = pNew; } int pop(POSITION& pos) { if(this->pTop == this->pBottom) { return -1;//NULL == 0 } pos = this->pTop->pos; PNODE pTemp = this->pTop; this->pTop = this->pTop->pNext; delete pTemp; return 0; } POSITION GetTop() { return this->pTop->pos; } bool isEmpety() { if(this->pTop == this->pBottom) { return true; } else return false; } void print() { PNODE pTemp = pTop; while(pTemp != pBottom) { cout<<pTemp->pos.i <<" "<<pTemp->pos.j<<endl; pTemp = pTemp->pNext; } cout<<endl; } }; int a[5][5] ={{0,1,0,1,1},{1,1,0,1,1},{1,0,0,1,0},{1,0,1,1,0},{1,1,1,0,0}}; int flag[5][5]; void InitFlag() { for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { flag[i][j] = false; } } } void walk(POSITION start_pos,POSITION end_pos) { InitFlag(); bool end_flag = false; mystack stack; flag[start_pos.i][start_pos.j] = true; stack.push(start_pos); while(!stack.isEmpety() && end_flag == false) { POSITION curpos = stack.GetTop(); //遍历4个方位 for(int k=0;k<4;k++) { POSITION newpos; if(k==0) { newpos.i = curpos.i; newpos.j = curpos.j-1; } else if(k==1) { newpos.i = curpos.i +1 ; newpos.j = curpos.j; } else if(k==2) { newpos.i = curpos.i; newpos.j = curpos.j+1; } else { newpos.i = curpos.i-1; newpos.j = curpos.j; } //找到出口 if(newpos.i == end_pos.i && newpos.j == end_pos.j) { flag[newpos.i][newpos.j] = true; stack.push(newpos); end_flag = true; break; } //越界 if(newpos.i<0 || newpos.i>4 || newpos.j<0 || newpos.j>4) { continue; } //新方位值为1(通路),并且没有访问过 if(a[newpos.i][newpos.j] == 1 && flag[newpos.i][newpos.j] == false) { flag[newpos.i][newpos.j] = true; //访问标记置为已访问 stack.push(newpos);//入栈 break; } else continue; } if(k==4)//四个方位都是死的 { POSITION pos; stack.pop(pos);//出栈 } } //输出路径 stack.print(); } void main() { POSITION start_pos,end_pos ; start_pos.i = 4; start_pos.j = 0; end_pos.i = 0; end_pos.j = 4; walk(start_pos,end_pos); } 数据结构----树 #include<iostream.h> typedef struct node { char val; struct node* pLeft; struct node* pRight; }NODE,*PNODE; void staticcons(PNODE & pRoot) { pRoot = new NODE; pRoot->val = 'A'; pRoot->pLeft = NULL; pRoot->pRight = NULL; PNODE pNew = new NODE; pNew->val = 'B'; pNew->pLeft = NULL; pNew->pRight = NULL; pRoot->pLeft = pNew; pNew = new NODE; pNew->val = 'C'; pNew->pLeft = NULL; pNew->pRight = NULL; pRoot->pRight = pNew; pNew = new NODE; pNew->val = 'D'; pNew->pLeft = NULL; pNew->pRight = NULL; pRoot->pLeft->pLeft = pNew; pNew = new NODE; pNew->val = 'E'; pNew->pLeft = NULL; pNew->pRight = NULL; pRoot->pLeft->pRight = pNew; pNew = new NODE; pNew->val = 'F'; pNew->pLeft = NULL; pNew->pRight = NULL; pRoot->pRight->pLeft = pNew; } void FTravel(PNODE pRoot) //先序排列根节点,从上到下从左到右输出 { if(pRoot != NULL) { cout<<pRoot->val<<endl; FTravel(pRoot->pLeft); FTravel(pRoot->pRight); } } void MTravel(PNODE pRoot) //中序排列根节点,从下向上从左到右输出 { if(pRoot != NULL) { MTravel(pRoot->pLeft); cout<<pRoot->val<<endl; MTravel(pRoot->pRight); } } void ETravel(PNODE pRoot) //从右到左输出 { if(pRoot != NULL) { ETravel(pRoot->pRight); cout<<pRoot->val<<endl; ETravel(pRoot->pLeft); } } void main() { PNODE pRoot; staticcons(pRoot); FTravel(pRoot); cout<<endl; MTravel(pRoot); cout<<endl; ETravel(pRoot); }