图的邻接表存储结构

程序调用入口

[csharp]  view plain copy print ?
  1. using System;  
  2.   
  3. namespace Graphic_AdjacencyList  
  4. {  
  5.     internal class Program  
  6.     {  
  7.         private static void Main(string[] args)  
  8.         {  
  9.             var adjacencyList = new AdjacencyList<char>();  
  10.   
  11.             Console.WriteLine("1.初始化树结构:");  
  12.   
  13.             Console.WriteLine("=================================");  
  14.             //添加顶点;  
  15.             adjacencyList.AddVertex('A');  
  16.             adjacencyList.AddVertex('B');  
  17.             adjacencyList.AddVertex('C');  
  18.             adjacencyList.AddVertex('D');  
  19.   
  20.             //添加边;  
  21.             adjacencyList.AddEdge('A''B');  
  22.             adjacencyList.AddEdge('A''C');  
  23.             adjacencyList.AddEdge('A''D');  
  24.             adjacencyList.AddEdge('B''D');  
  25.             adjacencyList.AddEdge('C''D');  
  26.   
  27.             Console.WriteLine("=================================");  
  28.   
  29.             Console.WriteLine("2.树的邻接表遍历:");  
  30.             Console.WriteLine(adjacencyList.ToString());  
  31.             Console.Read();  
  32.         }  
  33.     }  
  34. }  
图-顶点类

[csharp]  view plain copy print ?
  1. namespace Graphic_AdjacencyList  
  2. {  
  3.     /// <summary>  
  4.     ///     链表中的节点  
  5.     /// </summary>  
  6.     public class Node<T>  
  7.     {  
  8.         /// <summary>  
  9.         ///     邻接点域  
  10.         /// </summary>  
  11.         public Vertex<T> Adjvex;  
  12.   
  13.         /// <summary>  
  14.         ///     下一个邻接点指针域  
  15.         /// </summary>  
  16.         public Node<T> Next;  
  17.   
  18.         /// <summary>  
  19.         ///     链表中的节点  
  20.         /// </summary>  
  21.         /// <param name="value"></param>  
  22.         public Node(Vertex<T> value)  
  23.         {  
  24.             Adjvex = value;  
  25.         }  
  26.     }  
  27. }  
图-链表中的表头节点类:

[csharp]  view plain copy print ?
  1. using System;  
  2.   
  3. namespace Graphic_AdjacencyList  
  4. {  
  5.     /// <summary>  
  6.     ///     链表中的表头节点  
  7.     /// </summary>  
  8.     /// <typeparam name="T"></typeparam>  
  9.     public class Vertex<T>  
  10.     {  
  11.         /// <summary>  
  12.         ///     数据  
  13.         /// </summary>  
  14.         public T Data;  
  15.   
  16.         /// <summary>  
  17.         ///     邻接表表头指针  
  18.         /// </summary>  
  19.         public Node<T> FirstEdge;  
  20.   
  21.         /// <summary>  
  22.         ///     访问标志,遍历时使用  
  23.         /// </summary>  
  24.         public Boolean Visited;  
  25.   
  26.         /// <summary>  
  27.         ///     表头节点  
  28.         /// </summary>  
  29.         /// <param name="value"></param>  
  30.         public Vertex(T value) //构造方法;  
  31.         {  
  32.             Data = value;  
  33.         }  
  34.     }  
  35. }  
  图-图的邻接表存储类:
