数据结构:邻接表法存储有向图

有向图的拓扑结构

在这里插入图片描述

#include<iostream>
#include<string>
#include<queue>

#define MAXVEX 10

using namespace std;

class Graph{
private:
	struct EdgeNode{
		int adjvex;
		int weight;
		EdgeNode* next;
	};
	
	struct VertexNode{
		string data;
		EdgeNode *pFirstEdge;
	};
	
	struct AdjListGraph{
		VertexNode adjList[MAXVEX];
		int iVexNum;
		int iEdgeNum;
	};
	
	AdjListGraph *graph;
	
public:
	Graph(){
		graph=new AdjListGraph();
	}
	
	int GetIndexByVertexVal(AdjListGraph *graph,string val){
		//通过点的值获取点的下标(邻接表中的下标) 
		for(int i=0;i<graph->iVexNum;i++){
			if(val==graph->adjList[i].data){
				return i;
			}
		}
		return -1;
	}
	
	int CreateAdjListGraph(){
		//创建上图中的有向图 
		CreateAdjListGraph_s(graph);
	}
	
	int CreateAdjListGraph_s(AdjListGraph *&graph){
		graph->iVexNum=8;
		graph->iEdgeNum=9;
		graph->adjList[0].data="first";
		graph->adjList[1].data="second";
		graph->adjList[2].data="third";
		graph->adjList[3].data="forth";
		graph->adjList[4].data="fifth";
		graph->adjList[5].data="sixth";
		graph->adjList[6].data="seventh";
		graph->adjList[7].data="ninth";
		for(int i=0;i<graph->iVexNum;i++){
			graph->adjList[i].pFirstEdge=NULL;
		}
		
		int m=0;
		int n=2;
		EdgeNode *EdgeNode_1=new EdgeNode;
		EdgeNode_1->adjvex=n;
		EdgeNode_1->weight=0;
		EdgeNode_1->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_1;
		
		m=1;
		n=3;
		EdgeNode *EdgeNode_2=new EdgeNode;
		EdgeNode_2->adjvex=n;
		EdgeNode_2->weight=0;
		EdgeNode_2->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_2;
		
		m=1;
		n=5;
		EdgeNode *EdgeNode_3=new EdgeNode;
		EdgeNode_3->adjvex=n;
		EdgeNode_3->weight=0;
		EdgeNode_3->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_3;
		
		m=2;
		n=4;
		EdgeNode *EdgeNode_4=new EdgeNode;
		EdgeNode_4->adjvex=n;
		EdgeNode_4->weight=0;
		EdgeNode_4->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_4;
		
		m=3;
		n=4;
		EdgeNode *EdgeNode_5=new EdgeNode;
		EdgeNode_5->adjvex=n;
		EdgeNode_5->weight=0;
		EdgeNode_5->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_5;
		
		m=3;
		n=5;
		EdgeNode *EdgeNode_6=new EdgeNode;
		EdgeNode_6->adjvex=n;
		EdgeNode_6->weight=0;
		EdgeNode_6->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_6;
		
		m=4;
		n=6;
		EdgeNode *EdgeNode_7=new EdgeNode;
		EdgeNode_7->adjvex=n;
		EdgeNode_7->weight=0;
		EdgeNode_7->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_7;
		
		m=5;
		n=7;
		EdgeNode *EdgeNode_8=new EdgeNode;
		EdgeNode_8->adjvex=n;
		EdgeNode_8->weight=0;
		EdgeNode_8->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_8;
		
		m=6;
		n=7;
		EdgeNode *EdgeNode_9=new EdgeNode;
		EdgeNode_9->adjvex=n;
		EdgeNode_9->weight=0;
		EdgeNode_9->next=graph->adjList[m].pFirstEdge;
		graph->adjList[m].pFirstEdge=EdgeNode_9;
		return 1;
	}
	
	void DFS(int i){
		//深度优先搜索 
		cout<<"DFS"<<endl;
		bool visited[MAXVEX]={0};
		DFS_s(visited,i,graph);
		cout<<endl;
	}
	
	void DFS_s(bool *visited,int i,AdjListGraph* graph){
		cout<<graph->adjList[i].data<<" ";
		visited[i]=true;
		EdgeNode *pEdge=graph->adjList[i].pFirstEdge;
		while(pEdge){
			int j=pEdge->adjvex;
			if(!visited[j]){
				DFS_s(visited,j,graph);
			}
			pEdge=pEdge->next;
		}
	}
	
	void BFS(int i){
		//广度优先搜索 
		cout<<"BFS"<<endl;
		bool visited[MAXVEX]={0};
		BFS_s(visited,i,graph);
		cout<<endl;
	}
	
	void BFS_s(bool *visited,int i,AdjListGraph *graph){
		queue<int> Q;
		cout<<graph->adjList[i].data<<" ";
		visited[i]=true;
		Q.push(i);
		while(!Q.empty()){
			int iVex=Q.front();
			Q.pop();
			EdgeNode *pEdge=graph->adjList[iVex].pFirstEdge;
			while(pEdge){
				if(!visited[pEdge->adjvex]){
					cout<<graph->adjList[pEdge->adjvex].data<<" ";
					visited[pEdge->adjvex]=true;
					Q.push(pEdge->adjvex);
				}
				pEdge=pEdge->next;
			}
		}		
	}
	
	int GetVertexDegree(AdjListGraph *graph,string val){
		//计算出度和入度的和 
		int m=GetIndexByVertexVal(graph,val);
		int iCount=0;
		for(int i=0;i<graph->iVexNum;i++){
			if(i==m){
				EdgeNode *pEdgeOut=graph->adjList[i].pFirstEdge;
				while(pEdgeOut){
					//这里累加的是出度 
					++iCount;
					pEdgeOut=pEdgeOut->next;
				}
			}
			else{
				EdgeNode *pEdgeIn=graph->adjList[i].pFirstEdge;
				while(pEdgeIn){
					//这里累加的是入度 
					if(pEdgeIn->adjvex==m){
						++iCount;
					}
					pEdgeIn=pEdgeIn->next;
				}
			}
		}
		return iCount;
	}
};


int main(){
	Graph *g=new Graph();
	g->CreateAdjListGraph(); 
	g->BFS(1);
	g->DFS(1);
	return 0;
}



https://blog.csdn.net/s634772208/article/details/45580333

猜你喜欢

转载自blog.csdn.net/kking_edc/article/details/106837648