idk why these stuffs get stashed for so long and I didn't ever commit them

This commit is contained in:
2025-11-06 09:43:54 +08:00
parent 5dbdfef1a1
commit e01c041259
232 changed files with 22806 additions and 1256 deletions

View File

@@ -0,0 +1,132 @@
//#include<string>
#include <iostream>
#include "ALGraph.h"
#include "LinkQueue.h"
using namespace std;
void DispMenu()
{
cout<<"\n请选择你要的操作"<<endl;
cout<<" 1. 建立无向图"<<endl;
cout<<" 2. 图的连通信息"<<endl;
cout<<" 3. 求顶点v最远点"<<endl;
cout<<" 4. 显示图"<<endl;
cout<<" 0. 退出"<<endl;
}
//算法6.16
template<class DT>
void DFS3(ALGraph<DT> G,int v) // 连接表存储的图的深度优先遍历
{
int w;
ArcNode* p;
visited[v]=true; // 做访问标志
cout<<G.vertices[v].data; // 输出顶点
p=G.vertices[v].firstarc; // 找第一未访问邻接点
while(p) // 顶点非空
{
w=p->adjvex; // 未被访问过
if(!visited[w])
DFS3(G,w); // DFS遍历
p=p->nextarc; // 新启点DFS遍历
}
}
template<class DT>
void ConnectVex(ALGraph<DT> G) // 图的连通信息
{
int k,num=0; // num连通分量计数
for(k=0;k<G.vexnum;k++) // 访问标志初始化
visited[k]=false;
for(k=0;k<G.vexnum;k++) // 连通图遍历调用次数为连通分量数
if(!visited[k])
{
num++;
cout<<"\n"<<num<<"个连通分量序列为:";
DFS3(G,k);
}
}
// 算法6.17 求距离最远顶点
template<class DT>
int Maxdist( ALGraph<DT> G, int v)
{
int i,w;
ArcNode *p;
LinkQueue<int> Q; // 创建一个队列
InitQueue(Q);
for(i=0;i<G.vexnum;i++) // 访问标志初始化
visited[i]=false;
EnQueue(Q,v); // v入队
visited[v]=true; // 做访问标志
while(!QueueEmpty(Q)) // 队不空
{
DeQueue(Q,v); // 出队
p=G.vertices[v].firstarc; // 遍历 v 的邻接点
while(p)
{
w=p->adjvex;
if(!visited[w]) // 未被访问
{
visited[w]=true; // 做访问标志
EnQueue(Q,w); // 入队
}
p=p->nextarc; // 下一个邻接点
}
}
return v;
}
bool visited[MAX_VEXNUM]={false};
void main()
{
char u,v;
int k;
ALGraph<char> G;
int choice;
do
{
DispMenu();
cin>>choice;
switch(choice)
{
case 1: // 创建无向图
CreateUDG(G);
cout<<endl;
cout<<"创建的图为:"<<endl;
DispG(G);
break;
case 2: // 图的连通信息
ConnectVex(G);
cout<<endl;
break;
case 3: // 求距离最远顶点
cout<<"\n输入顶点信息v:"<<endl;
cin>>v;
k=LocateVex(G,v);
if(k==-1)
cout<<"\n顶点"<<v<<"不存在!"<<endl;
else
{
k=Maxdist(G, k);
GetVex(G,k,u);
cout<<""<<v<<"的最远点为"<<u<<endl;
}
break;
case 4: // 显示图
DispG(G);
cout<<endl;
break;
case 0:
cout<<"\n结束运行Bye-Bye!"<<endl;
break;
default:
cout<<"\n无效选择,请重选!"<<endl;
}
}while(choice!=0);
}