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,112 @@
template <class DT>
struct QNode //结点
{
DT data; //数据域,存储数据元素值
QNode *next;//指针域,指向下一个结点
};
template<class DT>
struct LinkQueue
{
QNode<DT> * front;
QNode<DT> * rear;
};
//【算法3.19】
template <class DT>
void InitQueue(LinkQueue<DT> &Q)//创建空队列
{
Q.front=new QNode<DT>; //创建头结点
if(!Q.front) exit(1); //创建失败,结束运行
Q.front->next=NULL;
Q.rear=Q.front;
}
//【算法3.20】
template <class DT>
void DestroyQueue(LinkQueue<DT> &Q)//释放链队
{
QNode<DT> *p;
while(Q.front)//从头结点开始,依次释放结点
{
p=Q.front;
Q.front=Q.front->next;
delete p;
}
}
//【算法3.21】 入队
template<class DT>
bool EnQueue(LinkQueue<DT> &Q,DT e)
{
QNode<DT> *p;
p=new QNode<DT>; // 创建新结点
if(!p) return false; // 创建失败,结束运行
p->data=e; // 新结点赋值
p->next=NULL; // 链在队尾
Q.rear->next=p;
Q.rear=p;
return true; // 入队成功返回true
}
//【算法3.22】 出队
template<class DT>
bool DeQueue(LinkQueue<DT> &Q,DT &e)
{
QNode<DT> *p;
if(Q.front==Q.rear) return false; //队空返回false
p=Q.front->next; // 取出队元素
e=p->data;
Q.front->next=p->next; //队首元素出队
if(Q.rear==p) //只有一个元素时出队,
Q.rear=Q.front; // 修改队尾
delete p;
return true; // 出队成功返回true
}
//【算法3.23】 取队头元素
template<class DT>
bool GetHead(LinkQueue<DT> Q,DT &e)
{
if(Q.front==Q.rear) return false; // 队空返回false
e=Q.front->next->data;
return true; // 删除成功返回true
}
//取队尾元素
template<class DT>
bool GetTail(LinkQueue<DT> Q,DT &e)
{
if(Q.front==Q.rear) // 队空
return false; // 返回false
e=Q.rear->data; // 获取队尾元素
return true; // 返回true
}
//测队空
template<class DT>
bool QueueEmpty(LinkQueue<DT> Q)
{
if(Q.front==Q.rear) // 队空
return true; //返回true
else //非空
return false; //返回false
}
//显示队列内容
template<class DT>
void DispQueue(LinkQueue<DT> Q)
{
QNode<DT> *p;
p=Q.front->next;
while(p)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}

View File

