注:请注意对应题号,以下内容仅为个人代码,不代表最终答案,仅供参考。
2024.9.26更新:题目DC08PE17的BUG已经修复,现已添加代码
目录
DC08PE15在图G中,相对于k顶点的当前邻接顶点m,求下一个邻接顶点
DC08PE21计算以邻接表方式存储的有向图G中某顶点的出度
DC08PE22计算以邻接表方式存储的有向图G中某顶点的入度
DC08PE12创建有向图的邻接数组存储结构
Status CreateDG(MGraph &G, VexType *vexs, int n,
ArcInfo *arcs, int e)
{ // Add your code here
if(n<0 or !vexs or !InitGraph(G, DG, n)) return 0;
G.e = e; G.n = n;
for(int i=0; i<G.n; i++) G.vexs[i] = vexs[i];
for(int i=0; i<G.e; i++) G.arcs[LocateVex(G, arcs[i].v)][LocateVex(G, arcs[i].w)].adj = 1;
return 1;
}
DC08PE15在图G中,相对于k顶点的当前邻接顶点m,求下一个邻接顶点
int NextAdjVex(MGraph G, int k, int m)
{ // Add your code here
if(G.n == 0) return 0;
if(k < 0 or k > G.n or G.e < 2) return -1;
for(int i=m+1; i<G.n; i++) {
if(G.kind == UDG or G.kind == DG and G.arcs[k][i].adj == 1 and i != k) return i;
if(G.arcs[k][i].adj != INFINITY and i != k) return i;
}
return -1;
}
DC08PE17在图G中设置顶点v到顶点w的弧或边
Status SetArc(MGraph &G, VexType v, VexType w, ArcCell info)
{ // Add your code here
if(!v or !w) return -1;
G.arcs[LocateVex(G,v)][LocateVex(G,w)] = info;
G.e++;
return 1;
}
DC08PE21计算以邻接表方式存储的有向图G中某顶点的出度
int outDegree(ALGraph G, int k)
{ // Add your code here
if(k < 0 or k > G.n) return -1;
int count = 0;
AdjVexNode *node = G.vexs[k].firstArc;
while(node){
count++;
node = node->next;
}
return count;
}
DC08PE22计算以邻接表方式存储的有向图G中某顶点的入度
int inDegree(ALGraph G, int k)
{ // Add your code here
if(k<0 or k>G.n) return -1;
int count = 0;
for(int i=0; i<G.n; i++) {
if(i == k) continue;
AdjVexNode *node = G.vexs[i].firstArc;
while(node){
if(node->adjvex == k) {
count++;
break;
}
node = node->next;
}
}
return count;
}
DC08PE32创建有向图的邻接表存储结构
Status CreateDG(ALGraph &G, VexType *vexs, int n,
ArcInfo *arcs, int e)
{ // Add your code here
if(n<0 or !vexs) return -1;
G.n = n; G.e = e;
G.kind = DG;
for(int i=0; i<n; i++) {
G.vexs[i].data = vexs[i];
G.vexs[i].firstArc = NULL;
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(G.vexs[i].data == arcs[j].v) {
AdjVexNode *node = G.vexs[i].firstArc;
if(!node) {
node = (AdjVexNode *)malloc(sizeof(AdjVexNode));
node->adjvex = arcs[j].w;
node->next = NULL;
}
else{
while(node->next) node = node->next;
AdjVexNode *w = (AdjVexNode *)malloc(sizeof(AdjVexNode));
w->adjvex = arcs[j].w;
w->next = NULL;
node->next = w;
}
}
}
}
return 1;
}
DC08PE34创建无向图的邻接表存储结构
Status CreateUDG(ALGraph &G, VexType *vexs, int n,
ArcInfo *arcs, int e)
{ // Add your code here
if(n<0 or !vexs) return -1;
G.n = n; G.e = e;
G.kind = UDG;
for(int i=0; i<n; i++) {
G.vexs[i].data = vexs[i];
G.vexs[i].firstArc = NULL;
}
for(int i=0; i<n; i++) {
for(int j=0; j<e; j++) {
if(G.vexs[i].data == arcs[j].v){
if(!G.vexs[i].firstArc) {
AdjVexNode *node = (AdjVexNode *)malloc(sizeof(AdjVexNode));
node->adjvex = arcs[j].w;
node->next = NULL;
}
else {
AdjVexNode *node = G.vexs[i].firstArc;
while(node->next) node = node->next;
AdjVexNode *w = (AdjVexNode *)malloc(sizeof(AdjVexNode));
w->adjvex = arcs[j].w;
w->next = NULL;
node->next = w;
}
}
if(G.vexs[i].data == arcs[j].w){
if(!G.vexs[i].firstArc) {
AdjVexNode *node = (AdjVexNode *)malloc(sizeof(AdjVexNode));
node->adjvex = arcs[j].v;
node->next = NULL;
}
else {
AdjVexNode *node = G.vexs[i].firstArc;
while(node->next) node = node->next;
AdjVexNode *w = (AdjVexNode *)malloc(sizeof(AdjVexNode));
w->adjvex = arcs[j].v;
w->next = NULL;
node->next = w;
}
}
}
}
return 1;
}