【深度优先遍历】运行出错

buyaozaiwangji 2009-05-17 03:19:23
//graph.h
//邻接链表
#include<iostream>
using namespace std;
const int Default=10;
//边结点结构
struct Edge
{
friend class Graph;
int VerAdj; //邻接顶点序号
int cost; //边的权值
Edge *link; //指向下一边结点的指针
//Edge(void);
//构造函数
Edge(int d,int c):VerAdj(d),cost(c),link(NULL){}
int operator !=(const Edge &E)const //操作符重载
{return VerAdj !=E.VerAdj;}
};
//顶点表中结点的结构

struct Vertex
{
friend class Graph;
char VerName; //的名称
Edge *Adjacent;
};

//图类的定义
class Graph
{
public:
Vertex *head; //顶点表的头指针
int graphsize; //当前顶点个数
int MaxGraphSize; //最大顶点个数
int NumEdge; //当前边数
int MaxNumEdge; //最大边数
int *visited;

//返回顶点vertex在顶点表中的序号
int GetVertexPos(const char &vertex);
//返回序号为v的定点的名称
int GetName(int v)
{
return head[v].VerName;
}

Graph(int sz); //构造
//~Graph(); //析构
//测空
int GraphEmpty(void)const{return graphsize==0;}
//测满
int GraphFull(void)const
{return graphsize==MaxGraphSize ||NumEdge==MaxNumEdge;}
//数据访问函数
int NumberOfVertex(void)const{return graphsize;}
int NumberOfEdge(void)const{return NumEdge;}
int GetWeight(const char &vertex1,const char &vertex2);
Edge * GetNeighbors(const char &vertex);
int GetFirstNeighbor(const int v);
int GetNextNeighbor(const int v1,const int v2);
//修改图的函数
void InsertVertex(const char & vertex);
void InsertEdge(const char & vertex1,const char & vertex2,int weight);
void DFS(int v);

};
//--------------------------------新加的bianli
void Graph::DFS(int v)
{

visited[v]=1;//v北方问过
cout<<GetName(v)<<" ";//输出v的名字
for(int w=GetFirstNeighbor(v);w>=0;w=GetNextNeighbor(v,w))
if(visited[w]==0)
DFS(w);//w未被访问过,从w递归访问

delete[] visited;
}


//插入顶点
void Graph::InsertVertex(const char & vertex)
{
if(!GraphFull())
{
head[graphsize].VerName=vertex;
head[graphsize].Adjacent=NULL;
graphsize++;
}
}

//插入边
void Graph::InsertEdge(const char & vertex1,const char & vertex2,int weight)
{
if(!GetWeight(vertex1,vertex2))
{

Edge * tt;//临时变量
int i1=GetVertexPos(vertex1),i2=GetVertexPos(vertex2);
Edge *temp=new Edge(i2,weight);

Edge * p1=head[i1].Adjacent;
if(p1==NULL)head[i1].Adjacent=temp;
else
{
while(p1!=NULL && p1->VerAdj<i2)
{
tt=p1;
p1=p1->link;
}
tt->link=temp;
temp->link=p1;
}
}
}

Graph ::Graph(const int sz=Default):graphsize(0),MaxGraphSize(sz),NumEdge(0)
{
int n,e,weight;
char name,from,to;
head = new Vertex [MaxGraphSize];
//用数组实现顶点表,head指向数组的第一个元素
cout<<"输入顶点个数:"<<endl;
cin>>n;
cout<<"输入这些点:";
for(int i=0;i<n;i++)//依次读入ding点,插入土中
{
cin>>name;
InsertVertex(name);
}
cout<<"输入边个数:"<<endl;
cin>>e;
cout<<"按照 <起点 终点 权值> 的格式输入各边:"<<endl;
for(i=0;i<e;i++)//依次读入边,插入土中
{
cin>>from>>to>>weight;
InsertEdge(from,to,weight);
}
visited=new int[graphsize];
}

