第6章课后习题算法设计题(严蔚敏数据结构c语言版第2版)

6-1 分别以邻接矩阵和邻接表作为存储结构,实现以下图的基本操作:
①增加一个新顶点v,InsertVex(G,v)
②删除顶点v及其相关的边,DeleteVex(G,v)
③增加一条边<v,w>,InsertArc(G,v,w)
④删除一条边<v,w>,DeleteArc(G,v,w)
(1)增加一个新顶点v,InsertVex(G,v)

#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
typedef struct{
    char vexs[MVNum];
    int arcs[MVNum][MVNum];
    int vexnum,arcnum;
}AMGraph;
typedef struct ArcNode{
    int adjvex;
    struct ArcNode * nextarc;
}ArcNode;
typedef struct VNode{
    char data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;
void CreateUDN(AMGraph &G);
int LocateVex(AMGraph G,char v);
void CreateUDG(ALGraph &G);
int LocateVex(ALGraph G,char v);
void InsertVex(AMGraph &G,char v);
void InsertVex(ALGraph &G,char v);
int main(){
    AMGraph G1;
    ALGraph G2;
    char v;
    CreateUDN(G1);
//    InsertVex(G1,v);
    CreateUDG(G2);
    InsertVex(G2,v);
    return 0;
}
void CreateUDN(AMGraph &G){
    cout<<"-----邻接矩阵创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vexs[i];
    }
    for(int i=0;i<G.vexnum;i++){
        for(int j=0;j<G.vexnum;j++){
            G.arcs[i][j]=MaxInt;
        }
    }
    char v1,v2;
    int w,i,j;
    cout<<"输入一条边的2个顶点和权值:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2>>w;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        G.arcs[i][j]=w;
        G.arcs[j][i]=G.arcs[i][j];
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接矩阵为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cout<<"[";
        for(int j=0;j<G.vexnum;j++){
            if(G.arcs[i][j]==32767)
                cout<<"* ";
            else
                cout<<G.arcs[i][j]<<" ";
        }
        cout<<"]"<<endl;
    }
}
int LocateVex(AMGraph G,char v){
    for(int i=0;i<G.vexnum;i++){
        if(v==G.vexs[i]){
            return i;
        }
    }
}
void CreateUDG(ALGraph &G){
    cout<<"-----邻接表创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }
    char v1,v2;
    int i,j;
    ArcNode *p1,*p2;
    cout<<"输入一条边的2个顶点:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        p1=new ArcNode;
        p1->adjvex=j;
        p1->nextarc=G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G.vertices[j].firstarc;
        G.vertices[j].firstarc=p2;
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接表为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i<<"->";
        while(p!=NULL){
            cout<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}
int LocateVex(ALGraph G,char v){
     for(int i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
}
void InsertVex(AMGraph &G,char v){
    cout<<"-----增加一个顶点-----"<<endl;
    cout<<"此时有"<<G.vexnum<<"个顶点"<<endl;
    cout<<"输入顶点数据:"<<endl;
    G.vexnum++;
    cin>>G.vexs[G.vexnum-1];
    cout<<"增加成功"<<endl;
    cout<<"此时还剩"<<G.vexnum<<"个顶点"<<endl;
}
void InsertVex(ALGraph &G,char v){
    cout<<"-----增加一个顶点-----"<<endl;
    cout<<"此时有"<<G.vexnum<<"个顶点"<<endl;
    int data;
    cout<<"输入顶点数据:"<<endl;
    cin>>data;
    G.vexnum++;
    G.vertices[G.vexnum-1].firstarc=NULL;
    G.vertices[G.vexnum-1].data=data;
    cout<<"增加成功"<<endl;
    cout<<"此时还剩"<<G.vexnum<<"个顶点"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i;
        while(p!=NULL){
            cout<<"->"<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}


(2)删除顶点v及其相关的边,DeleteVex(G,v)

#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
typedef struct{
    char vexs[MVNum];
    int arcs[MVNum][MVNum];
    int vexnum,arcnum;
}AMGraph;
typedef struct ArcNode{
    int adjvex;
    struct ArcNode * nextarc;
}ArcNode;
typedef struct VNode{
    char data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;
void CreateUDN(AMGraph &G);
int LocateVex(AMGraph G,char v);
void CreateUDG(ALGraph &G);
int LocateVex(ALGraph G,char v);
void DeleteVex(AMGraph &G,char v);
void DeleteVex(ALGraph &G,char v);
int main(){
    AMGraph G1;
    ALGraph G2;
    char v;
    CreateUDN(G1);
    //DeleteVex(G1,v);
    CreateUDG(G2);
    DeleteVex(G2,v);
    return 0;
}
void CreateUDN(AMGraph &G){
    cout<<"-----邻接矩阵创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vexs[i];
    }
    for(int i=0;i<G.vexnum;i++){
        for(int j=0;j<G.vexnum;j++){
            G.arcs[i][j]=MaxInt;
        }
    }
    char v1,v2;
    int w,i,j;
    cout<<"输入一条边的2个顶点和权值:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2>>w;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        G.arcs[i][j]=w;
        G.arcs[j][i]=G.arcs[i][j];
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接矩阵为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cout<<"[";
        for(int j=0;j<G.vexnum;j++){
            if(G.arcs[i][j]==32767)
                cout<<"* ";
            else
                cout<<G.arcs[i][j]<<" ";
        }
        cout<<"]"<<endl;
    }
}
int LocateVex(AMGraph G,char v){
    for(int i=0;i<G.vexnum;i++){
        if(v==G.vexs[i]){
            return i;
        }
    }
}
void CreateUDG(ALGraph &G){
    cout<<"-----邻接表创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }
    char v1,v2;
    int i,j;
    ArcNode *p1,*p2;
    cout<<"输入一条边的2个顶点:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        p1=new ArcNode;
        p1->adjvex=j;
        p1->nextarc=G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G.vertices[j].firstarc;
        G.vertices[j].firstarc=p2;
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接表为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i<<"->";
        while(p!=NULL){
            cout<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}
int LocateVex(ALGraph G,char v){
     for(int i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
}
void DeleteVex(AMGraph &G,char v){
    cout<<"-----删除一个顶点和相关的边-----"<<endl;
    cout<<"此时有"<<G.vexnum<<"个顶点"<<endl;
    cout<<"输入顶点数据:"<<endl;
    cin>>v;
    int temp;
    temp=LocateVex(G,v);
    if(temp!=G.vexnum-1){
        for(int i=0;i<G.vexnum-temp-1;i++){
            G.vexs[i+temp]=G.vexs[i+temp+1];
        }
        for(int i=0;i<G.vexnum;i++){
            for(int j=0;j<G.vexnum;j++){
                if(i==temp){
                G.arcs[i][j]=G.arcs[i+1][j];
            }else if(j==temp){
                G.arcs[i][j]=G.arcs[i][j+1];
            }
        }
        }
    }
    G.vexnum--;
    cout<<"删除成功"<<endl;
    cout<<"此时还剩"<<G.vexnum<<"个顶点"<<endl;
}
void DeleteVex(ALGraph &G,char v){
    cout<<"-----删除一个顶点和相关的边-----"<<endl;
    cout<<"此时有"<<G.vexnum<<"个顶点"<<endl;
    cout<<"输入顶点数据:"<<endl;
    cin>>v;
    int temp;
    temp=LocateVex(G,v);
    ArcNode *p,*q;
    for(int i=0;i<G.vexnum;i++){
        //删除其他顶点与该顶点有关的边
        if(i!=temp){
            p=G.vertices[i].firstarc;
            q=p;
            while(p && p->adjvex!=temp){
                q=p;
                p=p->nextarc;
            }
            if(p && q==p){
                G.vertices[i].firstarc=p->nextarc;
            }else if(p){
                q->nextarc=p->nextarc;
            }
        }else{
        //删除顶点所在的边表行
            p=G.vertices[temp].firstarc;
            while(p){
                q=p->nextarc;
                delete p;
                p=q;
            }
        }
    }
    //删除顶点
    for(int i=temp;i<G.vexnum-1;i++){
        G.vertices[i]=G.vertices[i+1];
    }
    //改变邻接表其余顶点的下标
    for(int i=0;i<G.vexnum;i++){
        p=G.vertices[i].firstarc;
        while(p){
            if(p->adjvex>temp){
                p->adjvex--;
            }
            p=p->nextarc;
        }
    }
    G.vexnum--;
    cout<<"删除成功"<<endl;
    cout<<"此时还剩"<<G.vexnum<<"个顶点"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i;
        while(p!=NULL){
            cout<<"->"<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}

(3)增加一条边<v,w>,InsertArc(G,v,w)

#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
typedef struct{
    char vexs[MVNum];
    int arcs[MVNum][MVNum];
    int vexnum,arcnum;
}AMGraph;
typedef struct ArcNode{
    int adjvex;
    struct ArcNode * nextarc;
}ArcNode;
typedef struct VNode{
    char data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;
void CreateUDN(AMGraph &G);
int LocateVex(AMGraph G,char v);
void CreateUDG(ALGraph &G);
int LocateVex(ALGraph G,char v);
void InsertArc(AMGraph &G,char v,char w);
void InsertArc(ALGraph &G,char v,char w);
int main(){
    AMGraph G1;
    ALGraph G2;
    char v;
    char w;
    CreateUDN(G1);
    //InsertArc(G1,v,w);
    CreateUDG(G2);
    InsertArc(G2,v,w);
    return 0;
}
void CreateUDN(AMGraph &G){
    cout<<"-----邻接矩阵创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vexs[i];
    }
    for(int i=0;i<G.vexnum;i++){
        for(int j=0;j<G.vexnum;j++){
            G.arcs[i][j]=MaxInt;
        }
    }
    char v1,v2;
    int w,i,j;
    cout<<"输入一条边的2个顶点和权值:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2>>w;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        G.arcs[i][j]=w;
        G.arcs[j][i]=G.arcs[i][j];
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接矩阵为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cout<<"[";
        for(int j=0;j<G.vexnum;j++){
            if(G.arcs[i][j]==32767)
                cout<<"* ";
            else
                cout<<G.arcs[i][j]<<" ";
        }
        cout<<"]"<<endl;
    }
}
int LocateVex(AMGraph G,char v){
    for(int i=0;i<G.vexnum;i++){
        if(v==G.vexs[i]){
            return i;
        }
    }
}
void CreateUDG(ALGraph &G){
    cout<<"-----邻接表创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }
    char v1,v2;
    int i,j;
    ArcNode *p1,*p2;
    cout<<"输入一条边的2个顶点:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        p1=new ArcNode;
        p1->adjvex=j;
        p1->nextarc=G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G.vertices[j].firstarc;
        G.vertices[j].firstarc=p2;
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接表为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i<<"->";
        while(p!=NULL){
            cout<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}
int LocateVex(ALGraph G,char v){
     for(int i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
}
void InsertArc(AMGraph &G,char v,char w){
    cout<<"-----增加一条边-----"<<endl;
    cout<<"此时有"<<G.arcnum<<"条边"<<endl;
    cout<<"输入两个顶点数据:"<<endl;
    cin>>v>>w;
    int i,j,value;
    i=LocateVex(G,v);
    j=LocateVex(G,w);
    cout<<"输入边的权值:"<<endl;
    cin>>value;
    G.arcs[i][j]=value;
    G.arcs[j][i]=value;
    G.arcnum++;
    cout<<"增加成功"<<endl;
    cout<<"此时还剩"<<G.arcnum<<"条边"<<endl;
}
void InsertArc(ALGraph &G,char v,char w){
    cout<<"-----增加一条边-----"<<endl;
    cout<<"此时有"<<G.arcnum<<"条边"<<endl;
    cout<<"输入两个顶点数据:"<<endl;
    cin>>v>>w;
    int i,j;
    ArcNode *p1,*p2;
    i=LocateVex(G,v);
    j=LocateVex(G,w);
    p1=new ArcNode;
    p1->adjvex=j;
    p1->nextarc=G.vertices[i].firstarc;
    G.vertices[i].firstarc=p1;
    p2=new ArcNode;
    p2->adjvex=i;
    p2->nextarc=G.vertices[j].firstarc;
    G.vertices[j].firstarc=p2;
    G.arcnum++;
    cout<<"增加成功"<<endl;
    cout<<"此时还剩"<<G.arcnum<<"条边"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i;
        while(p!=NULL){
            cout<<"->"<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}

(4)删除一条边<v,w>,DeleteArc(G,v,w)

#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
typedef struct{
    char vexs[MVNum];
    int arcs[MVNum][MVNum];
    int vexnum,arcnum;
}AMGraph;
typedef struct ArcNode{
    int adjvex;
    struct ArcNode * nextarc;
}ArcNode;
typedef struct VNode{
    char data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;
void CreateUDN(AMGraph &G);
int LocateVex(AMGraph G,char v);
void CreateUDG(ALGraph &G);
int LocateVex(ALGraph G,char v);
void DeleteArc(AMGraph &G,char v,char w);
void DeleteArc(ALGraph &G,char v,char w);
int main(){
    AMGraph G1;
    ALGraph G2;
    char v;
    char w;
    CreateUDN(G1);
    //DeleteArc(G1,v,w);
    CreateUDG(G2);
    DeleteArc(G2,v,w);
    return 0;
}
void CreateUDN(AMGraph &G){
    cout<<"-----邻接矩阵创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vexs[i];
    }
    for(int i=0;i<G.vexnum;i++){
        for(int j=0;j<G.vexnum;j++){
            G.arcs[i][j]=MaxInt;
        }
    }
    char v1,v2;
    int w,i,j;
    cout<<"输入一条边的2个顶点和权值:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2>>w;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        G.arcs[i][j]=w;
        G.arcs[j][i]=G.arcs[i][j];
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接矩阵为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cout<<"[";
        for(int j=0;j<G.vexnum;j++){
            if(G.arcs[i][j]==32767)
                cout<<"* ";
            else
                cout<<G.arcs[i][j]<<" ";
        }
        cout<<"]"<<endl;
    }
}
int LocateVex(AMGraph G,char v){
    for(int i=0;i<G.vexnum;i++){
        if(v==G.vexs[i]){
            return i;
        }
    }
}
void CreateUDG(ALGraph &G){
    cout<<"-----邻接表创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }
    char v1,v2;
    int i,j;
    ArcNode *p1,*p2;
    cout<<"输入一条边的2个顶点:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        p1=new ArcNode;
        p1->adjvex=j;
        p1->nextarc=G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G.vertices[j].firstarc;
        G.vertices[j].firstarc=p2;
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接表为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i<<"->";
        while(p!=NULL){
            cout<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}
int LocateVex(ALGraph G,char v){
     for(int i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
}
void DeleteArc(AMGraph &G,char v,char w){
    cout<<"-----删除一条边-----"<<endl;
    cout<<"此时有"<<G.arcnum<<"条边"<<endl;
    cout<<"输入顶点数据:"<<endl;
    cin>>v>>w;
    int i,j,value;
    i=LocateVex(G,v);
    j=LocateVex(G,w);
    G.arcs[i][j]=0;
    G.arcs[j][i]=0;
    G.arcnum--;
    cout<<"删除成功"<<endl;
    cout<<"此时还剩"<<G.arcnum<<"条边"<<endl;
}
void DeleteArc(ALGraph &G,char v,char w){
    cout<<"-----删除一条边-----"<<endl;
    cout<<"此时有"<<G.arcnum<<"条边"<<endl;
    cout<<"输入顶点数据:"<<endl;
    cin>>v>>w;
    int i,j,value;
    i=LocateVex(G,v);
    j=LocateVex(G,w);
    ArcNode *p=G.vertices[i].firstarc;
    ArcNode *q;
    q=p;
    while(p && p->adjvex!=j){
        q=p;
        p=p->nextarc;
    }
    if(p && q==p){
        G.vertices[i].firstarc=p->nextarc;
    }else if(p){
        q->nextarc=p->nextarc;
    }
    p=G.vertices[j].firstarc;
    q=p;
    while(p && p->adjvex!=i){
        q=p;
        p=p->nextarc;
    }
    if(p && q==p){
        G.vertices[j].firstarc=p->nextarc;
    }else if(p){
        q->nextarc=p->nextarc;
    }
    G.arcnum--;
    cout<<"删除成功"<<endl;
    cout<<"此时还剩"<<G.arcnum<<"条边"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i;
        while(p!=NULL){
            cout<<"->"<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}

6-2 一个连通图采用邻接表作为存储结构,设计一个算法,实现从顶点v出发的深度优先遍历的非递归过程。

#include <iostream>
using namespace std;
#define MVNum 100
typedef struct ArcNode{
    int adjvex;
    struct ArcNode * nextarc;
}ArcNode;
typedef struct VNode{
    char data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;
void CreateUDG(ALGraph &G);
int LocateVex(ALGraph G,char v);
void DFS(ALGraph G,int v);
int main(){
    ALGraph G;
    int v;
    CreateUDG(G);
    cout<<"请输出要从第几个顶点开始深度优先遍历:"<<endl;
    cin>>v;
    DFS(G,v);
    return 0;
}
void CreateUDG(ALGraph &G){
    cout<<"-----邻接表创建无向网-----"<<endl;
    cout<<"请输入顶点个数和边数:"<<endl;
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入顶点数据:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        cin>>G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }
    char v1,v2;
    int i,j;
    ArcNode *p1,*p2;
    cout<<"输入一条边的2个顶点:"<<endl;
    for(int k=0;k<G.arcnum;k++){
        cin>>v1>>v2;
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        p1=new ArcNode;
        p1->adjvex=j;
        p1->nextarc=G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G.vertices[j].firstarc;
        G.vertices[j].firstarc=p2;
    }
    cout<<"创建成功"<<endl;
    cout<<"创建的邻接表为:"<<endl;
    for(int i=0;i<G.vexnum;i++){
        ArcNode *p=G.vertices[i].firstarc;
        cout<<i<<"->";
        while(p!=NULL){
            cout<<p->adjvex<<"->";
            p=p->nextarc;
        }
        cout<<endl;
    }
}
int LocateVex(ALGraph G,char v){
     for(int i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
}
void DFS(ALGraph G,int v){
    cout<<"-----邻接表存储图的非递归深度优先遍历-----"<<endl;
    //初始化访问标志数组,0为未访问
    int visited[G.vexnum] = {0};
    ArcNode *p,*q;
    visited[v] = 1;
    cout<<v<<" ";
    p = G.vertices[v].firstarc;
    while (p != NULL){
        int temp=p->adjvex;
        while(visited[temp] == 0){
            visited[temp]=1;
            cout<<temp<<" ";
            q=G.vertices[temp].firstarc;
            temp=q->adjvex;
        }
        p=p->nextarc;
    }
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页