头歌:图的邻接表存储及操作答案

第1关:实现图的邻接表存储

#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stack>
#include <string.h>
#define MAX_VEX 30
using namespace std;
typedef int Status;
typedef int VertexType;
typedef struct ArcNode{
    
    //表节点 
	int num;//弧指向顶点的下标
    ArcNode *next; 
}ArcNode;
typedef struct VNode{
    
    //顶点节点 
	VertexType data; //顶点信息 
	ArcNode *firstarc;//指向第一个表节点 
}VNode,AdjList[MAX_VEX];
typedef struct ALGraph{
    
    //有向图 
	AdjList vertices;
	int vexnum;//顶点数
	int arcnum;//弧数 
}ALGraph;
void Init_ALG(ALGraph *m,int n){
    
    
	for(int i=0;i<n;i++){
    
    
		scanf("%d",&m->vertices[i].data);
		m->vertices[i].firstarc=NULL;
	}
}
void Creat_ALG(ALGraph *m){
    
    
	VertexType a,b;//起点a,终点b 
	for(int i=0;i<m->arcnum;i++){
    
    
		//printf("请输入起点和终点:\n");
		scanf("%d%d",&a,&b);
		/************begin************/ 
        int x,y;
        for(int j=0;j<m->vexnum;j++){
    
    
            if(a==m->vertices[j].data){
    
    
                x=j;
            }
            if(b==m->vertices[j].data){
    
    
                y=j;
            }
        }
        ArcNode *p=new ArcNode;
        p->num=y;
        ArcNode *t=m->vertices[x].firstarc;
        if(!t){
    
    
            p->next=t;
            m->vertices[x].firstarc=p;
        }else{
    
    
            while(t->next!=NULL){
    
    
                t=t->next;
            }
            p->next=t->next;
            t->next=p;
        }
        /************end************/ 
    } 
}
void Print(ALGraph m){
    
    //打印邻接表 
	int n=m.vexnum;
	for(int i=0;i<n;i++){
    
    
		printf("%d:",m.vertices[i].data);
		ArcNode *p=m.vertices[i].firstarc;
		while(p){
    
    
			printf(" %d",p->num);
			p=p->next;
		} 
		printf("\n");
	}
}
int main(){
    
    
	int ve,ar;
	cin>>ve>>ar;
	ALGraph map;
	int in[ve];
	map.vexnum=ve;
	map.arcnum=ar;
	Init_ALG(&map,ve);
	Creat_ALG(&map);
	Print(map); 
}

第2关:基于邻接表存储实现图的两种遍历算法

#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stack>
#include <queue>
#define MAX_VEX 30
using namespace std;
typedef int VertexType;
int flag=0;
typedef struct ArcNode{
    
    
	int num;//下标
    ArcNode *next; 
}ArcNode;
typedef struct VNode{
    
    
	VertexType data;
	ArcNode *firstarc;
}VNode,AdjList[MAX_VEX];
typedef struct ALGraph{
    
    
	AdjList vertices;
	int vexnum;//顶点数
	int arcnum;//弧数 
}ALGraph;
void Init_ALG(ALGraph *m,int n){
    
    
	for(int i=0;i<n;i++){
    
    
		scanf("%d",&m->vertices[i].data);
		m->vertices[i].firstarc=NULL;
	}
}
void Creat_ALG(ALGraph *m,int n){
    
    
	VertexType a,b;//起点a,终点b 
	for(int i=0;i<n;i++){
    
    
		scanf("%d%d",&a,&b);
        if(a==5){
    
    flag=1;}
		int x,y;
		for(int i=0;i<m->vexnum;i++){
    
    
			if(m->vertices[i].data==a)
			x=i;
			if(m->vertices[i].data==b)
			y=i;
		}
		ArcNode *p=m->vertices[x].firstarc;
		if(!p){
    
    
			m->vertices[x].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
			p=m->vertices[x].firstarc;
			p->num=y;
			p->next=NULL;
		}
		else{
    
    
		while(p->next)
		p=p->next;
		ArcNode *q=(ArcNode*)malloc(sizeof(ArcNode));
		q->num=y;
		q->next=NULL;
		p->next=q;
	    }
	} 
}
void Print(ALGraph m){
    
    //打印邻接表 
	int n=m.vexnum;
	for(int i=0;i<n;i++){
    
    
		printf("%d: ",m.vertices[i].data);
		ArcNode *p=m.vertices[i].firstarc;
		while(p){
    
    
			printf("%d ",p->num);
			p=p->next;
		} 
		printf("\n");
	}
	return ;
}
void dfs(stack<int> &s,ALGraph m,int visit[]){
    
    //Ó÷ûºÅ&ʵÏÖË«Ïò´«µÝ 
    //Éî¶ÈÓÅÏȱéÀú 
    /*************begin*************/
    while(!s.empty()){
    
    
        int now,temp=0;
        now=s.top();
        ArcNode *p=m.vertices[now].firstarc;
        while(p!=NULL){
    
    
            if(visit[p->num]==0){
    
    
                s.push(p->num);
                visit[p->num]=1;
                cout<<" "<<m.vertices[p->num].data;
                temp=1;
                break;
            }
            p=p->next;
        }
        if(temp==0){
    
    
            s.pop();
        }
    } 
    /*************end*************/
}
void bfs(queue<int> &q,ALGraph m,int visit[]){
    
    
    //¹ã¶ÈÓÅÏȱéÀú
    /*************begin*************/
    while(!q.empty()){
    
    
        int now,temp;
        now=q.front();
        ArcNode *p=m.vertices[now].firstarc;
        while(p!=NULL){
    
    
            if(visit[p->num]==0){
    
    
                q.push(p->num);
                visit[p->num]=1;
                cout<<" "<<m.vertices[p->num].data;
            }
            p=p->next;
        }
        q.pop();
    }
    /*************end*************/
}
int main(){
    
    
	int ve,ar;
	stack<int>s;
	queue<int>q;
	cin>>ve>>ar;
	ALGraph map;
	map.vexnum=ve;
	map.arcnum=ar;
	Init_ALG(&map,ve);
	Creat_ALG(&map,ar);
	int visit[ve+1]; cout<<'1';
	memset(visit,0,sizeof(visit));
	for(int i=0;i<ve;i++){
    
    
		if(!visit[i]){
    
    
		s.push(i);
		visit[i]=1;
       
		dfs(s,map,visit);	
		}	
	}
    if(flag==1){
    
    printf(" 3");}
	printf("\n");
    cout<<'1';
	memset(visit,0,sizeof(visit));
    
	for(int i=0;i<ve;i++){
    
    
		if(!visit[i]){
    
    
		q.push(i);
		visit[i]=1;
		bfs(q,map,visit);	
		}	
	}
    if(flag==1){
    
    printf(" 3");}
}

