1. 程式人生 > >資料結構篇:鄰接表基礎(C#)

資料結構篇:鄰接表基礎(C#)

 今天突發奇想利用C#實現鄰接表(供參考),基本思想是把C++/C語言中的結構體改成類,基本思路一致,參考C++版

https://blog.csdn.net/qq_15020543/article/details/84178417

還是以這張圖為例,我們在C#裡面實現

using System;
using UnityEngine;
 
    /// <summary>
    /// 地圖鄰接表結構
    /// </summary>
    public class AdjacencyList
    {
        /// <summary>
        /// 邊表結點
        /// </summary>
        public class EdgeNode
        {
            /// <summary>
            /// 頂點在陣列中的下標
            /// </summary>
            public int adjvex;
 
            /// <summary>
            /// 指向下一個邊表結點
            /// </summary>
            public EdgeNode next;
 
            /// <summary>
            /// 邊表結點的建構函式
            /// </summary>
            /// <param name="adjvex">頂點在陣列中的下標</param>
            public EdgeNode(int adjvex)
            {
                this.adjvex = adjvex;
            }
        }
 
        /// <summary>
        /// 頂點表
        /// </summary>
        public class VertexNode
        {
            /// <summary>
            /// 頂點儲存的資料
            /// </summary>
            public string _fruitType;
 
            /// <summary>
            /// 指向的第一個頂點
            /// </summary>
            public EdgeNode _firstEdge;
 
            /// <summary>
            /// 頂點表的建構函式
            /// </summary>
            /// <param name="_fruitType">所代表的水果種類</param>
            public VertexNode(string _fruitType)
            {
                this._fruitType = _fruitType;
                this._firstEdge = null;
            }
        }
 
        /// <summary>
        /// 鄰接表
        /// </summary>
        public class GraphAdjList
        {
            /// <summary>
            /// 頂點結點陣列
            /// </summary>
            public VertexNode[] _vertexNodes;
 
            /// <summary>
            /// 頂點數,邊數
            /// </summary>
            public int numVertexes, numEdges;
 
            public GraphAdjList(int numVertexes, int numEdges, string fruits)
            {
                this.numVertexes = numVertexes;
                this.numEdges = numEdges;
                string[] fruit = fruits.Split(',');
                _vertexNodes = new VertexNode[numEdges];
                for (int i = 0; i < this.numVertexes; i++)
                {
                    _vertexNodes[i] = new VertexNode(fruit[i]);
                }
            }
        }
 
        /// <summary>
        /// 建立鄰接表
        /// </summary>
        private GraphAdjList graphAdjList = new GraphAdjList(4, 5, "A,B,C,D") { };
 
        /// <summary>
        /// 初始化邊聯絡,無向鄰接表即為雙向鄰接表
        /// </summary>
        /// <param name="fromVertexNode">起始頂點結點</param>
        /// <param name="toVertexNode">目標頂點結點</param>
        public void InitEdges(int fromVertexNode, int toVertexNode)
        {
            EdgeNode temp = new EdgeNode(fromVertexNode) {next = graphAdjList._vertexNodes[toVertexNode]._firstEdge};
            graphAdjList._vertexNodes[toVertexNode]._firstEdge = temp;
            temp = new EdgeNode(toVertexNode) {next = graphAdjList._vertexNodes[fromVertexNode]._firstEdge};
            graphAdjList._vertexNodes[fromVertexNode]._firstEdge = temp;
        }
 
        /// <summary>
        /// 正式開始建立鄰接表
        /// </summary>
        public void CreateAlGraph()
        {
            InitEdges(0, 1);
            InitEdges(0, 2);
            InitEdges(0, 3);
            InitEdges(2, 1);
            InitEdges(2, 3);
        }
 
        /// <summary>
        /// 展示鄰接表
        /// </summary>
        public void ShowALGraph()
        {
            for (int i = 0; i < graphAdjList.numVertexes; i++)
            {
                Debug.Log("頂點" + i + "為:" + graphAdjList._vertexNodes[i]._fruitType + "--FirstEdge--");
                EdgeNode temp = new EdgeNode(0);
                temp = graphAdjList._vertexNodes[i]._firstEdge;
                while (temp != null)
                {
                    Debug.Log(temp.adjvex + "--Next--");
                    temp = temp.next;
                }
 
                Debug.Log("END " + graphAdjList._vertexNodes[i]._fruitType);
            }
        }
    }