Esempio n. 1
0
        public void SetEdge(GraphNode <T> v1, GraphNode <T> v2, int v)
        {
            if (!IsNode(v1) || !IsNode(v2))
            {
                throw new InvalidOperationException("v1 or v2 not belong to Graph");
            }
            if (HasEdge(v1, v2))
            {
                return;
            }

            if (v != 1)
            {
                throw new InvalidOperationException("Not a Undirection Grapth");
            }
            var p = new AdjListNode <T>(GetIndex(v2));

            if ((AdjList[GetIndex(v1)]).FirstAdj == null)
            {
                AdjList[GetIndex(v1)].FirstAdj = p;
            }
            else
            {
                p.Next = AdjList[GetIndex(v1)].FirstAdj;
                AdjList[GetIndex(v1)].FirstAdj = p;
            }

            p = new AdjListNode <T>(GetIndex(v1));
            if (AdjList[GetIndex(v2)].FirstAdj == null)
            {
                AdjList[GetIndex(v2)].FirstAdj = p;
            }
            else
            {
                p.Next = AdjList[GetIndex(v2)].FirstAdj;
                AdjList[GetIndex(v2)].FirstAdj = p;
            }
        }
Esempio n. 2
0
        public void DelEdge(GraphNode <T> v1, GraphNode <T> v2)
        {
            if (!IsNode(v1) || !IsNode(v2))
            {
                throw new InvalidOperationException("v1 or v2 not belong to Graph");
            }
            if (!HasEdge(v1, v2))
            {
                return;
            }

            var             p   = AdjList[GetIndex(v1)].FirstAdj;
            AdjListNode <T> pre = null;

            while (p != null)
            {
                if (p.AdjVex != GetIndex(v2))
                {
                    pre = p;
                    p   = p.Next;
                }
            }
            pre.Next = p.Next;

            p   = AdjList[GetIndex(v2)].FirstAdj;
            pre = null;
            while (p != null)
            {
                if (p.AdjVex != GetIndex(v1))
                {
                    pre = p;
                    p   = p.Next;
                }
            }

            pre.Next = p.Next;
        }