码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之图 Part2 - 2

时间:2014-08-09 13:18:07      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   io   数据   for   art   ar   

邻接表

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来看十字链表。

数据结构之图 Part2 - 2,布布扣,bubuko.com

数据结构之图 Part2 - 2

标签:des   style   color   io   数据   for   art   ar   

原文地址:http://www.cnblogs.com/bnbqian/p/3900789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!