十字链表的创建

十字链表:入度和出度结合在一起存储;邻接表中一个边要存储两次,采用十字链表来存储有向图;它是将有向图的邻接表和逆邻接表结合起来;他将存储下一条入度边和出度边,同时存储他的头尾节点;各边之间没有联系;

十字链表的创建_第1张图片

十字链表的创建_第2张图片

 

 


#ifndef MULTILIST_H_
#define MULTILIST_H_

#include
#include
#include
using namespace std;
#define MaxVex 20

namespace OLG {
	typedef int ArcType;
	typedef char VertexType;


	typedef struct ArcNode {//边的类型
		ArcType arcdata;
		int headVertex, tailVertex;
		struct ArcNode* nextHeadArc, * nextTailArc;


	}ArcNode, * ArcList;

	typedef struct VertexNode {//顶点的类型

		VertexType vertexData;//值
		ArcList headList, tailList;//边指针,虚拟头节点;
	}VertexNode, * VertexList;


	typedef struct {		//图的数据结构;

		VertexList vertexList;	//数组;指向顶点数组;
		int numVertexs, numArcs;//顶点数
	}OLGraph;

	void InitOLGraph(OLGraph* G)
	{
		G->numArcs = 0;
		G->numVertexs = 0;
		//指针数组;里面的元素不是指针;
		G->vertexList = (VertexNode*)malloc(MaxVex * sizeof(VertexNode));

		int i;
		for (i = 0; i < MaxVex; i++)

		{
			//初始化头尾节点
			G->vertexList[i].headList = (ArcList)malloc(sizeof(ArcNode));
			G->vertexList[i].headList->nextHeadArc = NULL;
			G->vertexList[i].headList->nextTailArc = NULL;

			G->vertexList[i].tailList = (ArcList)malloc(sizeof(ArcNode));
			G->vertexList[i].tailList->nextHeadArc = (ArcList)malloc(sizeof(ArcNode));
			G->vertexList[i].tailList->nextTailArc = (ArcList)malloc(sizeof(ArcNode));
		}
		cout << "已初始化十字链表" << endl;

	}

	void Create_OLGraph(OLGraph*G)
		{
		//顶点数和边数
		cout << "请输入顶点数: ";
		cin>> G->numVertexs;
		cout << "请输入边数: ";
		cin >> G->numArcs;
		int i, j, k;

		//依次录入顶点信息
		for (i=0;inumVertexs;i++) 
			{
			cout << "请输入顶点信息"<> G->vertexList[i].vertexData;
			}

		//录入边的信息;
		ArcType w=0;

		for (k = 0; k < G->numArcs; k++)
			{
			cout << "请输入弧头,弧尾和权值" << endl;
			cin >> i >> j >> w;
			ArcNode* s = (ArcList)malloc(sizeof(ArcNode));
			s->arcdata = w;
			s->headVertex = i-1;
			s->tailVertex = j-1;
			s->nextHeadArc = G->vertexList[i-1].headList->nextHeadArc;
			G->vertexList[i].headList->nextHeadArc = s;

			s->nextTailArc = G->vertexList[j - 1].tailList->nextTailArc;
			G->vertexList[j - 1].tailList->nextTailArc = s;

			}

		cout << "已完成创建十字链表"<numVertexs; i++)
			{
			cout << "输出第" << i << "个点的信息: " << G->vertexList[i].vertexData<vertexList[i].headList->nextHeadArc;
			while (p != NULL)
			{

				cout << G->vertexList[p->headVertex].vertexData << "  "
					<< p->arcdata << "  " <<
					G->vertexList[p->tailVertex].vertexData << endl;
				p = p->nextHeadArc;
			}

			cout << "相同弧尾的弧";
			p = G->vertexList[i].tailList->nextTailArc;
			while (p != NULL)
			{

				//cout << "(" << G->vertexList[p->headVertex].vertexData;

				cout << G->vertexList[p->headVertex].vertexData << "  "
					<< p->arcdata << "  " << 
					G->vertexList[p->tailVertex].vertexData << endl;
				
				
				p = p->nextTailArc;
			}

			cout << "该节点全部信息输入完毕"<

你可能感兴趣的:(算法,数据结构)