26.无向网邻接表类

namespace DSList
{
    //无向网邻接表类(Undirected Net Adjacency List Class)
    public class NetAdjList<T> : IGraph<T>
    {
        //Field
        private ALVexNode<T>[] adjList;     //vertex array
        //Property
        public ALVexNode<T> this[int index]
        {
            get
            {
                return adjList[index];
            }
            set
            {
                adjList[index] = value;
            }
        }
        //Constructor
        public NetAdjList(GvNode<T>[] nodes)
        {
            adjList = new ALVexNode<T>[nodes.Length];
            for (int i = 0; i < nodes.Length; i++)
            {
                adjList[i] = new ALVexNode<T>(nodes[i]);
            }
        }
        //Base methods
        public int GetNumOfVertex()
        {
            return adjList.Length;
        }
        public int GetNumOfEdge()
        {
            int i = 0;
            foreach (ALVexNode<T> nd in adjList)
            {
                AdjListNode<T> p = nd.FirstAdj;
                while (p != null)
                {
                    ++i;
                    p = p.Next;
                }
            }
            return i / 2;
        }
        public bool IsGvNode(GvNode<T> v)
        {
            foreach (ALVexNode<T> nd in adjList)
            {
                if (nd.Data.Equals(v))
                {
                    return true;
                }
            }
            return false;
        }
        public int GetIndex(GvNode<T> v)
        {
            int i = -1;
            for (i = 0; i < adjList.Length; ++i)
            {
                if (adjList[i].Data.Equals(v))
                {
                    return i;
                }
            }
            return i;
        }
        public void SetEdge(GvNode<T> v1, GvNode<T> v2, int v)
        {
            if (!IsGvNode(v1) || !IsGvNode(v2))
            {
                Console.WriteLine("Gvnode is not belong to Graph!");
                return;
            }
            if (v != 0)
            {
                AdjListNode<T> p = new AdjListNode<T>(GetIndex(v2));
                if (adjList[GetIndex(v1)].FirstAdj == null)
                {
                    adjList[GetIndex(v1)].FirstAdj = p;
                    p.Weight = v;
                }
                else
                {
                    p.Next = adjList[GetIndex(v1)].FirstAdj;
                    adjList[GetIndex(v1)].FirstAdj = p;
                    p.Weight = v;
                }
                p = new AdjListNode<T>(GetIndex(v1));
                if (adjList[GetIndex(v2)].FirstAdj == null)
                {
                    adjList[GetIndex(v2)].FirstAdj = p;
                    p.Weight = v;
                }
                else
                {
                    p.Next = adjList[GetIndex(v2)].FirstAdj;
                    adjList[GetIndex(v2)].FirstAdj = p;
                    p.Weight = v;
                }
            }
            else
            {
                Console.WriteLine("Weight is not right!");
            }
        }
        public void DelEdge(GvNode<T> v1, GvNode<T> v2)
        {
            if (!IsGvNode(v1) || !IsGvNode(v2))
            {
                Console.WriteLine("Gvnode is not belong to Graph!");
                return;
            }
            if (IsEdge(v1, v2) == true)
            {
                AdjListNode<T> p = adjList[GetIndex(v1)].FirstAdj;
                AdjListNode<T> pre = null;
                while (p != null && p.AdjVex != GetIndex(v2))
                {
                    pre = p;
                    p = p.Next;
                }
                pre.Next = p.Next;
                p = adjList[GetIndex(v2)].FirstAdj;
                pre = null;
                while (p != null && p.AdjVex != GetIndex(v1))
                {
                    pre = p;
                    p = p.Next;
                }
                pre.Next = p.Next;
            }
            else
            {
                Console.WriteLine("Edge is not existent!");
                return;
            }
        }
        public bool IsEdge(GvNode<T> v1, GvNode<T> v2)
        {
            if (!IsGvNode(v1) || !IsGvNode(v2))
            {
                Console.WriteLine("Gvnode is not belong to Graph!");
                return false;
            }
            AdjListNode<T> p = adjList[GetIndex(v1)].FirstAdj;
            while (p != null)
            {
                if (p.AdjVex == GetIndex(v2))
                {
                    return true;
                }
                p = p.Next;
            }
            return false;
        }
    }
}
posted @ 2011-03-27 13:10  山之松  阅读(203)  评论(0编辑  收藏  举报