64,497
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
template <class T>
struct LinkNode{
T data;
LinkNode* next;
};
template <class T>
class LinkedQueue{
public:
LinkedQueue():rear(NULL),front(NULL){}
~LinkedQueue(){makeEmpty();}
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool getFront(T& x)const;
void makeEmpty();
bool IsEmpty()const{return(front==NULL)?true:false;}
int getSize()const;
friend ostream& operator<<(ostream& os,LinkedQueue<T>& Q);
protected:
LinkNode<T>*front,*rear;
};
template <class T>
void LinkedQueue<T>::makeEmpty(){
LinkNode<T> *p;
while(front!=NULL){
p=front;
front=front->next;
delete p;
}
}
template <class T>
int LinkedQueue<T>::getSize()const{
int count=0;
LinkNode<T> *p=front;
while(p!=NULL){
++count;
p=p->next;
}
return count;
}
template <class T>
bool LinkedQueue<T>::getFront(T& x) const{
if(front==NULL) return false;
x=front->data;
return true;
}
template <class T>
bool LinkedQueue<T>::EnQueue(const T& x){
if(front==NULL){
rear=front=new LinkNode<T>;
front->data=x;
if(front==NULL)
return false;
}
else{
LinkNode<T> *p=new LinkNode<T>;
if(p==NULL)
return false;
p->data=x;
rear->next=p;
rear=rear->next;
}
return true;
}
template <class T>
bool LinkedQueue<T>::DeQueue(T& x){
if(IsEmpty()==true)
return false;
x=front->data;
LinkNode<T> *p=front;
front=front->next;
delete p;
return true;
}
template <class T>
ostream& operator<<(ostream& os,LinkedQueue<T>& Q){
cout<<"The number of the Queue is:"<<Q.getSize()<<endl;
LinkNode<T> *p=Q.front;int i=0;
while(p!=NULL){
cout<<i+1<<' '<<p->data<<endl;
p=p->next;
}
}
#include "队列模板.cpp"
class Position{
public:
int row;
int col;
Position(){}
Position(int x,int y):row(x),col(y){}
//Position(Position& P2){row=P2.row;col=P2.col;}
friend ostream& operator<<(ostream&,Position& );
};
ostream& operator<<(ostream& os,Position& item)
{
os<<'<'<<item.row<<','<<item.col<<'>'<<' ';
return os;
}
int grid[9][9]={
{1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,0,1},
{1,0,0,1,1,0,0,0,1},
{1,0,0,0,0,1,0,0,1},
{1,0,0,0,1,1,0,0,1},
{1,1,0,0,0,1,0,0,1},
{1,1,1,1,0,0,0,0,1},
{1,1,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1}
};
bool FindPath(Position start,Position finish,int& PathLen,Position*& path)
{
Position offsets[4];
offsets[0].row=0;offsets[0].col=1;
offsets[1].row=1;offsets[1].col=0;
offsets[2].row=0;offsets[2].col=-1;
offsets[3].row=-1;offsets[3].col=0;
if(start.row==finish.row&&start.col==finish.col)
{PathLen=0;return false;}
int NumOfNbrs=4,i,j;
Position here,nbr;
here.row=start.row;here.col=start.col;
grid[start.row][start.col]=2;
LinkedQueue<Position> Q;
do{
for(i=0;i!=NumOfNbrs;i++){
nbr.row=here.row+offsets[i].row;
nbr.col=here.col+offsets[i].col;
if(grid[nbr.row][nbr.col]==0){
grid[nbr.row][nbr.col]=grid[here.row][here.col]+1;
if(nbr.row==finish.row&&nbr.col==finish.col)
break;
Q.EnQueue(nbr);
}
}
if(nbr.row==finish.row&&nbr.col==finish.col) break;
if(Q.IsEmpty()==true)return false;
if(Q.DeQueue(here)==false)
cout<<"Queue out Error!"<<endl;
}while(true);
PathLen=grid[finish.row][finish.col]-2;
path=new Position[PathLen];
here=finish;
for(j=PathLen-1;j>=0;j--){
path[j]=here;
for(i=0;i!=NumOfNbrs;i++){
nbr.row=here.row+offsets[i].row;
nbr.col=here.col+offsets[i].col;
if(grid[nbr.row][nbr.col]==j+2)
break;
}
here=nbr;
}
return true;
}
int main(){
Position st(4,3),end(5,4);
int length=0;
Position *p=NULL;
if(FindPath(st,end,length,p)==true)
{
for(int i=0;i!=length;i++)
cout<<p[i]<<' ';
//cout<<"Good!"<<endl;
}
else cout<<"No Path!"<<endl;
return 0;
}
#include <iostream>
using namespace std;
template <class T>
struct LinkNode{
T data;
LinkNode* next;
};
template <class T>
class LinkedQueue{
public:
LinkedQueue():rear(NULL),front(NULL){}
~LinkedQueue(){makeEmpty();}
bool EnQueue(const T& x);
bool DeQueue(T& x);
bool getFront(T& x)const;
void makeEmpty();
bool IsEmpty()const{return(front==NULL)?true:false;}
int getSize()const;
friend ostream& operator<<(ostream& os,LinkedQueue<T>& Q);
protected:
LinkNode<T>*front,*rear;
};
template <class T>
void LinkedQueue<T>::makeEmpty(){
LinkNode<T> *p;
while(front!=NULL){
p=front;
front=front->next;
delete p;
}
}
template <class T>
int LinkedQueue<T>::getSize()const{
int count=0;
LinkNode<T> *p=front;
while(p!=NULL){
++count;
p=p->next;
}
return count;
}
template <class T>
bool LinkedQueue<T>::getFront(T& x) const{
if(front==NULL) return false;
x=front->data;
return true;
}
template <class T>
bool LinkedQueue<T>::EnQueue(const T& x){
if(front==NULL){
rear=front=new LinkNode<T>;
front->next=NULL;
front->data=x;
if(front==NULL)
return false;
}
else{
LinkNode<T> *p=new LinkNode<T>;
if(p==NULL)
return false;
p->data=x;
p->next=NULL;
rear->next=p;
rear=rear->next;
}
return true;
}
template <class T>
bool LinkedQueue<T>::DeQueue(T& x){
if(IsEmpty()==true)
return false;
x=front->data;
LinkNode<T> *p=front;
front=front->next;
delete p;
return true;
}
template <class T>
ostream& operator<<(ostream& os,LinkedQueue<T>& Q){
cout<<"The number of the Queue is:"<<Q.getSize()<<endl;
LinkNode<T> *p=Q.front;int i=0;
while(p!=NULL){
cout<<i+1<<' '<<p->data<<endl;
p=p->next;
}
}
#include "队列模板.cpp"
class Position{
public:
int row;
int col;
Position(){}
Position(int x,int y):row(x),col(y){}
//Position(Position& P2){row=P2.row;col=P2.col;}
friend ostream& operator<<(ostream&,Position& );
};
ostream& operator<<(ostream& os,Position& item)
{
os<<'<'<<item.row<<','<<item.col<<'>'<<' ';
return os;
}
int grid[9][9]={
{1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,0,1},
{1,0,0,1,1,0,0,0,1},
{1,0,0,0,0,1,0,0,1},
{1,0,0,0,1,1,0,0,1},
{1,1,0,0,0,1,0,0,1},
{1,1,1,1,0,0,0,0,1},
{1,1,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1}
};
bool FindPath(Position start,Position finish,int& PathLen,Position*& path)
{
Position offsets[4];
offsets[0].row=0;offsets[0].col=1;
offsets[1].row=1;offsets[1].col=0;
offsets[2].row=0;offsets[2].col=-1;
offsets[3].row=-1;offsets[3].col=0;
if(start.row==finish.row&&start.col==finish.col)
{PathLen=0;return false;}
int NumOfNbrs=4,i,j;
Position here,nbr;
here.row=start.row;here.col=start.col;
grid[start.row][start.col]=2;
LinkedQueue<Position> Q;
do{
for(i=0;i!=NumOfNbrs;i++){
nbr.row=here.row+offsets[i].row;
nbr.col=here.col+offsets[i].col;
if(grid[nbr.row][nbr.col]==0){
grid[nbr.row][nbr.col]=grid[here.row][here.col]+1;
if(nbr.row==finish.row&&nbr.col==finish.col)
break;
Q.EnQueue(nbr);
}
}
if(nbr.row==finish.row&&nbr.col==finish.col) break;
if(Q.IsEmpty()==true)return false;
if(Q.DeQueue(here)==false)
cout<<"Queue out Error!"<<endl;
}while(true);
PathLen=grid[finish.row][finish.col]-2;
path=new Position[PathLen];
here=finish;
for(j=PathLen-1;j>=0;j--){
path[j]=here;
for(i=0;i!=NumOfNbrs;i++){
nbr.row=here.row+offsets[i].row;
nbr.col=here.col+offsets[i].col;
if(grid[nbr.row][nbr.col]==j+2)
break;
}
here=nbr;
}
return true;
}
int main()
{
Position st(4,3),end(5,4);
int length=0;
Position *p=NULL;
if(FindPath(st,end,length,p)==true)
{
for(int i=0;i!=length;i++)
cout<<p[i]<<' ';
//cout<<"Good!"<<endl;
}
else cout<<"No Path!"<<endl;
//LinkedQueue<int> a;
//a.EnQueue(1);
return 0;
}
//现在运行正常了,终于又能上csdn了,myGod!
//vc6下运行结果:
<5,3> <5,4> Press any key to continue
template <class T>
bool LinkedQueue<T>::EnQueue(const T& x){
if(front==NULL){
rear=front=new LinkNode<T>;
front->next=NULL;
front->data=x;
if(front==NULL)
return false;
}
else{
LinkNode<T> *p=new LinkNode<T>;
if(p==NULL)
return false;
p->data=x;
p->next=NULL;
rear->next=p;
rear=rear->next;
}
return true;
}
template <class T>
bool LinkedQueue<T>::DeQueue(T& x){
if(IsEmpty()==true)
return false;
x=front->data;
LinkNode<T> *p=front;
front=front->next;
delete p;
return true;
}
int main()
{
LinkedQueue<int> a;
a.EnQueue(1);
return 0;
}