要求采用邻接矩阵作为无向图的存储结构,邻接表作为有向图的存储结构,完成无向图和有向图的建立,并对建

具体实现要求:
1.通过键盘输入图的顶点和边信息,分别构造一个无向图的邻接矩阵和一个有向图的邻接表。
2.分别对建立好的两个图进行深度和广度优先遍历,输出相应的遍历序列。
3.统计两个图的连通分量的个数。
要求采用邻接矩阵作为无向图的存储结构,邻接表作为有向图的存储结构,完成无向图和有向图的建立,并对建立好的图进行深度和广度优先遍历

第1个回答  2011-05-07
#include"utility.h"
#include"adj_matrix_undir_graph.h"
#include"adj_list_dir_graph.h"
#include"dfs.h"
#include"bfs.h"

int main(void)
{
int n,j=0,i=0;
int m,e,b=0;
char vexs[20],c;
char nums[20];

cout<<"输入无向图的顶点个数n:"<<endl;
cin>>n;
cout<<"输入顶点元素:"<<endl;
for(i=0;i<n;i++)
{
cout<<"请输入第"<<j<<"个结点"<<endl;
cin>>vexs[i];
j++;
}

cout<<"输出无向图的邻接矩阵:"<<endl;

AdjMatrixUndirGraph<char> aundir(vexs,n);
for(i=0;i<n;i++)
{
for(int v=1;v<n;v++)
{
cout<<"输入Y/N,是否插入边:";
cin>>c;
if(c == 'Y' )
aundir.InsertEdge(i,v);
}
}
Display(aundir);

cout<<"请输入有向图的顶点个数m:";
cin>>m;
for(int a=0;a<m;a++)
{
cout<<"输入第"<<b<<"个顶点数据";
cin>>nums[a];
b++;
}

AdjListDirGraph<char> dir(nums,m);

for(int k=0;k<m;k++)
{
for(e=0;e<m;e++)
{
cout<<"是否插入边V"<<k<<",V"<<e<<":";
cin>>c;
if(c == 'Y' )
dir.InsertEdge(k,e);
}
}
Display(dir);

cout<<"无向图的深度遍历:";
DFSTraverse<char>(aundir,Write<char>);
cout<<endl;
cout<<"无向图的广度遍历:";
BFSTraverse<char>(aundir,Write<char>);

cout<<endl;
cout<<"有向图的深度遍历:";
DFSTraverse<char>(dir,Write<char>);
cout<<endl;
cout<<"有向图的广度遍历:";
BFSTraverse<char>(dir,Write<char>);

参考资料:唐宁九版本的软件包

本回答被提问者采纳
第2个回答  2011-05-12
#include <iostream>
using namespace std;
struct Stack
{
int data[100];
int top;
};

typedef struct Stack stack;

void Initial(stack &S);
int Pop(stack &s);
void Push(Stack &s,int e);

void main()
{
stack s;
Initial(s);
Push(s,1);
Push(s,2);
Push(s,3);
cout<<Pop(s)<<endl;
cout<<Pop(s)<<endl;
Push(s,4);
cout<<Pop(s)<<endl;
}

void Initial(stack &s)
{
s.top=-1;
}

void Push(stack &s,int e)
{
s.data[++s.top]=e;
}

int Pop(stack &s)
{
return s.data[s.top--];
}
第3个回答  2011-05-04
这个我以前做过啊,第三个要求没有做
肯定是学校里面的实习内容。。自己写一下啦,锻炼一下
第4个回答  2011-05-04
..............谁会~
第5个回答  2011-05-04
一看题目就知道你是我们班的吧……正在搜竟然搜到这个………………