程序调用入口:
- using System;
- namespace Graphic_AdjacencyList
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- var adjacencyList = new AdjacencyList<char>();
- Console.WriteLine("1.初始化树结构:");
- Console.WriteLine("=================================");
- //添加顶点;
- adjacencyList.AddVertex('A');
- adjacencyList.AddVertex('B');
- adjacencyList.AddVertex('C');
- adjacencyList.AddVertex('D');
- //添加边;
- adjacencyList.AddEdge('A', 'B');
- adjacencyList.AddEdge('A', 'C');
- adjacencyList.AddEdge('A', 'D');
- adjacencyList.AddEdge('B', 'D');
- adjacencyList.AddEdge('C', 'D');
- Console.WriteLine("=================================");
- Console.WriteLine("2.树的邻接表遍历:");
- Console.WriteLine(adjacencyList.ToString());
- Console.Read();
- }
- }
- }
- namespace Graphic_AdjacencyList
- {
- /// <summary>
- /// 链表中的节点
- /// </summary>
- public class Node<T>
- {
- /// <summary>
- /// 邻接点域
- /// </summary>
- public Vertex<T> Adjvex;
- /// <summary>
- /// 下一个邻接点指针域
- /// </summary>
- public Node<T> Next;
- /// <summary>
- /// 链表中的节点
- /// </summary>
- /// <param name="value"></param>
- public Node(Vertex<T> value)
- {
- Adjvex = value;
- }
- }
- }
- using System;
- namespace Graphic_AdjacencyList
- {
- /// <summary>
- /// 链表中的表头节点
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class Vertex<T>
- {
- /// <summary>
- /// 数据
- /// </summary>
- public T Data;
- /// <summary>
- /// 邻接表表头指针
- /// </summary>
- public Node<T> FirstEdge;
- /// <summary>
- /// 访问标志,遍历时使用
- /// </summary>
- public Boolean Visited;
- /// <summary>
- /// 表头节点
- /// </summary>
- /// <param name="value"></param>
- public Vertex(T value) //构造方法;
- {
- Data = value;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Graphic_AdjacencyList
- {
- /// <summary>
- /// 图的邻接表存储类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class AdjacencyList<T>
- {
- /// <summary>
- /// 图的顶点集合
- /// </summary>
- private readonly List<Vertex<T>> _items;
- public AdjacencyList()
- : this(10)
- {
- }
- public AdjacencyList(int capacity)
- {
- _items = new List<Vertex<T>>(capacity);
- }
- /// <summary>
- /// 添加顶点
- /// </summary>
- /// <param name="item"></param>
- public void AddVertex(T item)
- {
- if (Contains(item))
- {
- throw new ArgumentException("插入了重复顶点!");
- }
- _items.Add(new Vertex<T>(item));
- Console.WriteLine("添加顶点:" + item);
- }
- /// <summary>
- /// 添加无向边
- /// </summary>
- /// <param name="from"></param>
- /// <param name="to"></param>
- public void AddEdge(T from, T to)
- {
- Vertex<T> fromVer = Find(from);
- if (fromVer == null)
- {
- throw new ArgumentException("头顶点不存在!");
- }
- Vertex<T> toVer = Find(to);
- if (toVer == null)
- {
- throw new ArgumentException("尾顶点不存在!");
- }
- //无向边的两个顶点都需记录边信息;
- AddDirectedEdge(fromVer, toVer);
- AddDirectedEdge(toVer, fromVer);
- Console.WriteLine(string.Format("添加无向边:{0}—{1}", from, to));
- }
- /// <summary>
- /// 添加有向边
- /// </summary>
- /// <param name="fromVer"></param>
- /// <param name="toVer"></param>
- private void AddDirectedEdge(Vertex<T> fromVer, Vertex<T> toVer)
- {
- if (fromVer.FirstEdge == null) //无临接点时;
- {
- fromVer.FirstEdge = new Node<T>(toVer);
- }
- else
- {
- Node<T> tem, node = fromVer.FirstEdge;
- do
- {
- if (node.Adjvex.Data.Equals(toVer.Data))
- {
- throw new ArgumentException("添加了重复的边!");
- }
- tem = node;
- node = node.Next;
- } while (node != null);
- tem.Next = new Node<T>(toVer); //添加到链表末尾;
- }
- }
- public bool Contains(T item)
- {
- //foreach (Vertex<T> v in _items)
- //{
- // if (v.data.Equals(item))
- // {
- // return true;
- // }
- //}
- return _items.Any(v => v.Data.Equals(item));
- }
- private Vertex<T> Find(T item)
- {
- //foreach (Vertex<T> v in _items)
- //{
- // if (v.data.Equals(item))
- // {
- // return v;
- // }
- //}
- return _items.FirstOrDefault(v => v.Data.Equals(item));
- }
- public override string ToString()
- {
- string result = string.Empty;
- foreach (var vertex in _items)
- {
- if (vertex != null)
- {
- result += string.Format("顶点:{0}:", vertex.Data);
- if (vertex.FirstEdge != null)
- {
- Node<T> tem = vertex.FirstEdge;
- while (tem != null)
- {
- result += tem.Adjvex.Data.ToString();
- tem = tem.Next;
- }
- }
- }
- result += "\r\n";
- }
- return result;
- }
- }
- }
原文链接:图的邻接表存储结构