[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace Graphic_AdjacencyList  
  6. {  
  7.     /// <summary>  
  8.     ///     图的邻接表存储类  
  9.     /// </summary>  
  10.     /// <typeparam name="T"></typeparam>  
  11.     public class AdjacencyList<T>  
  12.     {  
  13.         /// <summary>  
  14.         ///     图的顶点集合  
  15.         /// </summary>  
  16.         private readonly List<Vertex<T>> _items;  
  17.   
  18.         public AdjacencyList()  
  19.             : this(10)  
  20.         {  
  21.         }  
  22.   
  23.         public AdjacencyList(int capacity)  
  24.         {  
  25.             _items = new List<Vertex<T>>(capacity);  
  26.         }  
  27.   
  28.         /// <summary>  
  29.         ///     添加顶点  
  30.         /// </summary>  
  31.         /// <param name="item"></param>  
  32.         public void AddVertex(T item)  
  33.         {  
  34.             if (Contains(item))  
  35.             {  
  36.                 throw new ArgumentException("插入了重复顶点!");  
  37.             }  
  38.             _items.Add(new Vertex<T>(item));  
  39.             Console.WriteLine("添加顶点:" + item);  
  40.         }  
  41.   
  42.         /// <summary>  
  43.         ///     添加无向边  
  44.         /// </summary>  
  45.         /// <param name="from"></param>  
  46.         /// <param name="to"></param>  
  47.         public void AddEdge(T from, T to)  
  48.         {  
  49.             Vertex<T> fromVer = Find(from);  
  50.             if (fromVer == null)  
  51.             {  
  52.                 throw new ArgumentException("头顶点不存在!");  
  53.             }  
  54.             Vertex<T> toVer = Find(to);  
  55.             if (toVer == null)  
  56.             {  
  57.                 throw new ArgumentException("尾顶点不存在!");  
  58.             }  
  59.             //无向边的两个顶点都需记录边信息;  
  60.             AddDirectedEdge(fromVer, toVer);  
  61.             AddDirectedEdge(toVer, fromVer);  
  62.   
  63.             Console.WriteLine(string.Format("添加无向边:{0}—{1}", from, to));  
  64.         }  
  65.   
  66.   
  67.         /// <summary>  
  68.         ///     添加有向边  
  69.         /// </summary>  
  70.         /// <param name="fromVer"></param>  
  71.         /// <param name="toVer"></param>  
  72.         private void AddDirectedEdge(Vertex<T> fromVer, Vertex<T> toVer)  
  73.         {  
  74.             if (fromVer.FirstEdge == null//无临接点时;  
  75.             {  
  76.                 fromVer.FirstEdge = new Node<T>(toVer);  
  77.             }  
  78.             else  
  79.             {  
  80.                 Node<T> tem, node = fromVer.FirstEdge;  
  81.                 do  
  82.                 {  
  83.                     if (node.Adjvex.Data.Equals(toVer.Data))  
  84.                     {  
  85.                         throw new ArgumentException("添加了重复的边!");  
  86.                     }  
  87.                     tem = node;  
  88.                     node = node.Next;  
  89.                 } while (node != null);  
  90.                 tem.Next = new Node<T>(toVer); //添加到链表末尾;  
  91.             }  
  92.         }  
  93.         public bool Contains(T item)  
  94.         {  
  95.             //foreach (Vertex<T> v in _items)  
  96.             //{  
  97.             //    if (v.data.Equals(item))  
  98.             //    {  
  99.             //        return true;  
  100.             //    }  
  101.             //}  
  102.             return _items.Any(v => v.Data.Equals(item));  
  103.         }  
  104.   
  105.         private Vertex<T> Find(T item)  
  106.         {  
  107.             //foreach (Vertex<T> v in _items)  
  108.             //{  
  109.             //    if (v.data.Equals(item))  
  110.             //    {  
  111.             //        return v;  
  112.             //    }  
  113.             //}  
  114.             return _items.FirstOrDefault(v => v.Data.Equals(item));  
  115.         }  
  116.   
  117.         public override string ToString()  
  118.         {  
  119.             string result = string.Empty;  
  120.             foreach (var vertex in _items)  
  121.             {  
  122.                 if (vertex != null)  
  123.                 {  
  124.                     result += string.Format("顶点:{0}:", vertex.Data);  
  125.                     if (vertex.FirstEdge != null)  
  126.                     {  
  127.                         Node<T> tem = vertex.FirstEdge;  
  128.                         while (tem != null)  
  129.                         {  
  130.                             result += tem.Adjvex.Data.ToString();  
  131.                             tem = tem.Next;  
  132.                         }  
  133.                     }  
  134.                 }  
  135.                 result += "\r\n";  
  136.             }  
  137.             return result;  
  138.         }  
  139.     }  
  140. }  
执行结果:

原文链接:图的邻接表存储结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值