实验题1:实现图的邻接矩阵和邻接表的存储:编写一个程序,设计带权图的邻接矩阵与邻接表的创建和输出运算,并在此基础上设计一个主程序完成以下功能:
(1) 建立有向图G的邻接矩阵,并输出。
(2) 建立有向图G的邻接表,并输出。
(3) 销毁图G的邻接表。
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#define INF 32767
#define MAXV 100
using namespace std;
typedef char InfoType;
typedef struct
{
int no;
InfoType info;
}VertexType;
typedef struct
{
int edges[MAXV][MAXV];
int n,e;
VertexType vexs[MAXV];
}MatGraph;
typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
int weight;
}ArcNode;
typedef struct Vnode
{
InfoType info;
int count;
ArcNode *firstarc;
}Vnode;
typedef struct
{
Vnode adjlist[MAXV];
int n,e;
}AdjGraph;
void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e)
{
int i,j;
g.n=n;g.e=e;
for(i=0;i<g.n;i++)
{
for(j=0;j<g.n;j++)
{
g.edges[i][j]=A[i][j];
}
}
}
void DispMat(MatGraph g)
{
int i,j;
for(i=0;i<g.n;i++)
{
for(j=0;j<g.n;j++)
{
if(g.edges[i][j]!=INF)
cout<<" "<<g.edges[i][j]<<" ";
else
cout<<" "<<"…";
}
cout<<endl;
}
}
void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)
{
int i,j;
ArcNode *p;
G=(AdjGraph*)malloc(sizeof(AdjGraph));
for(i=0;i<n;i++)
G->adjlist[i].firstarc=NULL;
for(i=0;i<n;i++)
for(j=n-1;j>=0;j--)
if(A[i][j]!=0&&A[i][j]!=INF)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->weight=A[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=n;G->e=n;
}
void DispAdj(AdjGraph *G)
{
ArcNode *p;
for(int i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
cout<<i<<":";
while(p!=NULL)
{
cout<<p->adjvex<<"["<<p->weight<<"]->";
p=p->nextarc;
}
cout<<"^"<<endl;
}
}
void DestroyAdj(AdjGraph *&G)
{
ArcNode *pre,*p;
for(int i=0;i<G->n;i++)
{
pre=G->adjlist[i].firstarc;
if(pre!=NULL)
{
p=pre->nextarc;
while(p!=NULL)
{
free(pre);
pre=p;p-p->nextarc;
}
free(pre);
}
}
free(G);
}
int main()
{
MatGraph g;
AdjGraph *G;
int A[MAXV][MAXV]=
{
{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,INF,9},
{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}
};
int n=6,e=10;
CreateMat(g,A,n,e);
cout<<"邻接矩阵为:"<<endl;
DispMat(g);
CreateAdj(G,A,n,e);
cout<<"邻接表为:"<<endl;
DispAdj(G);
DestroyAdj(G);
return 0;
}
实验题2:实现图的遍历算法:编写一个程序实现图的两种遍历运算,并在此基础上设计一个程序完成以下功能:
(1) 输出有向图G从定点0开始的深度优先遍历序列(递归算法)。
(2) 输出有向图G从定点0开始的深度优先遍历序列(非递归算法)。
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#define INF 32767
#define MAXV 100
using namespace std;
typedef char InfoType;
typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
int weight;
}ArcNode;
typedef struct Vnode
{
InfoType info;
int count;
ArcNode *firstarc;
}Vnode;
typedef struct
{
Vnode adjlist[MAXV];
int n,e;
}AdjGraph;
void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)
{
int i,j;
ArcNode *p;
G=(AdjGraph*)malloc(sizeof(AdjGraph));
for(i=0;i<n;i++)
G->adjlist[i].firstarc=NULL;
for(i=0;i<n;i++)
for(j=n-1;j>=0;j--)
if(A[i][j]!=0&&A[i][j]!=INF)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->weight=A[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=n;G->e=n;
}
void DispAdj(AdjGraph *G)
{
ArcNode *p;
for(int i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
cout<<i<<":";
while(p!=NULL)
{
cout<<p->adjvex<<"["<<p->weight<<"]->";
p=p->nextarc;
}
cout<<"^"<<endl;
}
}
void DestroyAdj(AdjGraph *&G)
{
ArcNode *pre,*p;
for(int i=0;i<G->n;i++)
{
pre=G->adjlist[i].firstarc;
if(pre!=NULL)
{
p=pre->nextarc;
while(p!=NULL)
{
free(pre);
pre=p;p-p->nextarc;
}
free(pre);
}
}
free(G);
}
int visited[MAXV];
void DFS(AdjGraph *G,int v)
{
ArcNode *p;
cout<<v<<" ";
visited[v]=1;
p=G->adjlist[v].firstarc;
while(p!=NULL)
{
if(visited[p->adjvex]==0)
DFS(G,p->adjvex);
p=p->nextarc;
}
}
void DFS1(AdjGraph *G,int v)
{
ArcNode *p;
int st[MAXV];
int top=-1,w,x,i;
for(i=0;i<G->n;i++)
visited[i]=0;
cout<<v<<" ";
visited[v]=1;
top++;st[top]=v;
while(top>-1)
{
x=st[top];
p=G->adjlist[x].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if(visited[w]==0)
{
cout<<w<<" ";
visited[w]=1;
top++;
st[top]=w;
break;
}
p=p->nextarc;
}
if(p==NULL) top--;
}
cout<<endl;
}
int main()
{
AdjGraph *G;
int A[MAXV][MAXV]=
{
{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,INF,9},
{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}
};
int n=6,e=10;
CreateAdj(G,A,n,e);
cout<<"邻接表为:"<<endl;
DispAdj(G);
cout<<"深度优先遍历(递归算法):"<<endl;
DFS(G,0);cout<<endl;
cout<<"深度优先遍历(非递归算法):"<<endl;
DFS1(G,0);
DestroyAdj(G);
return 0;
}
仅作留档。