//file MazePath.cpp
#include "MazePath.h"
#include <stdlib.h> //for malloc()
#include <stdio.h>
int a[10][10]={
{1,1,1,1,1,1,1,1,1,1},//MazePath
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
void setPose(MazePath * ps,PosType t)//设置通道块的坐标
{
ps->seat.x=t.x;
ps->seat.y=t.y;
}
MazePath* initialize(void)
{
MazePath *pt;
PosType t;
t.x=1;
t.y=1;
pt = (MazePath* )malloc( sizeof(MazePath));
pt->ord=3;
pt->di=north;
setPose(pt,t);//begin from (1,1)
return pt;
}
void destroy(MazePath* ps)
{ MazePath* pt;
while(ps->next!=NULL)
{
pt=ps;
ps=ps->next;
free(pt);}
}
bool empty(MazePath* ps)//判断是否是空栈
{
return ps->ord==0;
}
int size(MazePath* ps)//判断栈的深度
{
return ps->ord;
}
bool cross(MazePath* ps)
{
return a[ps->seat.x][ps->seat.y]==0;
}
PosType top(MazePath* ps)//返回栈顶的坐标
{
return ps->seat;
}
MazePath* before(MazePath* pt,MazePath * ps)//返回指针指向的上一个元素
{
MazePath* pa;
pa=pt;
while (pa->next->ord<ps->ord-1)
{
pa=pa->next;
}
return pa;
}
void pop(MazePath* ps,MazePath * pHead)//栈顶出栈
{
destroy(ps);
ps=before(pHead,ps);
}
bool sear(MazePath const* pt,MazePath const* ps)//遍历,看栈顶有没有在路径上
{
while(pt!=ps)
{if((pt->seat.x ==ps->seat.x)&&(pt->seat.y==ps->seat.y)) {}
else return false;
pt=pt->next;
}
return true;
}
void push(MazePath* ps,director n)//增加新的栈顶元素,n是方向
{
MazePath *pt;
pt= (MazePath* )malloc(sizeof(MazePath));
pt->ord=ps->ord+1;
pt->di=n;
switch(ps->di)
{
case 1:
{
pt->seat.x=ps->seat.x;
pt->seat.y=ps->seat.y-1;
}
case 2:
{
pt->seat.x=ps->seat.x-1;
pt->seat.y=ps->seat.y;
}
case 3:
{
pt->seat.x=ps->seat.x;
pt->seat.y=ps->seat.y+1;
}
case 4:
{
pt->seat.x=ps->seat.x+1;
pt->seat.y=ps->seat.y;
}
}
ps->next=pt;
ps=ps->next;
}
void prints(MazePath const* pt)//打印结果
{
while (pt->next!=NULL)
{
printf("%d ",pt->ord);
printf("(");
printf("%d",pt->seat.x);
printf(",");
printf("%d",pt->seat.y);
printf(")/n");
}
}
director dirchange(MazePath* ps)//change the director
{
ps->di=(enum director)(ps->di+1);
return (enum director)(ps->di%4);
}
//~file MazePath.cpp
这是函数定义的文件,我感觉是不是做链表的时候有错
//file MazePath.h
#ifndef _C_MAZEPATH_H_INCLUDED_
#define _C_MAZEPATH_H_INCLUDED_
enum director {north=1,west,south,east};
struct PosType
{
int x;
int y;
};
struct MazePath//迷宫堆栈
{
int ord;//通道块在路径上的序号
PosType seat;//通道块在迷宫中坐标位置
enum director di;//从此通道走到"下一通道"的方向
MazePath *next;
};
MazePath* initialize(void);
void destroy(MazePath* ps);
bool empty(MazePath* ps);
int size(MazePath* ps);
bool cross(MazePath* ps);
PosType top(MazePath* ps);
MazePath* before(MazePath* pt,MazePath * ps);//for the last one
void pop(MazePath* ps,MazePath* pt);
void push(MazePath* ps,director n);
void setPose(MazePath * ps,PosType t);//for set the setPos
void prints(MazePath const* pt);
director dirchange(MazePath* ps);//change the director
bool sear(MazePath const* pt,MazePath const* ps);
#endle
AMD sempron3100+ 1.8GHZ 512M
前两天还中了个kill木马,不知道有没有影响