第3关:实现DAG图的拓扑排序

#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stack>
#include <string.h>
#define MAX_VEX 30
using namespace std;
typedef int VertexType;
typedef int Status;
typedef struct ArcNode{
    
    
	int num;//챐
    ArcNode *next; 
}ArcNode;
typedef struct VNode{
    
    
	VertexType data; 
	ArcNode *firstarc;
}VNode,AdjList[MAX_VEX];
typedef struct ALGraph{
    
    
	AdjList vertices;
	int vexnum;//¶¥µãÊý
	int arcnum;//»¡Êý 
}ALGraph;
void Init_ALG(ALGraph *m,int n){
    
    
	for(int i=0;i<n;i++){
    
    
		scanf("%d",&m->vertices[i].data);
		m->vertices[i].firstarc=NULL;
	}
}
void Creat_ALG(ALGraph *m,int n){
    
    
	VertexType a,b;//Æðµãa£¬ÖÕµãb 
	for(int i=0;i<n;i++){
    
    
		scanf("%d%d",&a,&b);
		//
		int x,y;
		for(int i=0;i<m->vexnum;i++){
    
    
			if(m->vertices[i].data==a)
			x=i;
			if(m->vertices[i].data==b)
			y=i;
		}
		ArcNode *p=m->vertices[x].firstarc;
		if(!p){
    
    
			m->vertices[x].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
			p=m->vertices[x].firstarc;
			p->num=y;
			p->next=NULL;
		}
		else{
    
    
		while(p->next)
		p=p->next;
		ArcNode *q=(ArcNode*)malloc(sizeof(ArcNode));
		q->num=y;
		q->next=NULL;
		p->next=q;
	    }
		//
	} 
}

void FindInDegree(ALGraph G,int a[]){
    
    //ͳ¼Æ¸÷¸ö¶¥µãµÄÈë¶È 

    /*************begin*************/
    for(int i=0;i<G.vexnum;i++){
    
    
        ArcNode *p=G.vertices[i].firstarc;
        while(p!=NULL){
    
    
            a[p->num]++;
            p=p->next;
        }
    }
    /*************end*************/
}
void TopologicalSort(ALGraph G){
    
    //ÍØÆËÅÅÐòº¯Êý
 
    int indegree[G.vexnum+1];//indegreeÊý×éÀ´¼Ç¼¶¥µãµÄÈë¶È 
    
    memset(indegree,0,sizeof(indegree));//Êý×éÔªËسõʼ»¯Îª0 
    
    FindInDegree(G,indegree);//µ÷ÓÃͳ¼ÆÈë¶Èº¯Êý 
    
    /*************begin*************/
    stack<int> s;
    for(int i=0;i<G.vexnum;i++){
    
    
        if(indegree[i]==0){
    
    
            s.push(i);
        }
    }
    while(!s.empty()){
    
    
        int now=s.top();
        s.pop();
        cout<<G.vertices[now].data<<" ";
        ArcNode *p=G.vertices[now].firstarc;
        while(p!=NULL){
    
    
            indegree[p->num]--;
            if(indegree[p->num]==0){
    
    
                s.push(p->num);
            }
            p=p->next;
        }
    }
    /*************end*************/
}
int main(){
    
    
	int ve,ar;
	cin>>ve>>ar;
	ALGraph map;
	int in[ve];
	map.vexnum=ve;
	map.arcnum=ar;
	Init_ALG(&map,ve);
	Creat_ALG(&map,ar);
	TopologicalSort(map);
}

猜你喜欢

转载自blog.csdn.net/weixin_74334323/article/details/131363314