@@ -0,0 +1,132 @@
#include<string>
#include "LinkQueue.h"
#include "Mgraph.h"
#include <iostream>
using namespace std;
// 无向图
void DispMenu()
{
cout<<"请选择你要的操作"<<endl;
cout<<" 1. 建立图"<<endl;
cout<<" 2. 返回顶点在图中的位置"<<endl;
cout<<" 3. 返回某位置的顶点的值"<<endl;
cout<<" 4. 修改顶点值"<<endl;
cout<<" 5. 增加新顶点"<<endl;
cout<<" 6. 删除顶点弧"<<endl;
cout<<" 7. 增添边"<<endl;
cout<<" 8. 删除边"<<endl;
cout<<" 9. 从第一个顶点出发深度优先遍历图"<<endl;
cout<<"10. 从第一个顶点广度优先遍历图"<<endl;
cout<<"11. 显示图"<<endl;
cout<<" 0. 退出"<<endl;
}
bool visited[MAX_VEXNUM]={false};
void main()
{
char u,v;
int k;
MGraph<char> G;
int choice;
do
{
DispMenu();
cin>>choice;
switch(choice)
{
case 1:
CreateUDN(G);
cout<<endl;
cout<<"创建的图为:"<<endl;
DispG(G);
break;
case 2:
cout<<"请输入您要的所要查询位置的顶点的名称: ";
cin>>u;
k=LocateVex(G,u);
if(k!=-1)
cout<<"顶点"<<u<<"在图中的位置为: "<<k<<endl;
else
cout<<"顶点"<<u<<"不存在!"<<endl;
cout<<endl;
break;
case 3:
int index;
cout<<"请输入您要的所要查询顶点的位置: ";
cin>>index;
if(GetVex(G,index,v))
cout<<"位置为"<<index<<"的顶点为: "<<v<<endl;
else
cout<<""<<index<<"顶点不存在!"<<endl;
cout<<endl;
break;
case 4:
cout<<"请输入要更改的顶点值: ";
cin>>u;
cout<<"请输入更改后顶点的值: ";
cin>>v;
PutVex(G,u,v);
cout<<"顶点值修后的图为“"<<endl;
DispG(G);
cout<<endl;
break;
case 5:
cout<<"请输入要增加的顶点的值: ";
cin>>v;
InsertVex(G,v);
cout<<"插入顶点和相应边后的图为“"<<endl;
DispG(G);
cout<<endl;
break;
case 6:
cout<<"请输入要删除的顶点的值:";
cin>>v;
DeleteVex(G,v);
cout<<"顶点删除后的图为“"<<endl;
DispG(G);
cout<<endl;
break;
case 7:
cout<<"请输入要增添边的顶相邻两顶点";
cin>>u>>v;
InsertArc(G,u,v);
cout<<"插入边后的图为“"<<endl;
DispG(G);
cout<<endl;
break;
case 8:
cout<<"请输入删除的边相邻两顶点: ";
cin>>u>>v;
DeleteArc(G,u,v);
cout<<"顶点边后的图为“"<<endl;
DispG(G);
cout<<endl;
break;
case 9:
cout<<"从第一个顶点出发深度优先遍历图的序列为: "<<endl;
DFSTraverse(G);
cout<<endl;
break;
case 10:
cout<<"从第一个顶点出发广度优先遍历图的序列为: "<<endl;
BFSTraverse(G);
cout<<endl;
break;
case 11:
DispG(G);
cout<<endl;
break;
case 0:
cout<<"结束运行Bye-Bye!"<<endl;
break;
default:
cout<<"选择不合理,请重选!"<<endl;
}//case
}while(choice!=0);
}//main

View File