int Graph::GetVertexPos(const char & vertex)
{
for(int i=0;i<graphsize;i++)
if(head[i].VerName==vertex)
return i;
return -1;
}
int Graph::GetWeight(const char & vertex1,const char & vertex2)//求边的权值
{
int a=GetVertexPos(vertex1);
int b=GetVertexPos(vertex2);
if(a!=-1&&b!=-1)
{
Edge *p = head[a].Adjacent;//vertex1边链表的头指针
while(p!=NULL)//循环搜索次边
{
if(p->VerAdj==b)
return p->cost;//找到,返回边的权值
p=p->link;
}

}
return 0;//此边不存在;
}
Edge *Graph ::GetNeighbors(const char & vertex)//求vertex的边链表
{
int i=GetVertexPos(vertex);
if(i!=-1)return head[i].Adjacent;
return NULL;
}

int Graph::GetFirstNeighbor(const int v)//求序号为v的顶点的第一个邻接顶点的序号
{
if(v!=-1)
{
Edge *p=head[v].Adjacent;
int w=p->VerAdj;
return w;
}
return -1;
}
int Graph::GetNextNeighbor(const int v1,const int v2)//求序号v1的顶点相对于v2的下一个邻接顶点的序号
{
if(v1!= -1&&v2!=-1)
{
Edge*p=head[v1].Adjacent;
while(p->VerAdj!=v2)
p=p->link;
p=p->link;
return p->VerAdj;
}
return -1;
}


//main.cpp
#include"graph.h"
#include<iostream>
using namespace std;
//计算每个节点的度,并输出每个顶点的度
void Degree(Graph & gg)
{
int j=0;
while(j<gg.graphsize)
{
Edge *p=(gg.head[j]).Adjacent;
int i=0;
while(p)
{
i++;
p=p->link;
}
cout<<"第"<<j<<"个结点度为:"<<i<<endl;
j++;
}
}
int main()
{
Graph g(20);//最大点数
Degree(g);
int n;
cout<<"input n please:";
cin>>n;
g.DFS(n);
return 0;
}


如上,不知道咋回事?大侠们解释下?
...全文
80 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingyin55 2009-05-17
  • 打赏
  • 举报
回复
帮顶下吧
  • 打赏
  • 举报
回复
太长了,帮顶
buyaozaiwangji 2009-05-17
  • 打赏
  • 举报
回复
连帮忙顶的人都没有|?
buyaozaiwangji 2009-05-17
  • 打赏
  • 举报
回复
//graph.h 和上面的一样,上面写的太大了,影响视觉,看这个一样
//邻接链表
#include <iostream>
using namespace std;
const int Default=10;
//边结点结构
struct Edge
{
friend class Graph;
int VerAdj; //邻接顶点序号
int cost; //边的权值
Edge *link; //指向下一边结点的指针
//Edge(void);
//构造函数
Edge(int d,int c):VerAdj(d),cost(c),link(NULL){}
int operator !=(const Edge &E)const //操作符重载
{return VerAdj !=E.VerAdj;}
};
//顶点表中结点的结构

struct Vertex
{
friend class Graph;
char VerName; //的名称
Edge *Adjacent;
};

