데이터 구조-교차 연결된 그래프 목록의 실현

교차 연결 목록 정의

유향 그래프에 대한 인접 목록 이전에 인접 테이블에 대한 인접 목록으로 나눌 수 있으며, 액세스에 대한 동일한 요구 사항이 아닌 테이블, 생성 된 다른 인접 테이블이
나타납니다. 교차 목록은 테이블과 인접 테이블에 대해 함께 인접합니다
. 동일한 코드에서 정점에 대해 그 정도와 정도를 찾을 수 있습니다.
교차 연결 목록 저장 구조에서 방향 그래프에서 정점 테이블의 노드 구조는 다음과 같습니다.
여기에 사진 설명 삽입
데이터는 데이터를 저장하고 firstIn은 다음을 가리 킵니다. 가장자리 안쪽 테이블 머리 포인터, firstOut은 바깥 쪽 테이블의 머리 포인터를 가리 킵니다.
사이드 테이블 노드 구조는 다음과 같습니다
여기에 사진 설명 삽입
. tailVex는 정점 배열에서 호의 호 꼬리 정점의 위치
를 나타냅니다. xList headVex는 정점 배열 에서 호의 호의 머리 정점 위치
나타냅니다. hLink는 다음을 가리킴을 나타냅니다. 호 헤드가 동일한 호
tLink then 호 테일이 동일한 다음 호를 가리킴을 나타냅니다.
아래 그림과 같은 방향성 그래프 :
여기에 사진 설명 삽입
여기에 사진 설명 삽입

교차 연결 목록 구조 정의

inof가 무슨 뜻인지 모르겠어요

#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;
}

이 글을 작성할 때 이전 데이터 구조가 부드러운 조건에서 생성 된 것을 발견했습니다. 입력 형식이나 범위를 초과하면 입력이 호환되지 않음을 알리는 루프 본문을 작성하고 다시 입력해야합니다. 다음주기 돌아 보면 다시 작성하겠습니다. 먼저 데이터 구조의 기본 사항을 살펴보고 c로 작성할 계획입니다. 이제 JAVA를 시작합니다. 기본 사항을 배우고 나면 데이터 구조를 JAVA로 작성하고 JAVA를 두 번째로 연습하여 데이터 구조를 통합하고 개선합니다.

추천

출처blog.csdn.net/weixin_46096297/article/details/113248682