Data structure-the realization of the cross-linked list of graphs

Cross-linked list definition

For a directed graph, the previous adjacency list can be divided into adjacency list and inverse adjacency list. When the ingress and egress requirements are different, the generated adjacency list is also different
. The emergence of the cross linked list is to integrate the adjacency list and the inverse adjacency list together
in In the same code, for a vertex, you can find its out degree and its in degree.
In the cross-linked list storage structure, the node structure of the vertex table in the directed graph is as follows:
Insert picture description here
data stores data, firstIn points to the in-edge table Head pointer, firstOut points to the head pointer of the out side table.
The side table node structure is as follows
Insert picture description here
: tailVex represents the position of the arc tail vertex of the arc in the vertex array xList
headVex represents the position of the arc head vertex of the arc in the vertex array
hLink indicates that it points to the next arc with the same arc head
tLink then Indicates that it points to the next arc with the same arc tail.
A directed graph as shown in the figure below:
Insert picture description here
Insert picture description here

Cross-linked list structure definition

I don't know what the inof means

#include<iostream>
using namespace std;
#define MAX 25

typedef char Vertype;
typedef int infotype;//这是权重吗
typedef int Status;

typedef struct Arc_node//弧结点结构
{
    
    
	int tailvex, headvex;
	struct Arc_node *hlink, *tlink;
	infotype *info;//这是权重的意思吗?
	int weight;
}Arc_node;

typedef struct Vex_Node//顶点结构
{
    
    
	Vertype data;
	Arc_node *firstIn, *firstOut;
}Vex_Node;

typedef struct Or_list//十字链表
{
    
    
	Vex_Node list[MAX];//顶点列表
	int Vexnum, Arcnum;//顶点数目 和 边的数目
}Or_list;
//被调用的函数要声明
Status locateVertex(Or_list &G, Vex_Node node);
Status inserArc(Or_list &G, Vex_Node node1, Vex_Node node2);
Status inserArction(Or_list &G, int index1, int index2);

//生成图
Status CreatOr_list(Or_list &G)
{
    
    
	cout <<"输入顶点数目和边的数目: " << endl;
	cin >> G.Vexnum;
	cin >> G.Arcnum;

	//初始化
	cout <<"输入"<<G.Vexnum<<"个顶点内容: " << endl;
	for (int i = 0; i < G.Vexnum; i++)
	{
    
    
		cin >> G.list[i].data;
		G.list[i].firstIn = NULL;
		G.list[i].firstOut = NULL;
	}

	//增加弧
	for (int j = 0; j < G.Arcnum; j++)
	{
    
    
		//此时,只有初始化的十字链表  弧还没有形成,所以这里将形成弧
		Vex_Node node1, node2;
		cout << "请输入第"<<j+1<<"条弧的两个顶点"<< endl;
		cin >> node1.data >> node2.data;
		inserArc(G,node1,node2);
	}
	return 0;
}

//获取两个顶点下标 并生成弧
Status inserArc(Or_list &G, Vex_Node node1, Vex_Node node2)
{
    
    
	int index1 = locateVertex(G,node1);//获取下标
	int index2 = locateVertex(G,node2);

	if (index1 == -1 || index2 == -1)
	{
    
    
		cout << "顶点不存在"<< endl;
		return NULL;
	}
	inserArction(G,index1,index2);
	return 0;
}

//寻找某一顶点的下标
Status locateVertex(Or_list &G, Vex_Node node)//获取该顶点下标
{
    
    
	int i,index=-1;
	for (i = 0; i < G.Vexnum; i++)
	{
    
    
		if (G.list[i].data == node.data)
		{
    
    
			index = i;
			break;//如果有重复的呢
		}
	}
	return index;
}

//生成弧
Status inserArction(Or_list &G, int index1, int index2)
{
    
    
	Arc_node *pArc = new Arc_node;
	pArc->tailvex = index1;
	pArc->headvex = index2;

	pArc->info = NULL;//这一段是什么, 
	pArc->tlink = G.list[index1].firstOut;
	pArc->hlink = G.list[index2].firstIn;

	G.list[index1].firstOut = pArc;
	G.list[index2].firstIn = pArc;

	cout << "输入权重: "<< endl;
	cin>>pArc->weight;
	return 0;
}

//输出图
Status DispGhaph(Or_list &G)
{
    
    
	for (int i = 0; i < G.Vexnum; i++)
	{
    
    
		//ptail  和phead  可以指向弧的前后顶点位置
		Arc_node *ptail = G.list[i].firstOut;//以i位置顶点为出指针
		Arc_node *phead = G.list[i].firstIn;//以i位置顶点为入指针

		cout << i << " 位置顶点元素为: " << G.list[i].data << endl;
		cout << "输出以" << G.list[i].data << "弧尾结构: " << endl;
		while (ptail)
		{
    
    
			cout << G.list[ptail->tailvex].data <<'\t'<< "->" << '\t' << ptail->weight << '\t' << G.list[ptail->headvex].data << endl;
			ptail = ptail->tlink;
		}
		cout << "输出以" << G.list[i].data << "弧头结构" << endl;
		while (phead)
		{
    
    
			cout << G.list[phead->headvex].data << '\t' << phead->weight << '\t' << "<-" << '\t' << G.list[phead->tailvex].data << endl;
			phead = phead->hlink;
		}

	}
	return 0;
}
//增加弧
Status ADDGhaphARC(Or_list &G)
{
    
    
	//增加新的弧   如果弧原本存在,提示。如果不存在就生成这条弧
	Vex_Node node1, node2;
	int I1=-1, I2=-1;
	cout << "输入两个顶点以便生成弧"<< endl;
	cin >> node1.data >> node2.data;
	for (int i = 0; i < G.Vexnum; i++)
	{
    
    
		if (G.list[i].data == node1.data)
		{
    
    
			I1 = i;
			break;
		}
	}

	for (int i = 0; i < G.Vexnum; i++)
	{
    
    
		if (G.list[i].data == node2.data)
		{
    
    
			I2 = i;
			break;
		}
	}

	if (I1 == -1 || I2 == -1)
	{
    
    
		cout << "没有这个值"<< endl;
		return NULL;
	}
	else if (G.list[I1].firstOut ==G.list[I2].firstIn)
	{
    
    
		cout << "弧原本存在"<< endl;
		return NULL;
	}
	else
	{
    
    
		cout << "顶点合法,将生成弧"<< endl;
		inserArction(G,I1,I2);
	}
	return 0;
}

int main()
{
    
    
	Or_list G;
	CreatOr_list(G);
	DispGhaph(G);
	ADDGhaphARC(G);
	DispGhaph(G);
	system("pause");
	return 0;
}

When writing this, I found that the previous data structure was generated under smooth conditions. If the input format or range is exceeded, you should write a loop body that prompts that the input is not compliant, and re-enter,,, remember the next cycle When I look back, I will rewrite it. I plan to go through the basics of the data structure first, and write it in c. Now I’m starting to get started with JAVA. After I learn the basics, I will write the data structure in JAVA for practice. JAVA second to consolidate and improve the data structure

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/113248682