首页 > 代码库 > 数据结构之图 Part2 - 2
数据结构之图 Part2 - 2
邻接表
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LH.GraphConsole{ public struct AdjacencyListGraph { public List<EdgeItem>[] VertexNodes; public AdjacencyListGraph(int vertexNumber) { VertexNodes = new List<EdgeItem>[vertexNumber]; for (int i = 0; i < vertexNumber; i++) { VertexNodes[i] = new List<EdgeItem>(); } } } public struct EdgeItem { public int Weight; public List<EdgeItem> NextNode; public EdgeItem(int weight, List<EdgeItem> nextNote) { Weight = weight; NextNode = nextNote; } }}
同样是简单的主程序:
namespace LH.GraphConsole{ class Program { static void Main(string[] args) { AdjacencyList(); } private static void AdjacencyList() { int vertexNumber = 5; var adjacencyListGraph = new AdjacencyListGraph(vertexNumber); adjacencyListGraph.VertexNodes[0].Add(new EdgeItem(6, adjacencyListGraph.VertexNodes[4])); adjacencyListGraph.VertexNodes[1].Add(new EdgeItem(9, adjacencyListGraph.VertexNodes[0])); adjacencyListGraph.VertexNodes[1].Add(new EdgeItem(3, adjacencyListGraph.VertexNodes[2])); adjacencyListGraph.VertexNodes[2].Add(new EdgeItem(2, adjacencyListGraph.VertexNodes[0])); adjacencyListGraph.VertexNodes[2].Add(new EdgeItem(5, adjacencyListGraph.VertexNodes[3])); adjacencyListGraph.VertexNodes[3].Add(new EdgeItem(1, adjacencyListGraph.VertexNodes[4])); } }}
下一部分part3来看十字链表。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。