编辑代码

#include <iostream>
#include <stack>
using namespace std;
#define MVNum 100
typedef char VerTexType;
bool visited[MVNum];
typedef struct ArcNode
{
    int adjvex;
    struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode
{
    VerTexType data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct
{
    AdjList Vertices;
    int vexnum,arcnum;
}ALGraph;
bool InsertVex(ALGraph &G,char c){
    if(G.vexnum>=MVNum) return false;
    G.Vertices[G.vexnum].data = c;
    G.Vertices[G.vexnum].firstarc = NULL;
    G.vexnum++;
    return true; 
}
bool InsertArc(ALGraph &G,int i,int j){
    ArcNode *p;
    p =new ArcNode;
    p->adjvex = j;
    p->nextarc = G.Vertices[i].firstarc;
    G.Vertices[i].firstarc = p;
    G.arcnum++;
    return true; 
}
void DeleteVex(ALGraph &G,int v){
    ArcNode *p,*d;
    p = G.Vertices[v].firstarc;
    cout<<G.arcnum<<endl;
    while(p!=NULL){
        d = p;
        p=p->nextarc;
        delete d;
        G.arcnum--;
    }
    //G.Vertices[v].firstarc = NULL;
    cout<<G.arcnum<<endl;
    for(int i=v;i<G.vexnum;i++)
        G.Vertices[i] = G.Vertices[i+1];//这里更改了位置,所以指向的顶点错误了。
    G.vexnum--;
    //for(int i=0;i<G.vexnum;i++){
        //p = G.Vertices[i].firstarc;
        //if(p==NULL) continue;
        //if(p->adjvex==v){
        //   d=p;
        //    p = p->nextarc;
            //G.Vertices[i].firstarc = NULL;
            //delete d;
        //    cout<<"删除了"<<i<<endl;
            //G.arcnum--;
        //    continue;
        //}    
   // }
    cout<<G.arcnum<<endl;
}
void DFS(ALGraph G,int v){
    cout<<v;
    visited[v] = true;
    ArcNode *p;
    p = G.Vertices[v].firstarc;
    while(p!=NULL){
        v = p->adjvex;
        if(!visited[v])
            DFS(G,v);
        p = p->nextarc;
    }
}
void DFS2(ALGraph G,int v){
    stack<int> s;
    ArcNode *p;
    visited[v] = true;
    cout<<v;
    s.push(v);
    while(!s.empty()){
        p = G.Vertices[v].firstarc;
        while(p!=NULL){
            v = p->adjvex;
            if(!visited[v])
            {
                visited[v] = true;
                s.push(v);
                cout<<v;
                p = G.Vertices[v].firstarc;
            }else
                p = p->nextarc;
        }
        v = s.top();
        s.pop();
    }
}
void DFS3(ALGraph G,int v){
    stack<int> s;
    ArcNode *p;
    s.push(v);
    while(!s.empty()){
        v = s.top();
        s.pop();
        visited[v] = true;
        cout<<v;
        p = G.Vertices[v].firstarc;
        while(p!=NULL)
        {
            v = p->adjvex;
            if(!visited[v])
                s.push(v);
            p = p->nextarc;
        }
    }
}
void cha(ALGraph &G) {
    G.vexnum = 0;
    G.arcnum = 0;
    InsertVex(G,0);
    InsertVex(G,1);
    InsertVex(G,2);
    InsertVex(G,3);
    InsertVex(G,4);
    InsertVex(G,5);
    InsertArc(G,0,2);
    InsertArc(G,2,1);
    InsertArc(G,2,5);
    InsertArc(G,3,2);
    InsertArc(G,1,4);
    InsertArc(G,5,3);
}
int main(){
    ALGraph G;
    ArcNode *p;
    cha(G);
    DeleteVex(G,3);
    //cout<<G.Vertices[0].firstarc->adjvex;
    for(int i=0;i<G.vexnum;i++){
        visited[i] = false;
    }
    DFS(G,0);
    for(int i=0;i<G.vexnum;i++){
        cout<<i;
        p = G.Vertices[i].firstarc;
        while(p!=NULL){
            cout<<p->adjvex;
            p = p->nextarc;
        }
        cout<<endl;
    }
	return 0;
}