@@ -0,0 +1,334 @@
/*--------------------------无向图的邻接矩阵表示-----------------------------*/
#define MAX_VEXNUM 20 // 最大顶点数
template <class DT>
struct MGraph // 图的邻接矩阵表示存储定义
{
DT vexs[MAX_VEXNUM]; // 顶点信息
int arcs[MAX_VEXNUM][MAX_VEXNUM];
int vexnum,arcnum; // 顶点数和边数
};
template <class DT>
void DispG(MGraph<DT> G) // 显示图信息
{
int i,j;
DT u,v;
cout<<G.vexnum<<"个顶点:"<<endl;
for(i=0;i<G.vexnum;i++)
cout<<G.vexs[i]<<" ";
cout<<endl;
cout<<G.arcnum<<"条边信息如下:"<<endl;
for(i=0;i<G.vexnum;i++)
{
for(j=i+1;j<G.vexnum;j++)
if (G.arcs[i][j]!=0)
{
GetVex(G,i,u);
GetVex(G,j,v);
cout<<'('<<u<<","<<v<<")"<<'\t';
}
}
cout<<endl;
}
//算法6.1 顶点定位
template <class DT>
int LocateVex(MGraph<DT> G,DT v)
{
for(int i = 0;i<G.vexnum;i++)
{
if(G.vexs[i] == v)
{
return i;
}
}
return -1;
}
//6.2 创建无向图
template <class DT>
void CreateUDN(MGraph<DT> &G)
{
int i,j,k;
DT v1,v2;
cout<<"请输入无向图的顶点数 "; // 1. 输入顶点数、边数
cin>>G.vexnum ;
cout<<"请输入无向图的边数 ";
cin>>G.arcnum ;
cout<<"请输入"<<G.vexnum<<"个顶点的值"<<endl; // 2. 输入顶点值
for(i = 0;i<G.vexnum;i++)
cin>>G.vexs[i];
for(i=0;i<G.vexnum;i++) // 3.邻接矩阵初始化
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=0;
cout<<"请输入各条边的两个邻接点"<<endl; // 4.创建各条边
for( k=0;k<G.arcnum;k++)
{
cout<<"输入第"<<k<<"条边的两个顶点:"<<endl;
cin>>v1>>v2; // 4.1 输入边的两个邻接点
i = LocateVex(G,v1); // 4.2 定位两个邻接点
j = LocateVex(G,v2);
if(i<0 || j<0 || i==j)
{
cout<<"顶点信息错,重新输入!"<<endl;
k--;
continue;
}
G.arcs[i][j]=1; // 4.3 修改邻接矩阵
G.arcs[j][i]=1;
}
}
template <class DT>
bool GetVex(MGraph<DT> G, int k, DT &v) // 获取第 u 个顶点值v
{
if(k<0 || k>=G.vexnum) // u不存在返回false
return false;
else
{
v=G.vexs[k];
return true;
}
}
template <class DT>
bool PutVex(MGraph<DT> &G,DT &u,DT v) // 为第u个顶点赋值
{
int k;
k=LocateVex(G,u);
if(k<0 ) // u不存在返回false
return false;
else // u存在赋值
{
G.vexs[k] = v;
return true;
}
}
//算法6.3 按值查找第一邻接点
template <class DT>
int FirstAdjVex(MGraph<DT> G,int u)
{
if(u<0 || u>=G.vexnum) // 顶点不存在
return -1; // 无邻接点,返回-1
for(int j=0;j<G.vexnum;j++) // 扫描邻接矩阵第u行
if(G.arcs[u][j]!=0) // 如果有非零元,
return j; // 第一个非零元所在列号,为其邻接点序号
return -1; // 否则,无邻接点,返回-1
}
template <class DT>
int NextAdjVex(MGraph<DT> G,int u,int w) //查找第u个顶点邻接点W的下一个邻接点
{
if(u<0 || u>=G.vexnum || w<0
|| w>=G.vexnum || G.arcs[u][w]==0 ) // 参数不合理
return -1; // 无邻接点
for(int j=w+1;j<G.vexnum;j++) // 扫描邻接矩阵第u行w列后的元素
if(G.arcs[u][j]!=0) // 如果有非零元,
return j; // 第一个非零元所在列号,为其邻接点序号
return -1; // 否则无邻接点,返回-1
}
template <class DT>
bool InsertVex(MGraph<DT> &G,DT v) // 插入值为v的顶点
{
DT w;
int j;
char ans;
if(G.vexnum>=MAX_VEXNUM) // 无存储空间,不能插入
{
cout<<"无存储空间,不能插入!"<<endl;
return false;
}
G.vexs[G.vexnum++]= v; // 顶点信息加入至G.vexs中顶点数增1
for(j=0;j<G.vexnum;j++) // 初始化邻接矩阵最后一行和最后一列值
{
G.arcs[G.vexnum-1][j]=0;
G.arcs[j][G.vexnum-1]=0;
}
cout<<"创建边吗Y/N)?"<<endl;
cin>>ans;
while(ans=='Y'|| ans=='y')
{
cout<<"输入另一个顶点值"<<endl;
cin>>w;
j=LocateVex(G,w);
if(j>=0) // 顶点存在
InsertArc(G,v,w);
else
cout<<w<<"顶点不存在!";
cout<<"继续创建边吗Y/N)?"<<endl;
cin>>ans;
};
return true;
}
template <class DT>
bool InsertArc(MGraph<DT> &G,DT v,DT w) // 在值为v、w顶点间加边
{ int i = LocateVex(G,v);
int j = LocateVex(G,w);
if(i<0 || j<0 || i==j) // 顶点不存在或两端点相同
return false; // 不能插入边返回false
G.arcs[i][j]=1;
G.arcs[j][i]=1;
G.arcnum++;
return true;
}
template <class DT>
bool DeleteArc(MGraph<DT> &G,DT v,DT w) // 按顶点值删除边
{
int i = LocateVex(G,v);
int j = LocateVex(G,w);
if(i<0||j<0||i == j) // 边不存在返回false
{
cout<<"边不存在!"<<endl;
return false;
}
G.arcs[i][j]=0; // 置邻接矩阵第 i 行第 j 列为零
G.arcs[j][i]=0; // 置邻接矩阵第 i 列第 j 行为零
G.arcnum--; // 边数减1
return true;
}
template <class DT>
bool DeleteVex(MGraph<DT> &G,DT v) // 按值删除顶点
{
int i,j;
DT w;
i = LocateVex(G,v); // 顶点定位
if(i<0)
{
cout<<"顶点不存在!"<<endl; // 顶点不存在
return false;
}
for(j=0;j<G.vexnum;j++) // 删除与顶点v相连的边
{
if(G.arcs[i][j]!=0)
{
GetVex(G,j,w);
DeleteArc(G,v,w);
}
}
for(j=i+1;j<G.vexnum;j++) // 排在顶点v后面的顶点前移
{
G.vexs[j-1] = G.vexs[j];
}
G.vexnum--;
return true;
}
// 算法6.10
template <class DT>
void DFS(MGraph<DT> G,int v) // 连通图深度优先遍历
{
int w;
visited[v] = true; // 先访问index
cout<<G.vexs[v];
for(w=0;w<G.vexnum;w++)
{
if(G.arcs[v][w]!=0 && !visited[w] )
DFS(G,w);
}
//cout<<endl;
}
// 算法6.9
template <class DT>
void DFSTraverse(MGraph<DT> G) // 非连通图深度优先遍历
{
int i;
for(i=0;i<G.vexnum;i++)
visited[i]=0;
for( i=0;i<G.vexnum;i++) //对每个顶点进行深度递归
{
if(!visited[i])
DFS(G,i);
}
cout<<endl;
return ;
}
// 算法 6.11 广度优先遍历
template <class DT>
void BFS(MGraph<DT> G,int v)
{
int w;
LinkQueue<int> Q;
InitQueue(Q);
cout<<G.vexs[v];
visited[v]=ture;
EnQueue(Q,v);
while(!QueueEmpty(Q))
{
DeQueue(Q,u);
for(w=Firstadjvex(G,u);w>=0;w=Nextadjvex(G,u,w)))
if(!visited[w])
{
cout<<G.vexs[w];
visited[w]=true;
EnQueue(Q,w);
}
}
}
// 算法6.12
template <class DT>
void BFSTranverse(MGraph<DT> G)
{
int i;
for(i=0;i<G.vexnum;i++)
visited[i]=0;
for(i=0;i<G.vexnum;i++)
{
if(!visited[i])
BFS(G,i);
}
}
template <class DT>
bool BFSTraverse(MGraph<DT> G) // 广度优先遍历
{
int i;
int u,w;
LinkQueue<int> Q;
InitQueue(Q);
for(i = 0;i<G.vexnum;i++) //访问标识向量初始化
visited[i] = false;
for( i=0;i<G.vexnum;i++) //对每个未被访问的顶点进行广度优先遍历
{
if(!visited[i])
{
visited[i] = true;
cout<<G.vexs[i]<<" ";
EnQueue(Q,i);
while(!QueueEmpty(Q))
{
DeQueue(Q,u);
for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
{
if(!visited[w])
{
visited[w] = true;
cout<<G.vexs[w];
EnQueue(Q,w);
}
}
}
}
}
cout<<endl;
return true;
}