//图类的定义
class Graph
{
public:
Vertex *head; //顶点表的头指针
int graphsize; //当前顶点个数
int MaxGraphSize; //最大顶点个数
int NumEdge; //当前边数
int MaxNumEdge; //最大边数
int *visited;

//返回顶点vertex在顶点表中的序号
int GetVertexPos(const char &vertex);
//返回序号为v的定点的名称
int GetName(int v)
{
return head[v].VerName;
}

Graph(int sz); //构造
//~Graph(); //析构
//测空
int GraphEmpty(void)const{return graphsize==0;}
//测满
int GraphFull(void)const
{return graphsize==MaxGraphSize ||NumEdge==MaxNumEdge;}
//数据访问函数
int NumberOfVertex(void)const{return graphsize;}
int NumberOfEdge(void)const{return NumEdge;}
int GetWeight(const char &vertex1,const char &vertex2);
Edge * GetNeighbors(const char &vertex);
int GetFirstNeighbor(const int v);
int GetNextNeighbor(const int v1,const int v2);
//修改图的函数
void InsertVertex(const char & vertex);
void InsertEdge(const char & vertex1,const char & vertex2,int weight);
void DFS(int v);

};
//--------------------------------新加的bianli
void Graph::DFS(int v)
{

visited[v]=1;//v北方问过
cout < <GetName(v) < <" ";//输出v的名字
for(int w=GetFirstNeighbor(v);w>=0;w=GetNextNeighbor(v,w))
if(visited[w]==0)
DFS(w);//w未被访问过,从w递归访问

delete[] visited;
}


//插入顶点
void Graph::InsertVertex(const char & vertex)
{
if(!GraphFull())
{
head[graphsize].VerName=vertex;
head[graphsize].Adjacent=NULL;
graphsize++;
}
}

//插入边
void Graph::InsertEdge(const char & vertex1,const char & vertex2,int weight)
{
if(!GetWeight(vertex1,vertex2))
{

Edge * tt;//临时变量
int i1=GetVertexPos(vertex1),i2=GetVertexPos(vertex2);
Edge *temp=new Edge(i2,weight);

Edge * p1=head[i1].Adjacent;
if(p1==NULL)head[i1].Adjacent=temp;
else
{
while(p1!=NULL && p1->VerAdj <i2)
{
tt=p1;
p1=p1->link;
}
tt->link=temp;
temp->link=p1;
}
}
}

Graph ::Graph(const int sz=Default):graphsize(0),MaxGraphSize(sz),NumEdge(0)
{
int n,e,weight;
char name,from,to;
head = new Vertex [MaxGraphSize];
//用数组实现顶点表,head指向数组的第一个元素
cout < <"输入顶点个数:" < <endl;
cin>>n;
cout < <"输入这些点:";
for(int i=0;i <n;i++)//依次读入ding点,插入土中
{
cin>>name;
InsertVertex(name);
}
cout < <"输入边个数:" < <endl;
cin>>e;
cout < <"按照 <起点 终点 权值> 的格式输入各边:" < <endl;
for(i=0;i <e;i++)//依次读入边,插入土中
{
cin>>from>>to>>weight;
InsertEdge(from,to,weight);
}
visited=new int[graphsize];
}

int Graph::GetVertexPos(const char & vertex)
{
for(int i=0;i <graphsize;i++)
if(head[i].VerName==vertex)
return i;
return -1;
}
int Graph::GetWeight(const char & vertex1,const char & vertex2)//求边的权值
{
int a=GetVertexPos(vertex1);
int b=GetVertexPos(vertex2);
if(a!=-1&&b!=-1)
{
Edge *p = head[a].Adjacent;//vertex1边链表的头指针
while(p!=NULL)//循环搜索次边
{
if(p->VerAdj==b)
return p->cost;//找到,返回边的权值
p=p->link;
}

}
return 0;//此边不存在;
}
Edge *Graph ::GetNeighbors(const char & vertex)//求vertex的边链表
{
int i=GetVertexPos(vertex);
if(i!=-1)return head[i].Adjacent;
return NULL;
}

int Graph::GetFirstNeighbor(const int v)//求序号为v的顶点的第一个邻接顶点的序号
{
if(v!=-1)
{
Edge *p=head[v].Adjacent;
int w=p->VerAdj;
return w;
}
return -1;
}
int Graph::GetNextNeighbor(const int v1,const int v2)//求序号v1的顶点相对于v2的下一个邻接顶点的序号
{
if(v1!= -1&&v2!=-1)
{
Edge*p=head[v1].Adjacent;
while(p->VerAdj!=v2)
p=p->link;
p=p->link;
return p->VerAdj;
}
return -1;

}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