基于链表的图搜索
2016-07-02 17:56
260 查看
#include<iostream>
#include<iomanip>
using namespace std;
//构造一个循环队列来存放广度优先算法的下标
#define ADD 5;
using namespace std;
class CirQueue
{
private:
int * base;
int front,rear,size,length;
public:
bool InitCirQueue(int size)
{
this->size=size;
base=new int[size];
if(!base)
{
return false;
}
front=rear=length=0;
return true;
}
bool insertQueue(int num)
{
if(length==size)
{
int newsize=size+ADD;
int * newbase=new int[newsize];
if(!newbase)
{
return false;
}
int i=0;
for(i=0;i<length;i++)
{
newbase[(front+i)%newsize]=base[(front+i)%size];
}
rear=(front+i)%newsize;
base=newbase;
size=newsize;
}
base[rear]=num;
rear=(rear+1)%size;
++length;
return true;
}
int outQueue()
{
int temp=base[front];
front=(front+1)%size;
--length;
return temp;
}
void traverseQueue()
{
for(int i=0;i<length;i++)
{
cout << "V" << base[(front + i) % size]<<" , ";
}
}
int getLength()
{
return length;
}
bool isEmpty()
{
if(0==length)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(length==size)
{
return true;
}
else
{
return false;
}
}
};
struct Arc
{
int adjvex;
Arc * next;
};
struct Vertex
{
char data;
Arc * firstarc;
};
class Map
{
private:
Vertex * vexList;
int vexNum;
int arcNum;
bool * visted;
public:
Map(int vexNum,int arcNum)
{
this->vexNum=vexNum;
this->arcNum=arcNum;
visted=new bool[vexNum];
for(int i=0;i<vexNum;i++)
{
visted[i]=false;
}
vexList=new Vertex[vexNum];
cout << endl;
for(int i=1;i<=vexNum;i++)
{
cout<<" 请输入第"<<i<<"个顶点的数据:V";
cin>>vexList[i-1].data;
vexList[i-1].firstarc=NULL;
}
cout << endl;
for(int i=0;i<arcNum;i++)
{
int a,b;
cout<<" 请输入第"<<i+1<<"条边的两顶点";
cout << endl;
cout << " 结点:V";
cin >> a;
cout << " 结点:V";
cin>>b;
cout << endl;
Arc * tempArc=new Arc;
tempArc->adjvex=b;
tempArc->next=vexList[a].firstarc;
vexList[a].firstarc=tempArc;
//因为是无向图所以是双向的
tempArc=new Arc;
tempArc->adjvex=a;
tempArc->next=vexList[b].firstarc;
vexList[b].firstarc=tempArc;
};
}
void DFS(int v)
{
cout << "V" << vexList[v].data <<" , ";
visted[v]=true;
Arc * p=vexList[v].firstarc;
while(p)
{
int u=p->adjvex;
if(!visted[u])
{
DFS(u);
}
p=p->next;
}
}
void BFS(int v)
{
CirQueue cq;
cq.InitCirQueue(5);
cout << "V" << vexList[v].data << " , ";
visted[v]=true;
cq.insertQueue(v);
while(!cq.isEmpty())
{
v=cq.outQueue();
Arc * p=vexList[v].firstarc;
while(p)
{
int j=p->adjvex;
if(!visted[j])
{
cout << "V" << vexList[j].data << " , ";
visted[j]=true;
cq.insertQueue(j);
}
p=p->next;
}
}
}
void ClearVisted()
{
for(int i=0;i<vexNum;i++)
{
visted[i]=false;
}
}
};
int main()
{
Map map(5,6);//五个定点六条边
cout << endl;
cout<<" 深度优先遍历:";
map.DFS(0);
cout << endl;
map.ClearVisted();
cout<<" 广度优先遍历:";
map.BFS(0);
cout << endl;
cout << endl;
cout << endl;
system("pause");
return 0;
}
#include<iomanip>
using namespace std;
//构造一个循环队列来存放广度优先算法的下标
#define ADD 5;
using namespace std;
class CirQueue
{
private:
int * base;
int front,rear,size,length;
public:
bool InitCirQueue(int size)
{
this->size=size;
base=new int[size];
if(!base)
{
return false;
}
front=rear=length=0;
return true;
}
bool insertQueue(int num)
{
if(length==size)
{
int newsize=size+ADD;
int * newbase=new int[newsize];
if(!newbase)
{
return false;
}
int i=0;
for(i=0;i<length;i++)
{
newbase[(front+i)%newsize]=base[(front+i)%size];
}
rear=(front+i)%newsize;
base=newbase;
size=newsize;
}
base[rear]=num;
rear=(rear+1)%size;
++length;
return true;
}
int outQueue()
{
int temp=base[front];
front=(front+1)%size;
--length;
return temp;
}
void traverseQueue()
{
for(int i=0;i<length;i++)
{
cout << "V" << base[(front + i) % size]<<" , ";
}
}
int getLength()
{
return length;
}
bool isEmpty()
{
if(0==length)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(length==size)
{
return true;
}
else
{
return false;
}
}
};
struct Arc
{
int adjvex;
Arc * next;
};
struct Vertex
{
char data;
Arc * firstarc;
};
class Map
{
private:
Vertex * vexList;
int vexNum;
int arcNum;
bool * visted;
public:
Map(int vexNum,int arcNum)
{
this->vexNum=vexNum;
this->arcNum=arcNum;
visted=new bool[vexNum];
for(int i=0;i<vexNum;i++)
{
visted[i]=false;
}
vexList=new Vertex[vexNum];
cout << endl;
for(int i=1;i<=vexNum;i++)
{
cout<<" 请输入第"<<i<<"个顶点的数据:V";
cin>>vexList[i-1].data;
vexList[i-1].firstarc=NULL;
}
cout << endl;
for(int i=0;i<arcNum;i++)
{
int a,b;
cout<<" 请输入第"<<i+1<<"条边的两顶点";
cout << endl;
cout << " 结点:V";
cin >> a;
cout << " 结点:V";
cin>>b;
cout << endl;
Arc * tempArc=new Arc;
tempArc->adjvex=b;
tempArc->next=vexList[a].firstarc;
vexList[a].firstarc=tempArc;
//因为是无向图所以是双向的
tempArc=new Arc;
tempArc->adjvex=a;
tempArc->next=vexList[b].firstarc;
vexList[b].firstarc=tempArc;
};
}
void DFS(int v)
{
cout << "V" << vexList[v].data <<" , ";
visted[v]=true;
Arc * p=vexList[v].firstarc;
while(p)
{
int u=p->adjvex;
if(!visted[u])
{
DFS(u);
}
p=p->next;
}
}
void BFS(int v)
{
CirQueue cq;
cq.InitCirQueue(5);
cout << "V" << vexList[v].data << " , ";
visted[v]=true;
cq.insertQueue(v);
while(!cq.isEmpty())
{
v=cq.outQueue();
Arc * p=vexList[v].firstarc;
while(p)
{
int j=p->adjvex;
if(!visted[j])
{
cout << "V" << vexList[j].data << " , ";
visted[j]=true;
cq.insertQueue(j);
}
p=p->next;
}
}
}
void ClearVisted()
{
for(int i=0;i<vexNum;i++)
{
visted[i]=false;
}
}
};
int main()
{
Map map(5,6);//五个定点六条边
cout << endl;
cout<<" 深度优先遍历:";
map.DFS(0);
cout << endl;
map.ClearVisted();
cout<<" 广度优先遍历:";
map.BFS(0);
cout << endl;
cout << endl;
cout << endl;
system("pause");
return 0;
}
相关文章推荐
- 4.4.C语言的陷阱之typedef
- 基于邻接矩阵的图搜索
- 4.3.函数指针
- 图解C++虚函数
- 基于回溯法的0-1背包问题
- 基于动态规划的0-1背包问题
- c++的隐藏和覆盖
- C++中explicit关键字的使用
- Jam的计数法(C++)
- C/C++中static和extern小结
- C++ 将一幅图片进行等分
- 在IOS工程中使用OC调用C语言国密算法SM4(来替换DES算法)
- static 声明的变量在C语言中有两方面的特征
- 【c++程序】提取不重复的整数
- C语言结构体类型占用的内存空间,需要考虑字节对齐
- C语言sizeof和strlen
- C/C++基本数据类型长度
- 打印1到最大的n位数
- C++多态的原因
- C++ 类的多态三(多态的原理--虚函数指针--子类虚函数指针初始化)